Copying files via the command line. xcopy command: options

COPY command when working on the command line, allows you to copy one or more files. The command syntax is: COPYsource[+ source [+ ...]][result]. The source is the name of the file being copied, and the result is the directory where the copy of that file will be placed and/or the name of the file being created.

For example, let’s create some file on drive “C” (I created the text file “robot.txt”), and on drive “D” some folder (I created folder “folder1”). Then the command to copy the file “robot.txt” to the folder “folder1” looks like this: copy robot.txt d:\folder1

If you need to copy a file but change its name, you need to write the following command: copy robot.txt d:\folder1\file.txt In this case, the file “robot.txt” will be copied to the folder “folder1”, but with the name “file.txt”.

In order to copy all files with the selected extension, you must put an asterisk “*” instead of the file name. For example, let’s create some folder on the “C” drive (I created the “papka” folder) and copy into it all the text files from the “folder1” folder located on the “D” drive. The command will look like: copy d:\folder1\*.txt c:\papka

If you do not specify a directory as [result], the copy command will copy the file(s) and place them in the current directory. In this case, the copied files will have the same name, creation date and time as the original file. For example, let’s create a text file named “robot1.txt” in the folder “folder1” located on drive “D”. After that, in the command line, change the current directory from “C:\>” to “C:\Fapka”. Then the copy d:\folder1\robot1.txt command will copy the file “robot1.txt” from the folder “folder1” and place it in the current directory, i.e. to the “papka” folder located on the “C” drive. Moreover, the name, date and time of creation of the copied file will be identical to the original file.

In order to copy all files from a specific directory, you must put an asterisk “*” instead of the file name and an asterisk “*” instead of the file extension. For example, let's create several files in the “folder1” folder with different extensions (Word document, Rar archive, etc.). Then the command to copy all files from the folder “folder1” to drive “C” will look like: copy d:\folder1\*.* c:\


The [source] or [result] of copying can be not only files, but also various computer devices. The following device names are supported on Windows:

  • LPT1 – LPT3 (parallel ports).
  • COM1 – COM3 (serial ports).
  • CON (terminal, for output it is the computer screen, for input it is the keyboard).
  • PRN (printer).
  • AUX (device connected to serial port 1).

For example, using the command line and using the “copy” command, you can create a text file and write information to it: copy con f1.txt . The copy con f1.txt command will create a text file “f1.txt” and copy the characters you enter from the keyboard into it.

After entering the command copy con f1.txt, press Enter and enter the words that need to be saved (I entered command com). After entering, you must mark the end of the file (press Ctrl+Z).
Using the copy command you can merge multiple files into one file. For example, let’s create two files f2.txt and f3.txt on drive “C” (we created the f1.txt file earlier) and write some information to these files. Then the team copy f1.txt+f2.txt+f3.txt d:\f4.txt will copy the contents of the files “f1.txt”, “f2.txt”, “f3.txt” to the file “f4.txt”, which will be automatically created on the “D” drive.

If, when merging files, you do not specify the file - [result], then all information will be saved in the first file. For example, the command copy f1.txt+f2.txt+f3.txt will add the contents of file f1.txt to the contents of files f2.txt and f3.txt

Key /Y disables the message about replacing files. By default, when copying files, if such a file exists, a message warning about the replacement is displayed on the screen. The user can agree with the replacement (press Y) or disagree (press N). The /Y switch disables this message and copying occurs immediately. For example, let’s create a file vov.txt on drive “C” and copy it to drive “D”: copy vov.txt d:\
Then copy this file again. In this case, the message “Replace d:\vov.txt ] [result ] is displayed

Let's start:

/D– indicates the possibility of creating an encrypted file.

