This tutorial explains Linux “cp” command, options and its usage with examples.
Usage:
cp [OPTION]… [-T] SOURCE DEST
cp [OPTION]… SOURCE… DIRECTORY
To copy files and directories use the cp command under Linux, UNIX, and BSD like operating systems.
Here’s the listing of example usage of cp command. :
1. Copy a file or directory from source to destination(cp source_path destination_path):
sanfoundry-> touch 1.txt sanfoundry-> mkdir sample_folder sanfoundry-> cp 1.txt sample_folder/ sanfoundry-> cd sample_folder/ sanfoundry-> ls 1.txt
Here as you can see that file 1.txt is copied from with the same name in folder sample_folder.
If you want to copy the file in another file name, you can mention the name of the file in the destination path.
sanfoundry-> cp 1.txt sample_folder/1_copy.txt sanfoundry-> cd sample_folder/ sanfoundry-> ls 1_copy.txt
2. A directory (and all its content) can be copied from source to destination with the recursive option -r(cp -r source destination):
It allows directories including all of their contents to be copied:
sanfoundry-> ls sample_folder1 x y z sanfoundry-> cp -r sample_folder1/ sample_folder2/ sanfoundry-> ls sample_folder2/ sample_folder1
3. Copy multiple files or directories (cp file1 file2 source destination):
sanfoundry-> cp 1.txt 2.txt sample_folder/ sanfoundry-> ls sample_folder/ 1.txt 2.txt
To copy directories use -r option.
sanfoundry-> ls final/ sanfoundry-> ls | grep sample sample1 sample2 sample3 sanfoundry-> cp -r sample* final/ sanfoundry-> ls final/ sample1 sample2 sample3
From the above example, you can see that all the folder named sample* are copied in a folder final in a single step.
4. To avoid overwriting the the existing file(cp -n source destination):
If you want to copy only when the destination file doesn’t exist, use option “-n”.This won’t overwrite the existing file.
sanfoundry-> cat sample/1.txt Hi i am in sample folder sanfoundry-> cat 1.txt Hi i am x sanfoundry-> cp -n 1.txt sample/1.txt sanfoundry-> cat sample/1.txt Hi i am in sample folder
Here as we can see that after copying file 1.txt in sample folder the original file 1.txt in sample folder is not changed since of “-n” option.
5. Confirm before overwriting in a interactive mode(cp -i source destination):
sanfoundry-> cp -i 1.txt sample/1.txt cp: overwrite `sample/1.txt'? y sanfoundry-> cat sample/1.txt Hi i am x
6. To make a backup of files if copying file has the same name(cp –backup source destination ):
sanfoundry-> cat 1.txt Hi I am x sanfoundry-> cat sample/1.txt Hi i am in folder sample sanfoundry-> cp --backup 1.txt sample/ sanfoundry-> ls sample 1.txt 1.txt~ sanfoundry-> cat sample/1.txt Hi I am x sanfoundry-> cat sample/1.txt~ Hi i am in folder sample
As you can see here that when 1.txt is copied from source to destination in sample folder, a backup of the original file 1.txt in the sample folder is made as 1.txt~ and new file is copied in the 1.txt as usual.
7. To show copying details in a verbose manner(cp -v source destination):
sanfoundry-> cp -v 2.txt sample `2.txt' -> `sample/2.txt'
8. To preserve the file permission and other attributes(cp -p source destination):
You need to pass the -p option to save File modification time, access time, modes, flags of the files.
if you copy withoy “-p” option.
sanfoundry-> ls -l /etc/shells -rw-r--r-- 1 root root 165 Oct 12 2011 /etc/shells sanfoundry-> cp /etc/shells . sanfoundry-> ls -l shells -rw-r--r-- 1 himanshu himanshu 165 Jun 18 23:50 shells
If you apply “-p” option while copying ->
sanfoundry-> sudo cp -p /etc/shells . [sudo] password for himanshu: sanfoundry-> ls -l shells -rw-r--r-- 1 root root 165 Oct 12 2011 shells
9. To preserve the links while copying(cp -d source destination):
If you copy a file, which refers to other file as a soft link, then only copying the file will remove the link of the file and only copy the file. So to preserve the links use “-d” option while copying.
sanfoundry-> ls -l 1_link.txt lrwxrwxrwx 1 himanshu himanshu 5 Jun 18 23:58 1_link.txt -> 1.txt sanfoundry-> cp 1_link.txt sample/ sanfoundry-> ls -l sample/1_link.txt -rw-rw-r-- 1 himanshu himanshu 0 Jun 19 00:02 sample/1_link.txt sanfoundry-> cp -d 1_link.txt sample/ sanfoundry-> ls -l sample/1_link.txt lrwxrwxrwx 1 himanshu himanshu 5 Jun 19 00:02 sample/1_link.txt -> 1.txt
As you can see from the above example, that after adding “-d” option a link is preserved while copying.
10. To create hard link to a file(cp -l source destination):
You can create a hard link to a file, instead of copying it.
sanfoundry-> ls -li 1.txt 585339 -rw-rw-r-- 1 himanshu himanshu 0 Jun 19 00:05 1.txt sanfoundry-> cp -l 1.txt sample/ sanfoundry-> ls -li sample/1.txt 585339 -rw-rw-r-- 2 himanshu himanshu 0 Jun 19 00:05 sample/1.txt
As you can see, that after adding “-l”option in cp the innode, 585339 is same for the file i.e a hard link is created.
Note
To create a soft link while copying Use “-s” option.
cp -s file1 file2
Here, soft link for file1 is created as file2.