The Linux cp Command
The Linux cp command stands as an abbreviation for the copy command. The cp command helps with quickly copying files as well as directories. This is a widely used command and comes with several options catering to basic as well as more complex copying requirements.
Knowing the Linux cp command is an essential skill in your Linux toolset. If you work in Cyber Security, as a DevOps or a Linux Admin, you probably will use this command on a daily basis. I show you the most important commands.
To start with, we will first check the Linux cp command syntax and then look at the various possible options.
The basic syntax for the Linux cp command
To use this command, you would need to specify the source and the destination, where the source gets copied to the destination.
cp [options] source destination
Copy a file within the same directory to a new file with a new name
You can copy Sample.txt to Sample_copy.txt by using the below command:
cp Sample.txt Sample_copy.txt
Copy file from the current directory to another directory:
From your current directory you can copy files to another /tmp directory as shown below:
cp Sample.txt /tmp
Copy file to another directory with a new file name with the Linux cp command:
In the above example, Sample.txt is copied to another directory with the same name. However, the same command can be modified to copy a file to a new directory and give a new file name. One such example is as shown below:
cp Sample.txt /tmp/Sample_copy.txt
Here this Linux cp command creates a new file Sample_copy.txt with the same content as Sample.txt.
Linux cp options
There are multiple options available for the Linux cp command. You can check these options list from the manual pages. This can be done by using the below so called man command.
man cp
As part of the output you will see the complete list of manual entry for cp command as shown below:
In case you just want to view options, you can also use the below command:
cp --help
The output as shown below will provide all the relevant options for the Linux cp command.
Version for the cp command can be checked by using the below command:
cp --version
Few of the commonly used options are:
- -f This is used for force copy and removes destinations file in case the file already exists
- –i This is used for interactive copying. Prompt is displayed before overwriting.
- -n This option does not allow file overwrites
- -v This prints informative messages while copying
- -u This stands for update and copies only when the source file is newer than the destination
- -R This stands for a recursive copy. Also includes hidden files to be copied
- -s create a symbolic link for a file instead of copying.
- -l Link files instead of copying files
- -p Preserve file attributes while copying. This can preserve modification timestamp, access time, file mode, file flags, User ID and Group ID
- -b This option creates a backup of the file before copying.
In a single copy command, you can use multiple options. One such example is as shown below:
cp -vi /tmp/linux_cp /home/usr/linux_copy
Copy multiple files
Instead of one file, you can also allow multiple files to be copied to a specific location. In the below example, two files Sample.txt and Sample_copy.txt are copied to the location /tmp
cp -vi /tmp/linux_cp /home/usr/linux_copy
Similarly, you can add more files to copy into a specific directory location.
Copy multiple files with the same extension
The Linux cp command lets you quickly copy all files with the same extension using a single command. In case you need to copy all .sh files to a single directory /tmp, then you can use the command as shown below:
cp *.sh /tmp
Similarly, you can copy any other file extension. In case you have a pattern of files to be copied such as Sample_1.sh, Sample_22.sh, Sample_32.sh, then you can consolidate the copy into one command as shown below:
cp Sample*.sh /tmp
Single Directory copy
A single directory can be copied directly by using the cp command. In case you want to copy the test directory to /home/usr/linux_cp directory, then you can use the command as shown below:
cp test /home/usr/linux_cp
Copy all files from the current directory
To copy all files from the present working directory to a new directory, you can try:
cp * /home/usr/linux_cp
Recursive Copy of directories and sub-directories
To recursively copy all directories and files present in a directory, you need to use the -R option while using the cp command. In case you want to copy directory tmp to directory linux_cp then you can use the command as shown below:
cp -R tmp linux_cp
In case you need to point to a different directory location, then you can also provide the absolute path of the directory in the above Linux cp command.
Copy all files from one directory to another directory
To copy all files from one directory to another directory, you would need to use -R option to recursively copy all files. An example of such a copy command is as shown below:
cp -R /tmp/linux_cp/* /home/usr/linux_copy
This Linux cp command will copy all files and directories from /tmp/linux_cp to /home/usr/linux_copy
Conclusion
The Linux copy commands are widely used while working on the shell. The Linux cp command can be used to rapidly copy large directories, files and retain the complete directory structure. On top of this, the Linux cp commands support multiple options as you have seen above. Knowing the Linux cp command is an essential skill in your Linux reportoire.