Cp Command in Linux (Copy Files)

Posted 

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 cp command.

How to Use cp command

The general syntax for the cp command is as follows:

cp [OPTIONS] SOURCE... DESTINATION

The 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 SOURCE and DESTINATION arguments are both files the cp command copies the first file to the second one. If the file doesn’t exist the command will create it.
  • When the SOURCE has multiple files or directories as arguments, the DESTINATION argument must be a directory. In this case, the SOURCE files and directories are moved to the DESTINATION directory.
  • When the SOURCE and DESTINATION arguments are both directories the cp command 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.txt to file_backup.txt you wound run the following command:

cp file file_backup

or

cp 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 /backup directory:

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 new_file.txt.

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 -i option.

cp -i file.txt file_backup.txt

If you want to copy the file only if it is newer than the destination use the -u option:

cp -u file.txt file_backup.txt

When copying a file, the new file will be owned by the user running the command. Use the -p option 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 -R or -r option. In the following example we are copying the directory Pictures to Pictures_backup:

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 -T option:

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 -R option.

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.

Conclusion

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 want to copy files over the network check the rsync and scp commands.