/Y | /-Y– if the first parameter is selected, then there will be no system prompts for confirmation during copying (in other words, if you have to copy 1000 files in bulk, then the annoying “ “ Are you sure, or maybe call a friend?"), it is clear that the second parameter includes a confirmation request.

/Z– allows you to copy data over the network; by the way, this key was not present in previous versions of this utility.

/A– indicates that the file is a text file; if we specify this parameter, copying will be interrupted at the end-of-file mark ASCII code 26.

If this parameter is indicated at the very end, then the effect is completely different. The document will be completely copied, but an end-of-file mark will be added to the copy if it was not there. As a result, you can try experimenting with a pdf document (my_file.pdf - document size is 247217 bytes):

copy my_file.pdf/A new_file.pdf– at the output we will receive a document new_file.pdf with a size of 2538 bytes.

/B– this key is the default and indicates that the file is binary. That is, the entire document is copied.

/V– this parameter enables checking the correctness of copying by comparing the data after the procedure is completed.

And so the command line, copying files and a couple of examples:

Copying the 123.txt file from the current directory to the D:\NEW directory under the same name via the command line:

COPY 123.txt D:\NEW

Copying file 123.txt from the current directory to the D:\NEW directory under the new name new.txt:

COPY 123.txt D:\NEW\new.txt

Copying all documents with a txt extension from drive D:\ to the My Data directory on drive C:

COPY D:\*.txt "C:\My Data"

Let's say we are in the D:\NEW directory and we need to copy all the data from the C:\DATA directory into it, in this case it is enough to write the following syntax:

COPY C:\DATA *.*

Using the command line, copied files can be merged. For these purposes, it is enough to use the plus sign “+”, for example:

COPY 1.txt +2.txt 3.txt– in this example, the contents of documents 1.txt and 2.txt were combined into 3.txt. It is worth remembering that by default, the COPY utility perceives data as text.

Here's an example with binary data:

COPY /B *.dat all.dat– in this option, all data with the dat extension from the current folder were combined into one file all.dat.

In general, using the plus symbol “+” and thinking like “what if”, you can do a couple of experiments. It is also worth remembering a number of points:

If the name of the target object is the same as one of the copies (except the first one), then the original contents of the target object will be lost.

If the name of the target object is omitted, then the first object from the list will be used as its name:.

COPY 1.txt +,– this construction allows you to assign the current date to the 1.txt object without modifying it.

The COPY utility, due to its primacy, contains a number of disadvantages:

  • You cannot copy files via the command line with the hidden or system attributes, as well as system files with the “Read-Only” attribute.
  • interruption of the entire copying process when encountering an object that cannot currently be copied.

Due to such inconveniences, smart people thought and added the letter X to the name COPY, which solved a lot of problems by creating.

REPLACE Command Line

In fact, the difference between this utility is that it can replace data. The syntax is quite simple and clear:

REPLACE [drive1:][path1]filename [drive2:] [path2]

REPLACE [drive1:][path1]filename [drive2:] [path2]

[drive1:] [path1]filename – determine the location of the copied object, or objects.

[drive2:] [path2] - determine the location of the result object. As you can see, names of replaced objects cannot be specified.

/A– add to the result directory only new objects from the result directory (without overwriting). This key cannot be used with keys /S And /u.

/R– You are prompted for confirmation before replacing the target or adding the source file.

/R– replacement of not only ordinary, but also write-protected documents.

/S– allows you to search all subdirectories of the target directory and replace files with matching names. This key cannot be used in conjunction with the /A. Directories specified by the path1 parameter are not searched.

/W– wait for the disk to be inserted before starting execution

/U– update mode, that is, only those objects that have an earlier modification date will be replaced. Key incompatibility /A.

Replace D:\Data\my_file.txt D:\Work– in the Work folder, replace the file my_file.txt with D:\Data\my_file.txt

When copying files via the command line using the REPLACE utility, there is no way to update hidden or system files. To do this, you must first change their attributes.


The XCopy utility is one of the oldest and most useful data transfer tools. The purpose of XCopy is to change the location of folders. Explorer has largely replaced this utility, and we recommend using it whenever possible. However, there are several important uses of the XCopy utility:

  • scripts for file manipulation;
  • moving individual files in a complex structure;
  • copying all available files, even if some files cannot be moved;
  • archiving data from one system to another;
  • moving files with specific attributes.

Many people use XCopy as their archiving application because it does a great job. You can easily copy data files from a folder to backup media, such as another hard drive, floppy disk, Zip drive, or network drive. Unfortunately, XCopy does not support traditional media such as magnetic tape. In addition, the media you are copying to must be partitioned, so XCopy does not allow you to burn files to CDs.

How to use?

The XCopy utility is used in the same way as the standard copy command - specifying the source and target files. For example, to copy all files from the MyDir folder on drive C to the same folder on drive D, enter the command:

XCopy C:\MyDir\*.* D:\MyDir\*.*

Then press the Enter key. The XCopy utility supports a set of keys, the most important of which are listed below.

Additional keys

/A and /M- only archive files are copied. With these keys, the XCopy utility can be used for archiving. The /M switch also resets the archive attribute on copied files, indicating that they have been backed up. Therefore, when using XCopy as an archiving application, you should only use the /M switch.

/C- continues copying even if data errors occur. Use this feature to copy all possible contents from the damaged folder to a new location. Explorer does not have this function - copying stops after the first error.

/G- allows you to copy encrypted files to a location that does not support encryption. This function decrypts the file and you must have the appropriate system rights to run it.

/H- copies hidden and system files.

/K- copies file attributes along with the files themselves. In normal mode, the XCopy utility strips files of the read-only attribute.

/O and /X- protection of copied files. The /O key, along with the file, copies information about its owner and the access control table. The access control table contains information about the users who have access rights to the file and the actions they can perform with it. The /X switch also copies the file's audit information. This feature is important when monitoring file access.

/R- overwrites files with the read-only attribute when copying. Use this key when archiving to ensure that write-protected file versions are updated in the backup.

/S and /E- copy files not only from the current folder, but also from all its subfolders. The /E switch also copies empty subfolders. The specified keys allow you not only to copy files, but also to preserve the folder structure.

/T- creates a copy of the original folder structure without copying any files. Use this feature to create new projects. Sometimes it is useful to just repeat the folder structure without copying specific information from it.

/U- copies from the source folder only those files that are present in the target folder. This feature helps update a portion of the files in the target folder, eliminating the need to specify each file individually.

/V- checks all copied files, comparing their contents with the original files. This switch increases the copying time, but it should always be used when archiving using the XCopy utility. Otherwise, there is a risk of backing up damaged data and not knowing about it.

COPY command

Source [+ source [+ ...]] [result]:

sourceThe names of one or more files to be copied.

/AFile is an ASCII text file.

/BFile is a binary file.

resultThe directory and/or name for the target files.

/VChecks that files are copied correctly.

/N Use short names if possible when copying files whose names do not comply with the 8.3 standard.

/Y Suppresses the confirmation prompt to overwrite an existing target file.

/-Y Mandatory confirmation request to overwrite an existing target file.

The /Y switch can be set via the COPYCMD environment variable.

The /-Y command line switch overrides this setting.

By default, confirmation is required unless the COPY command is executed in a batch file

XCOPY command

To combine files, specify one target and multiple source files using wildcards or the format "file1+file2+file3+...". Copying files and directory structures.

source [result] ] ]

