Linux cp command
On Unix-like operating systems, the cp command makes copies of files and directories.
The general form of the command is cp source destination, for example:
cp myfile.txt myfilecopy.txt
Like many core Linux commands, if the cp command is successful, by default, no output is displayed. To view output when files are copied, use the -v (verbose) option.
By default, cp will overwrite files without asking. If the destination file name already exists, its data is destroyed. If you want to be prompted for confirmation before files are overwritten, use the -i (interactive) option.
cp [option]... [-T] source destination
cp [option]... source... directory
cp [option]... -t directory source...
|Same as -dR --preserve=ALL. When performing the copy, attempt to preserve as much of the original file structure, attributes, and associated metadata as possible. This metadata includes security context data if you are running SELinux.|
|--attributes-only||Don't copy the file data, only create a file with the same attributes. If the destination file already exists, don't alter its contents. You can control exactly which attributes are copied with the --preserve option.|
|--backup[=control]||Make a backup of each existing destination file that would otherwise be overwritten or removed. The control parameter specifies what version control method to use; see version control for details.
As a special case, cp --force --backup makes a backup of source when source and dest are the same, regular file.
|-b||Like --backup, but does not accept a control argument; the default control method is always used.|
|--copy-contents||When operating recursively, copy contents of special files, such as FIFOs and devices found in /dev. You usually don't want to use this option, because it can have undesired results, such as hanging forever or filling up your entire disk. However, this option is available for special, expert use cases.|
|-d||Copy symbolic links themselves, rather than the files they refer to, and preserve hard links between source files in the copies. Same as "--no-dereference --preserve=links".|
|If an existing destination file cannot be opened, remove it and try again. This option has no effect if the -n/--no-clobber option is used. However, it applies independently of -i/--interactive; neither option cancels the effect of the other.|
|Prompt before overwrite (overrides a previous -n option).|
|-H||Follow symlinks specified on the command line, but preserve discovered links.
If one of the arguments on the command line is a symbolic link, copy the referenced file, not the link itself. However, if a symbolic link is discovered during recursive traversal, it's copied as a symlink, not a regular file.
|Create hard links to files instead of copying them.|
|Always follow symbolic links in source; if source is a symlink, copy the file linked to rather than the symlink itself. When this option is specified, cp cannot create symlinks in the destination copies.|
|Do not overwrite an existing file. If -i/--interactive was previously specified, this option overrides it. This option cannot be specified with -b/--backup, because backups are only created when a file would be overwritten.|
|Never follow symbolic links in source; copy symlinks as symlinks. Existing symlinks encountered in the destination may still be followed, however.|
|-p||Same as --preserve=mode,ownership,timestamps.|
|--preserve=[attr_list]||Preserve the specified attributes, separated by a comma. Attributes are:
|--no-preserve=attr_list||Don't preserve the specified attributes.|
|--parents||Create missing parent directories in the destination, if necessary, when copying to target directory, according to the pathname specified in source.
For instance, "cp --parents dir2/dir3/file dir1" creates the destination file "dir1/dir2/dir3/file", even if dir2 and dir3 do not presently exist under dir1.
|Copy directories recursively.|
|--reflink=[when]||Perform an optimized CoW ("copy-on-write") clone, if the destination filesystem supports it. The resulting copy will share the same bytes on disk as the original file, until the copy is modified. Be aware that this means if the source bytes are corrupted, the destination will share the corrupted data.
The when parameter defines what will happen if copy-on-write is not supported by the destination filesystem. If when is always (the default), the copy fails. If auto, copying will proceed, using standard copy behavior.
|--remove-destination||Remove each existing destination file before attempting to open it (contrast with the --force option, which only removes the destination after a failed attempt to open).|
|--sparse=when||Control creation of sparse files.
A sparse file contains "holes", where a hole is a sequence of zero bytes that occupies no physical disk space. When the file is read, the holes are read as zeros. This can conserve disk space since many files contain long sequences of zeros. By default, cp detects sparse files and creates sparse destination files as well.
The when parameter defines what cp should do when a source file is detected to be sparse:
|--strip-trailing-slashes||Remove any trailing slashes from each source argument.|
|Make symbolic links instead of copying the files themselves. All source files must be absolute pathnames starting with a slash, unless the destination files are in the current directory.|
|Override the usual backup suffix.|
|Copy all source arguments into directory|
|Treat destination as a normal file.|
|Copy only when the source file is newer than the destination file or when the destination file is missing.|
|Verbose mode; explain what is being done.|
|Only operate on the filesystem where the command was executed. If cp tries to cross the boundary to another filesystem, those files are skipped. This includes networked drives, another partition — any file that resides on a filesystem with a different mount point.
The directory representing the mount point itself is copied, but not traversed.
If -v is specified, you will see exactly which files are skipped.
|--help||Display a help message, and exit.|
|--version||Output version information, and exit.|
By default, sparse source files are detected by a crude heuristic and the corresponding destination file is made sparse as well. That is the behavior selected by --sparse=auto. Specify --sparse=always to create a sparse destination file whenever the source file contains a long enough sequence of zero bytes. Use --sparse=never to inhibit creation of sparse files.
When --reflink[=always] is specified, cp performs a lightweight copy, where the data blocks are copied only when modified. If this is not possible, the copy fails; or, if --reflink=auto is specified, cp falls back to a standard copy operation.
When using -b/--backup, the backup suffix is '~', unless set with --suffix or SIMPLE_BACKUP_SUFFIX. The version control method may be selected via the --backup=control option or through the VERSION_CONTROL environment variable. The possible values of control or VERSION_CONTROL are:
|none, off||Never make backups (even if --backup is given).|
|numbered, t||Make numbered backups.|
|existing, nil||Numbered if numbered backups exist, simple otherwise.|
|simple, never||Always make simple backups.|
As a special case, cp makes a backup of source when the force and backup options are given and source and destination are the same name for an existing, regular file.
Let's say you have a file named picture.jpg in your working directory, and you want to make a copy of it called picture-02.jpg. You would run the command:
cp picture.jpg picture-02.jpg
...and the file is copied. Here, picture.jpg is the source of the copy operation, and picture-02.jpg is the destination. Both files now exist in your working directory.
The source and destination files may also reside in different directories. For instance,
cp /home/chuck/pictures/picture.jpg /home/chuck/backup/picture.jpg
...makes a copy of the file /home/chuck/pictures/picture.jpg in the directory /home/chuck/backup. The destination file is also named picture.jpg.
If you are the user chuck, you can abbreviate your home directory ("/home/chuck") using a tilde ("~"). For instance,
cp ~/pictures/picture.jpg ~/backup/picture.jpg
...functions the same as the above command when it is run by chuck.
Copying multiple files to a directory
Or, perhaps you want to copy multiple files into another directory. To accomplish this, you can specify multiple files as the source, and a directory name as the destination. Let's say you are the user sally, and you have a bunch of files in the directory /home/sally/pictures/ named picture-01.jpg, picture-02.jpg, etc. and you want to copy them into the directory /home/sally/picture-backup/. This command will do the trick:
cp ~/pictures/picture-*.jpg ~/picture-backup
Here, we use a wildcard (the asterisk, "*") to indicate that the source files are all the files in the directory /home/sally/pictures whose name starts with "picture-" and has the extension ".jpg". They are copied into the directory /home/sally/picture-backup, assuming that directory already exists. If it doesn't exist, cp gives you an error message, and no files are copied.
You can also specify multiple source files one after the other, and cp will expect that the final argument is a directory name, and copy them all there. For instance,
cp ~/pictures/picture-01.jpg ~/pictures/picture-02.jpg ~/picture-backup
...will copy only those two files, /home/sally/picture-01.jpg and /home/sally/picture-02.jpg, into the directory /home/sally/picture-backup.
Copying files recursively
You can use cp to copy entire directory structures from one place to another using the -R option to perform a recursive copy. Let's say you are the user steve and you have a directory, /home/steve/files, which contains many files and subdirectories. You want to copy all those files, and all the subdirectories (and the files and subdirectories they contain), to a new location, /home/steve/files-backup. You can copy all of them using the command:
cp -R ~/files ~/files-backup
...and the entire directory structure is copied to the directory /home/steve/files-backup. When performing a recursive copy:
- If the directory files-backup already exists, the directory files are placed inside.
- If files-backup does not already exist, it will be created and the contents of the files directory is placed inside it.
Creating symbolic links instead of copying data
Another useful trick is to use cp to create symbolic links to your source files. You may already be familiar with using the ln command to create symlinks; cp is a great way to create multiple symlinks all at once.
cp creates symbolic links if you specify the -s option. So, for instance,
cp -s file.txt file2.txt
...creates a symbolic link, file2.txt, which points to file.txt.
You can also create symbolic links from multiple source files, specifying a directory as the destination.
To create symbolic links in another directory, cp needs you to specify the full pathname, including the full directory name, in your source file name(s). Relative paths will not work.
Let's say you are user melissa and you have a set of files, file01.txt, file02.txt, etc. in the directory /home/melissa/myfiles. You want to create symbolic links to these files in the existing directory /home/melissa/myfiles2. This command will do the trick:
cp -s ~/myfiles/file*.txt ~/myfiles2
The directory myfiles2 now contain symbolic links to the file*.txt in the directory /home/melissa/myfiles. The myfiles2 directory must already exist for the operation to succeed; if it doesn't exist, cp gives you an error message and nothing is copied.
This will work with a recursive copy, as well. So the command:
cp -R -s ~/myfiles ~/myfiles2
...will re-create the directory structure of /home/melissa/myfiles, including any subdirectories and their contents; any files are created as symlinks to the originals, but the directories are not symbolic links, only regular directories. If myfiles2 already exists, cp creates a directory inside it called myfiles which contains the directory structure and symlinks; if myfiles2 does not already exist, it is created, and contain the subdirectories and symlinks to the files that myfiles contains.
There are other options you can provide to cp that affect its behavior. These are listed, with the precise command syntax, in the following sections.
Make a copy of a file into the same directory
cp origfile newfile
Creates a copy of the file in the working directory named origfile. The copy is named newfile, and is located in the working directory.
If you want to be prompted before overwriting a file, use the -i (interactive) option. For example:
cp -i oldfile newfile
If newfile already exists, you are prompted:
cp: overwrite ‘newfile’?
If you type y (or yes, Y, YES, or any other combination of upper and lowercase of these), then newfile is overwritten with a copy of origfile. Typing anything else will abort the operation.
Copy a file into another directory
cp origfile /directory/subdirectory
Creates a copy of the file in the working directory named origfile. The copy is located in the directory /directory/subdirectory, and is named origfile.
cp origfile /directory/subdirectory/.
Same as the above command. The slash-dot (/.) is implied in the above form of the command. (The dot is a special file in every Linux directory which means "this directory.")
Copy a file into another directory, and give it a new name
cp origfile /directory/subdirectory/newfile
Creates a copy of the file in the working directory named origfile. The copy is named newfile, and is located in the directory /directory/subdirectory.
Copy multiple files into another directory, using a wildcard
cp file* /directory/subdirectory
Copy every file in the working directory whose name begins with file into the directory /directory/subdirectory. The asterisk ("*") is a wildcard — a special character which expands to match other characters. Specifically, the asterisk wildcard matches zero or more non-whitespace characters. For instance, this command will copy any files named file, file001, file.txt, fileone.jpg, file-archive.zip, etc.
cp file*.jpg /directory/subdirectory
Copy every file in the working directory whose name begins with file, and ends with the file extension .jpg. For instance, it would make copies of any files named file, file001.jpg, file002.jpg, or file-new.jpg, etc. The copies are placed into the directory /directory/subdirectory.
Copy an entire directory structure to another location
cp -R /one/two /three/four
Copy the directory two (located in the directory /one), and everything two contains, into the destination directory /three/four. The result is called /three/four/two. The directory /three must already exist for the command to succeed. If the directory four does not already exist in the directory /three, it is created.
cp file1.txt newdir
Copies the file1.txt in the working directory to the newdir subdirectory.
cp /home/public_html/mylog.txt /home/public_html/backup/mylog.bak
Copies the file mylog.txt in the public_html directory into the public_html/backup directory as mylog.bak.
cp -u *.txt newdir
Copy all files ending in .txt into the newdir directory, but only if the files do not already exist in the new directory, or if the files being copied are newer.
cp -R /home/hope/files/* /home/hope/backup
Recursively copies all the files, directories, and subdirectories in the /home/hope/files directory into the /home/hope/backup directory. If the directory backup does not exist in the directory /home/hope, it is created.
cp --backup origfile newfile
If newfile already exists, make a backup of the existing newfile before overwriting it with a copy of origfile. By default, the backup of newfile is named newfile~.
cp --backup=numbered origfile newfile
If newfile already exists, make a backup of the existing newfile before overwriting it with a copy of origfile. The backup of newfile is named newfile.~1~ if no other backup exists, or newfile.~2~ if newfile.~1~ exists, etc.