Learn all about cp command
cp - copy files and directories
Copy SOURCE to DESTINATION, or multiple SOURCE(s) to DIRECTORY.
root:~# man cp
1.1 Copy One File to another Location
root:~# cat temp.txt Hello World ! How are you? root:~# cp temp.txt /tmp/ root:~# ls -l temp.txt /tmp/temp.txt -rw-r--r-- 1 root root 27 Jul 23 10:34 /tmp/temp.txt -rw-r--r-- 1 root root 27 Jul 23 10:33 temp.txt root:~# cat /tmp/temp.txt Hello World ! How are you?
root:~# cp file1.txt file2.txt file3.txt /tmp/ root:~# ls /tmp/ file1.txt file2.txt file3.txt
root:~# ls directory_1/ file1.txt file2.txt file3.txt root:~# cp -r directory_1 /tmp/ root:~# ls /tmp/directory_1/
2.1 Create hard link to file
root:~# cp -l file1.txt temp1.txt root:~# ls -liv file1.txt temp1.txt 183085 -rw-r--r-- 2 root root 27 Jul 23 10:37 file1.txt 183085 -rw-r--r-- 2 root root 27 Jul 23 10:37 temp1.txt
root:~# cp -s file2.txt temp2.txt root:~# ls -l temp2.txt file2.txt -rw-r--r-- 1 root root 27 Jul 23 10:37 file2.txt lrwxrwxrwx 1 root root 9 Jul 23 11:18 temp2.txt -> file2.txtHere, 'l' in lrwxrwxrwx denotes its symbolic link.
root:~# ls -l temp2.txt file2.txt -rw-r--r-- 1 root root 27 Jul 23 10:37 file2.txt lrwxrwxrwx 1 root root 9 Jul 23 11:18 temp2.txt -> file2.txt root:~# cp -P temp2.txt /tmp/ root:~# ls -l /tmp/temp2.txt lrwxrwxrwx 1 root root 9 Jul 23 11:21 /tmp/temp2.txt -> file2.txt root:~# cat /tmp/temp2.txt cat: /tmp/temp2.txt: No such file or directory2.4 Copy with following symbolic links in Source
root:~# cp -L file3.txt /tmp/ root:~# ls -l /tmp/file3.txt -rw-r--r-- 1 root root 27 Jul 23 11:32 /tmp/file3.txt root:~# cat /tmp/file3.txt Hello World ! How are you?
3.1 Copy directory recursively (-r or -R)
root:~# ls -l /tmp/abc total 16 -rwxrwxrwx 1 root root 6 Aug 10 13:18 1.txt -rw-r--r-- 2 root root 6 Aug 10 13:18 2.txt -rw-r--r-- 1 root root 6 Aug 10 13:18 3.txt -rw-r--r-- 2 root root 6 Aug 10 13:18 hard.txt lrwxrwxrwx 1 root root 5 Aug 10 13:20 soft.txt -> 3.txt root:~# cp -R /tmp/abc /tmp/xyz root:~# ls -l /tmp/abc /tmp/xyz /tmp/abc: total 16 -rwxrwxrwx 1 root root 6 Aug 10 13:18 1.txt -rw-r--r-- 2 root root 6 Aug 10 13:18 2.txt -rw-r--r-- 1 root root 6 Aug 10 13:18 3.txt -rw-r--r-- 2 root root 6 Aug 10 13:18 hard.txt lrwxrwxrwx 1 root root 5 Aug 10 13:20 soft.txt -> 3.txt /tmp/xyz: total 16 -rwxr-xr-x 1 root root 6 Aug 10 13:26 1.txt -rw-r--r-- 1 root root 6 Aug 10 13:26 2.txt -rw-r--r-- 1 root root 6 Aug 10 13:26 3.txt -rw-r--r-- 1 root root 6 Aug 10 13:26 hard.txt lrwxrwxrwx 1 root root 5 Aug 10 13:26 soft.txt -> 3.txtHere, if you notice then permission of files and last modification times are not preserved.
root:~# cp -a /tmp/abc /tmp/mno root:~# ls -l /tmp/abc /tmp/mno /tmp/abc: total 16 -rwxrwxrwx 1 root root 6 Aug 10 13:18 1.txt -rw-r--r-- 2 root root 6 Aug 10 13:18 2.txt -rw-r--r-- 1 root root 6 Aug 10 13:18 3.txt -rw-r--r-- 2 root root 6 Aug 10 13:18 hard.txt lrwxrwxrwx 1 root root 5 Aug 10 13:20 soft.txt -> 3.txt /tmp/mno: total 16 -rwxrwxrwx 1 root root 6 Aug 10 13:18 1.txt -rw-r--r-- 2 root root 6 Aug 10 13:18 2.txt -rw-r--r-- 1 root root 6 Aug 10 13:18 3.txt -rw-r--r-- 2 root root 6 Aug 10 13:18 hard.txt lrwxrwxrwx 1 root root 5 Aug 10 13:20 soft.txt -> 3.txtHere, file time and permissions are preserved.
4.1 Verbose Mode
root:~# mkdir xyz root:~# cp -v /tmp/abc/*.txt xyz/ '/tmp/abc/1.txt' -> 'xyz/1.txt' '/tmp/abc/2.txt' -> 'xyz/2.txt' '/tmp/abc/3.txt' -> 'xyz/3.txt' '/tmp/abc/hard.txt' -> 'xyz/hard.txt' '/tmp/abc/soft.txt' -> 'xyz/soft.txt'
root:~# mkdir /tmp/xyz_1 root:~# cd /tmp/abc root:/tmp/abc# cp --backup=simple -v *.txt ../xyz_1/ '1.txt' -> '../xyz_1/1.txt' (backup: '../xyz_1/1.txt~') '2.txt' -> '../xyz_1/2.txt' (backup: '../xyz_1/2.txt~') '3.txt' -> '../xyz_1/3.txt' (backup: '../xyz_1/3.txt~') 'hard.txt' -> '../xyz_1/hard.txt' (backup: '../xyz_1/hard.txt~') 'soft.txt' -> '../xyz_1/soft.txt' (backup: '../xyz_1/soft.txt~') root:~# cd - root:~# ls -l /tmp/xyz_1/ total 36 -rwxr-xr-x 1 root root 6 Aug 10 16:40 1.txt -rwxr-xr-x 1 root root 6 Aug 10 13:43 1.txt~ -rw-r--r-- 1 root root 6 Aug 10 16:40 2.txt -rw-r--r-- 1 root root 6 Aug 10 13:43 2.txt~ -rw-r--r-- 1 root root 6 Aug 10 16:40 3.txt -rw-r--r-- 1 root root 6 Aug 10 13:43 3.txt~ -rw-r--r-- 1 root root 6 Aug 10 16:40 hard.txt -rw-r--r-- 1 root root 6 Aug 10 13:43 hard.txt~ -rw-r--r-- 1 root root 6 Aug 10 16:40 soft.txt lrwxrwxrwx 1 root root 5 Aug 10 13:43 soft.txt~ -> 3.txt