cp Command: Tutorial & Examples

Copy files and directories

The cp command is a Unix and Linux command used to copy files and directories. It stands for "copy." The cp command allows you to copy files and directories from one location to another.

Here is the basic syntax for the cp command:

cp source destination

Here, source is the name of the file or directory that you want to copy, and destination is the location where you want to copy the file or directory.

You can specify multiple source files and directories, separated by spaces. The cp command will copy each of the specified sources to the specified destination. For example:

cp file1 file2 directory

This would copy file1 and file2 to the specified directory.

You can use the -R option to recursively copy a directory and all of its contents. For example:

cp -R directory1 directory2

This would copy the contents of directory1, including any subdirectories and their contents, to directory2.

You can use the -p option to preserve the file attributes, such as the owner and permissions, when copying files. For example:

cp -p file1 file2

This would copy file1 to file2 and preserve the file attributes of file1 in the copy.

Note that you must have the appropriate permissions to use the cp command. You must have read permission for the source file or directory, and write permission for the destination location.

Except where otherwise noted, content on this site is licensed under a CC BY-SA 4.0 license CC BY SA