[+file3]...]

source Copied files.

result The location and/or names of the new files.

/A Copies only files with the archive attribute set. The attribute itself does not change.

/M Copies only files with the archive attribute set. After copying, the attribute is removed.

/D: dateCop. files modified on or before the specified date. If no date is specified, only the target files are replaced

/EXCLUDE:file1[+file2][+file3]...List of files containing strings. When any of the strings MATCHES any part of the absolute path of a file being copied, that file is excluded from the copy operation. For example, by specifying the string obj or .obj, you can exclude all files from the obj folder or all files with the obj extension, respectively.

/P Print prompts before creating each new file.

/S Copies directories with subdirectories (except empty ones).

/E Copy directories with subdirectories (including empty ones).

Equivalent to the /S /E switch combination. Compatible with /T key. /V

Comparison of final files with source ones.

/W Prompts you to press a key before copying.

/C Continue copying, regardless of errors.

/I If the result does not exist and multiple files are copied, the directory name is assumed to be specified.

/Q Prevents displaying the names of copied files.

/F Displays the full names of source and destination files.

/L Displays the names of copied files.

/H Copies hidden and system files (among others).

/R Allow read-only file replacement.

/T Creates a directory structure (except empty directories) without copying files. If you need to create empty directories and subdirectories, use the /T /E switch combination.

/U Copies only files already present in the destination directory.

/K Copies the Read Only attribute (usually it is reset).

/N Use short names when copying.

/O Copies owner information and ACL information.

/X Copies file audit settings (requires /O).

/Y Suppresses the confirmation prompt to overwrite an existing target file. /-Y Mandatory confirmation request to overwrite an existing target file.

/Z Copy network files with resume.

The /Y switch can be set via the COPYCMD environment variable. The /-Y command line switch overrides this setting.

Example: 1. Copy text.txt to A:TasksNew

2. When copying, the file name will be changed

In order to fully work with files and directories on the command line, we need to be able to create, move, copy and delete them. For each of these actions there is a corresponding command.

Although, to be precise, the same command is used to rename files and move them, because it performs the same operation of editing the table in which the directory stores the list of files located inside this directory.

Working with files on the command line

Creating a file

To create a file, you can use the touch command. In fact, the purpose of this command is to change the last access date of the file and the last modification date of the file.

Executing the command without parameters touch file_path/file will update the file's last access time and last modification time. If such a file does not exist, then an empty file with the name specified in the command will be created.

