Let’s say you have a requirement to copy the contents of folder to another but only the files which AREN’T present in the destination folder. Use the cp command with ‘-aru’ option. You got to notice certain things while doing this; it is explained with the below example here which is self-explanatory.
In this example, you got to just notice the time-stamp of each files created under the folder /dir1 & /dir2.
[root@host01 ~]# mkdir /dir1 /dir2
[root@host01 ~]# cd /dir1
[root@host01 dir1]#
[root@host01 dir1]# touch a b c d e f; mkdir d1 d2
[root@host01 dir1]# touch d1/file1 d1/file2
[root@host01 dir1]# touch d2/take1 d2/take2
[root@host01 dir1]# ls -lR /dir1 ß Using -lR option to list the sub-folder contents of /dir1
/dir1:
total 8
-rw-r--r-- 1 root root 0 Dec 9 12:18 a
-rw-r--r-- 1 root root 0 Dec 9 12:18 b
-rw-r--r-- 1 root root 0 Dec 9 12:18 c
-rw-r--r-- 1 root root 0 Dec 9 12:18 d
drwxr-xr-x 2 root root 4096 Dec 9 12:18 d1
drwxr-xr-x 2 root root 4096 Dec 9 12:19 d2
-rw-r--r-- 1 root root 0 Dec 9 12:18 e
-rw-r--r-- 1 root root 0 Dec 9 12:18 f
/dir1/d1:
total 0
-rw-r--r-- 1 root root 0 Dec 9 12:18 file1
-rw-r--r-- 1 root root 0 Dec 9 12:18 file2
/dir1/d2:
total 0
-rw-r--r-- 1 root root 0 Dec 9 12:19 take1
-rw-r--r-- 1 root root 0 Dec 9 12:19 take2
[root@host01 dir1]#
[root@host01 dir1]# cd /dir2
[root@host01 dir2]# touch b d f Z; mkdir d1
[root@host01 dir2]# touch d1/key1 d1/key2
[root@host01 dir2]# ls -lR /dir2
/dir2:
total 4
-rw-r--r-- 1 root root 0 Dec 9 12:20 b
-rw-r--r-- 1 root root 0 Dec 9 12:20 d
drwxr-xr-x 2 root root 4096 Dec 9 12:20 d1
-rw-r--r-- 1 root root 0 Dec 9 12:20 f
-rw-r--r-- 1 root root 0 Dec 9 12:20 Z
/dir2/d1:
total 0
-rw-r--r-- 1 root root 0 Dec 9 12:20 key1
-rw-r--r-- 1 root root 0 Dec 9 12:20 key2
[root@host01 dir2]# cp -aru /dir1/* /dir2
[root@host01 dir2]# ls -lR /dir2
/dir2:
total 8
-rw-r--r-- 1 root root 0 Dec 9 12:18 a
-rw-r--r-- 1 root root 0 Dec 9 12:20 b
-rw-r--r-- 1 root root 0 Dec 9 12:18 c
-rw-r--r-- 1 root root 0 Dec 9 12:20 d
drwxr-xr-x 2 root root 4096 Dec 9 12:18 d1
drwxr-xr-x 2 root root 4096 Dec 9 12:19 d2
-rw-r--r-- 1 root root 0 Dec 9 12:18 e
-rw-r--r-- 1 root root 0 Dec 9 12:20 f
-rw-r--r-- 1 root root 0 Dec 9 12:20 Z
/dir2/d1:
total 0
-rw-r--r-- 1 root root 0 Dec 9 12:18 file1
-rw-r--r-- 1 root root 0 Dec 9 12:18 file2
-rw-r--r-- 1 root root 0 Dec 9 12:20 key1
-rw-r--r-- 1 root root 0 Dec 9 12:20 key2
/dir2/d2:
total 0
-rw-r--r-- 1 root root 0 Dec 9 12:19 take1
-rw-r--r-- 1 root root 0 Dec 9 12:19 take2
[root@host01 dir2]#
Conclusion on executing cp command with -aru option:
1. Files with same names (b, d & f ) were left un-touched. You can confirm it by timestamps of those files (12.20).
2. It copied all the missing files (a , c & e ) and missing folder (d2) on to the destination folder /dir2.
3. The file ‘Z’ which present only on /dir2 remains same. It haven’t got deleted
4. Contents of folder “d1” which is present both in source and destination folder is retained, however it copied the files (key1 & key2) which are present in /dir1 folder. So it didn’t replace the d1 folder on destination.
Usage: Use this option when incase the copy which you initiated before got interrupted for some reason. Using this you need not to copy it over again by typing “yes” for over-writing the existing files which are copied before.