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 contain one or more files or directories as arguments, and the
DESTINATION argument can be a single file or directory .
- When the
DESTINATIONarguments are both files, the
cpcommand copies the first file to the second one. If the file doesn’t exists the command creates it.
- When the
SOURCEhas multiple files or directories as arguments, the
DESTINATIONargument must be a directory. In this situation, the
SOURCEfiles and directories are moved to the
- When the
DESTINATIONarguments are both directories, the
cpcommand 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, a permission denied error is shown.
Copying Files with cp Command
The most basic scenario of using
cp is to copy 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 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 is overwritten. The
-n option tells
cp not to overwrite an existing file.
cp 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
When copying a file, the new file is owned by the user running the command. Use the
-poption to preserve the file mode, ownership, and timestamps:
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
In the following example we are copying the directory
cp -R Pictures Pictures_backup
The command above creates the 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 is copied inside the destination directory. To copy only the files and subdirectories but not the target directory, 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 does 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 also be 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, specify 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.
If you have any questions or feedback, feel free to leave a comment.