Introduction to CP Command in Linux
Cp command is an abbreviated form of “Copy”.As the name depicts, we can replicate. The file or the directory in every environment like Linux, Unix, Windows, or Mac Operating systems. Copy Command(cp) is based on a command-line terminal used for copying files/directories. This command creates a replica of a file/directory with different/same file names on the disk.cp command usually takes at least two arguments as input. It is recently very useful to use the cp command to copy the files or directory while the rysnc command for copying the directories. Moving a file differ from copying a file from source to destination.
Users need to have permission to copy the file or directory from source to destination.
There are different types of usage of the cp command, listed below:
- cp source destination
- cp source Target_Directory
- cp source1 source2 source3 sourceN Target_Directory
- cp [options] source destinations
- cp [option] source directory
where the first, second syntax we will copy the source file to destination directories
However, from the third syntax, we can copy multiple source files to the target directory, and the fourth and fifth syntax is with options that we can use for different purposes.
- cp -a: This option is used to archive the existing files in the directory for retention purpose.
- cp -f: This option forcefully copy the files even it may remove the target file if needed. It is applicable if the file is already in use.
- cp -i: This option stands for interactive mode, which means that it will ask the user to overwrite the file by prompt.
- cp -l: This option is used to link a file with another existing one instead of copying it.
- cp -L: It is will create a symbolic link for the file.
- cp -n: This option is used not to overwrite any existing file.
- cp -R: This option means recursive copy means that it will copy all files with a cascading directory, including hidden file.
- cp -u: This means update, copy when the source file is new than the destination file.
- cp -v: This option stands for verbose which means that will it print all the process which happens on a file while copying.
Examples of CP Command in Linux
Let us discuss the understanding of the cp command with the help of examples.
1. Copying file to a target destination
Suppose we want to copy the /application/Praveen/file.txt file to /application/Praveen/Backup directory, so we need to run below:
cp /application/Praveen/file.txt /application/Praveen/Backup
The above output shows that the file has been copied to the target directory.
2. Multiple file copy at the same time
cp /application/Praveen/file1.txt /application/Praveen/file2.txt /application/Praveen/Backup/
3. Copying the file in interactive mode (-i)
If we want to copy the file in interactive mode, then we will use the option “-i”. Interactive Mode work if the same file already exists in the target directory.
cp -i /application/Praveen/file1.txt /application/Praveen/Backup/
Note: We have to manually type ‘y’ to start the copy operation.
4. Copying File with Verbose mode enabled
We will use the “-v” option for this.
cp -v /application/Praveen/file1.txt /application/Praveen/Backup/
If we want to use both verbose and interactive mode, then use the option “-iv.”
cp -iv /application/Praveen/file1.txt /application/Praveen/Backup/
5. Copying a folder or directory (-R or -r)
To copy a directory from one to another place, we will use -R or -r options
cp -r /application/Praveen/Testing /application/Praveen/Backup/
The above output shows the files and directory will be recursive.
Let us verify the content :
6. Archiving of files and directory
We will use the option “-a” instead of “-r” or “-R” options
cp -a /application/Praveen/Testing /application/Praveen/Backup/
7. Copying of file only when new source arrives
There are cases where we want to copy files only when the source file is newer than the target by using the option “-u”. We have added file2.txt at the source.
cp -v -u /application/Praveen/Testing/file*.txt /application/Praveen/Backup/Testing/
** the Only file2.txt has been updated in the target directory.
8. To avoid overwriting of an existing file
There may be a case where we do not want to overwrite the existing file in the target.
In this case, use the “-n” option
cp -n /application/Praveen/Testing/file*.txt /application/Praveen/Backup/Testing/
9. Creation of a symbolic link of the file
If we need to create a symbolic link instead of copying, we will use the “-s option.
cp -s /application/Praveen/Testing/file2.txt /application/Praveen/Backup/Testing/
In the above output, we can see that file2.txt colored and point to the link.
Also, the permission is “lrwxrwxrwx”, where l stands for a symbolic link.
10. Creation of a Hard link for a file
There are cases where we need to have a hard link instead of a symbolic link and copying of file so that we will achieve this by the option “-l”.
In Hard link inode, no. of source and link file will be the same.
cp -l /application/Praveen/Testing/file.txt /application/Praveen/Backup/Testing/
Target file inode :
Source file inode :
We can see that inode is “131081” for both source and target files.
11. Creation of backup file of existing destination
As we know, the default cp command overwrites the file if it exists. If we need to take the backup, then we have to use the “–backup” option, where we have to give the path at which backup to be taken.
cp --backup=simple -v /application/Praveen/Testing/file.txt /application/Praveen/Backup/Testing/file.txt
Output: It is created the same file with the extension(~) on the target path.
15. Copying files/directory forcefully(-f)
There are cases where the existing destination file cannot be opened and removed, and if we want to copy the file in place of the existing destination file, then the “-f” option can be used.
cp -f /application/Praveen/Testing/file.txt /application/Praveen/Backup/Testing/
This is a guide to CP Command in Linux. Here we have discussed the cp command and its different usage in the Linux ecosystem. You may also have a look at the following articles to learn more –