To update only the access date, you need to use the ‑a parameter; to update only the modification date, use the ‑m parameter. You can also set not the current, but an arbitrary date, using the -t parameter, and specifying after it the date in the format MMDDhhmm[.SS], for example, 201203201359.11, which corresponds to 2012, 03 month 20 day 13 hours 59 minutes 11 seconds.

Creating a directory

To create directories, there is the mkdir command (from English m a k e dir sector).

Syntax: mkdir directory_name... , where directory name— this is the path and name of the directory to be created. You can specify multiple directories separated by a space.

If the path to the directory being created contains non-existent directories, the directory will not be created, but an error message will be displayed. To create multiple nested directories, use the -p parameter. For example, the command:

Mkdir -p folder1/folder2/folder3

will create all three directories folder1, folder2, folder3 in case they do not exist.

By default, directories are created with permissions set to 755; to create a directory with a different set of rights, use the ‑m access_rights parameter, for example: mkdir ‑m 750 folder .

Moving and changing the name of a file or directory

The names of files or directories are not stored in the files themselves, but are entries in a table that contains a list with the names of files and directories located inside this directory. Thus, when renaming a file or moving it to another directory, the same operation is performed - editing the directory table with a list of files. Therefore, the same mv command (from English m o v e).

Command syntax: mv source_file new_file where original file is the path to the file or directory and the name of the file or directory you want to move, and new file is the path to the destination where the file or directory needs to be moved, and the new name for the moved file or directory.

mv /Users/ixrevo/Downloads/hello.txt /Users/ixrevo/Desktop/hi.txt in this example we move the hello.txt file from the /Users/ixrevo/Downloads/ directory to the desktop /Users/ixrevo/Desktop/ . and the name of the moved file will be hi.txt (the contents of the file will remain unchanged).

Given that our current directory was /Users/ixrevo/ , we could specify a relative path to the file:
cp Downloads/hello.txt Desktop/hi.txt

You can read more about the current directory in the article “Navigating the file system on the command line,” and about relative and absolute paths in the article “How the Mac OS X file system is structured.”

You can also rename a file or directory without moving it:

Mv hello.txt hi.txt

Or move without renaming:

Mv /Users/ixrevo/Downloads/hello.txt /Users/ixrevo/Desktop/

Why do you need to specify only the directory where to move the file or directory, without specifying a new name (or specify the old name).

Another way to use the mv command is useful for moving multiple files or directories: mv source_file_1 source_file_2 ... directory where source file_1, source file_2, etc.— paths and names of files or directories that need to be moved, and directory— this is the path to the directory where you need to move the files specified earlier.

Copying a file

To copy files and directories, there is the cp command, which is short for c o p y.

Command syntax: cp source_file new_file where original file is the path and name of the file that needs to be copied, and new file— this is the path to the created copy of the file and the name of this copy.

For example, by running the command:

Cp /Users/ixrevo/Downloads/hello.txt /Users/ixrevo/Desktop/hi.txt

We will copy the file hello.txt from the /Users/ixrevo/Downloads/ directory to the desktop /Users/ixrevo/Desktop/, and the name of the copy of the file will be hi.txt.

The cp command also allows you to copy multiple files:

Cp source_file_1 source_file_2 ... directory

Source file_1, source file_2, etc.— paths to files and names of files that need to be copied, and directory- this is the path to the directory where you want to copy the files specified earlier.

Copying a directory

To copy directories, use the cp command with the ‑R or ‑r option (from English r ecursive). The cp command executed with this option, if a directory was specified as the first argument, will copy the directory and all its contents, that is, all subdirectories and files.

The command syntax in this case is as follows:

Cp -r copied_directory destination

If you try to copy a directory without the -r parameter, the directory will not be copied, but an error message will be displayed.

Deleting a file

To delete files, use the rm command (from English r emo v e). Syntax: rm file... , where file is the path and name of the file that needs to be deleted. You can also specify multiple files to delete.

Removing a directory

There are several ways to delete directories. For example, you can use the command

Rmdir directory...

directory— this is the path and name of the directory that needs to be deleted. You can also specify multiple directories for deletion.

If the directory is not empty, that is, it contains nested files and/or folders, then the rmdir command will not be able to delete this directory and will display an error message.

The rm command with the -d parameter works similarly to the rmdir command.

Of course, deleting the contents of a directory first each time, and then the directory itself, is not very interesting, but, fortunately, there is a way to delete a directory along with all its contents. To do this, run the rm command with the ‑R or ‑r parameter, and then all files and directories specified in the rm ‑r directory... command will be deleted along with all subdirectories. But be careful not to delete anything you need with this command, especially since there is no Recycle Bin on the command line and files and directories are permanently deleted.