Cp Command in Linux (Copy Files)
4 min read
When working on Linux and Unix systems, copying files and directories is one of the most common tasks you’ll perform on a daily basis.
cp is a command-line utility for copying files and directories on Unix and Linux systems. In this article, we will explain how to use the
How to Use
The general syntax for the
cp command is as follows:
cp [OPTIONS] SOURCE... DESTINATION
SOURCE can have one or more files or directories as arguments and the
DESTINATION can have a single file or directory an as argument.
- When the
DESTINATIONarguments are both files the
cpcommand copies the first file to the second one. If the file doesn’t exist the command will create it.
- When the
SOURCEhas multiple files or directories as arguments, the
DESTINATIONargument must be a directory. In this case, the
SOURCEfiles and directories are moved to the
- When the
DESTINATIONarguments are both directories the
cpcommand will copies the first directory into the second one.
To copy files and directories you must have at least read permissions on the source file and write permission on the destination directory. Otherwise, you will get a permission denied error.
Copying Files with cp Command
The most basic scenario of using
cp is copying a file in the current working directory. For example, to copy a file named
file_backup.txt you wound run the following command:
cp file file_backup
To copy a file to another directory specify the absolute or the relative path to the destination directory. When only the directory name is specified as a destination the copied file will have the same name as the original file.
In the following example we are copying the file
file.txt to the
cp file.txt /backup
If you want to copy the file under a different name you need to specify the desired file name. The command below will copy the file to the specified directory as
cp file.txt /backup/new_file.txt
By default, if the destination file exists it will be overwritten. The
-n option tells
cp to not overwrite an existing file. To prompt for confirmation use the
cp -i file.txt file_backup.txt
If you want to copy the file only if it is newer than the destination use the
cp -u file.txt file_backup.txt
cp -p file.txt file_backup.txt
Another option that can be useful is
-v, which tells
cp to print verbose output:
cp -v file.txt file_backup.txt
'file.txt' -> 'file_backup.txt'
Copying Directories with cp Command
To copy a directory, including all its files and subdirectories, use the
-r option. In the following example we are copying the directory
cp -R Pictures Pictures_backup
The command above will create a destination directory and recursively copy all files and subdirectories from the source to the destination directory.
If the destination directory already exists the source directory itself and all its content will be copied to the destination directory. To copy only the files and subdirectories but not the target directories use the
cp -RT Pictures Pictures_backup
Another way to copy only the directory’s content but not the directory itself is to use the wildcard character (
*). The disadvantage of the following command is that it will not copy the hidden files and directories (the ones starting with a dot
cp -RT Pictures/* Pictures_backup/
All the options we used in the previous section when copying files can be also used when copying directories. The main difference is that when copying directories you always need to use the
Copy Multiple Files and Directories
To copy multiple files and directories at once specifying their names and use the destination directory as the last argument:
cp file.txt dir file1.txt dir1
When copying multiple files the destination must be a directory.
Copying files and directories with the
cp command is a simple task. For information about all available
cp options type
man cp in your terminal.