cp Command in Linux/Unix with Examples

The cp (copy) command in Linux is a command-line utility that is used to copy files and directories. It is one of the most basic and commonly used commands in Linux and it allows you to create copies of files and directories in the same file system or to move them to another file system.

The basic syntax of the cp command is:

cp [options] [source] [destination]

The "source" is the file or directory that you want to copy, and the "destination" is the location where the file or directory will be copied to.

The most basic use of the cp command is to copy a single file from one location to another. For example, to copy a file called "fruits.txt" from the current directory to the directory "backup", you would use the following command:

cp fruits.txt backup/

This command will create a copy of the "fruits.txt" file in the "backup" directory.

The cp command can also be used to copy a directory and its contents from one location to another. To copy a directory called "fruits" from the current directory to the directory "backup", you would use the following command:

cp -r fruits backup/

This command will create a copy of the "fruits" directory and its contents in the "backup" directory. The -r option is used to copy the directory and its contents recursively.

The cp command also has several options that can be used to modify its behavior. Some of the most commonly used options include:

  • -R: recursive, copies the directory and its contents recursively.
  • -v: verbose, provides detailed information about the progress of the copy.
  • -n: no clobber, does not overwrite an existing file.
  • -p: preserves the modification and access times, and permissions of the files during the copy.

The cp command can also be used in combination with other commands to perform more complex tasks. For example, you can use the cp command in combination with the find command to copy all files of a certain type in a directory to a new location. For example, the following command will copy all files with the .txt extension in the current directory to the directory "backup":

find . -name "*.txt" -exec cp {} backup/ \;

It is important to note that the cp command can only be used by the owner of the file or by a user with superuser privileges. This is because the cp command modifies the ownership and permissions of a file, which are considered sensitive operations. Also, when copying files between different file systems, the cp command may not be able to preserve all the file attributes, such as permissions and timestamps, it is better to use the rsync command for this kind of task.

Another important feature of the cp command is the ability to overwrite or update the destination files with newer versions of the source files. This can be done by using the -u option, which stands for update. The -u option tells the cp command to copy the source file to the destination only if the source file is newer than the destination file or if the destination file does not exist.

cp -u fruits.txt backup/

The cp command also has the ability to copy a file and rename it at the same time. This can be done by specifying the new name of the file at the end of the destination path. For example, the following command will copy the file "fruits.txt" to the "backup" directory and rename it to "vegetables.txt":

cp fruits.txt backup/vegetables.txt

It is also important to note that the cp command can be used to copy files between different devices, such as a hard drive and a USB drive, but it can only copy files within the same file system. To copy files between different file systems, other commands such as tar or rsync should be used.

It's important to note that the cp command can also be used in conjunction with redirection operators to copy the output of a command to a file. For example, the following command will copy the output of the "ls" command to a file called "filelist.txt":

ls > filelist.txt

You can also use the >> operator to append the output of a command to an existing file.

ls >> filelist.txt

Another important feature of the cp command is the ability to copy files over a network. This can be done by using the -a option, which stands for archive. The -a option tells the cp command to preserve the file attributes, such as permissions, timestamps and ownership, during the copy.

cp -a /home/user/fruits.txt [email protected]:/home/user/backup/

It's also important to note that cp command can also be used to copy symbolic links instead of the files they point to. This can be done by using the -d option, which stands for directory. The -d option tells the cp command to copy the symbolic link instead of the file it points to.

cp -d fruits.txt backup/

In addition to the cp command, there are also other similar command-line utilities that can be used to copy files and directories in Linux such as cat, dd, and tee. Both of these utilities have different features and uses. The cat command is used to concatenate and display the contents of files, the dd command is used to copy and convert files, and tee command is used to split the output of a command to multiple files or to a file and the terminal.

In summary, the cp command in Linux is a powerful command-line utility that is used to copy files and directories. It allows you to create copies of files and directories in the same file system or move them to another file system. The cp command has several options that can be used to modify its behavior, such as copying files recursively, preserving file attributes, updating destination files, renaming files, copying files over a network, and copying symbolic links. It also can be used in conjunction with redirection operators to copy the output of a command to a file. It is important to note that the cp command can only be used by the owner of the file or by a user with superuser privileges. Also, when copying files between different file systems, the cp command may not be able to preserve all the file attributes, such as permissions and timestamps, it is better to use the rsync command for this kind of task. It's also important to be aware of other similar command-line utilities such as cat, dd, and tee, which can also be used for copying files and directories in Linux, but have different features and uses.