How to Copy Only New / Modified / Changed Files in Unix / Linux?


There are two commands to perform this task – rsync and cp command with –u option. In both cases it will copy all the files in the initial run and only modified file in the subsequent run.


Option 1 Using rsync
Using rsync you can perform the copy within server and across servers also.

rsync Example for within 2 directories

rsync -av /home/oracle/source /home/oracle/target
building file list ... done
source/
source/.swo
source/.swp
source/ls.txt
source/vi
source/mod/
source/mod/a
source/mod/sample

sent 45060 bytes  received 164 bytes  90448.00 bytes/sec
total size is 44586  speedup is 0.99


$ cd /home/oracle/source/mod
[oracle@dev01Infor01 mod]$ ls -ltr
total 0
-rw-r--r-- 1 oracle oinstall 0 Nov 21 07:00 a
-rw-r--r-- 1 oracle oinstall 0 Nov 21 07:00 sample

I am going to change the file a using touch command.
[oracle@dev01Infor01 mod]$ touch a
[oracle@dev01Infor01 mod]$ ls -ltr
total 0
-rw-r--r-- 1 oracle oinstall 0 Nov 21 07:00 sample
-rw-r--r-- 1 oracle oinstall 0 Nov 21 07:02 a

Again I am running the rsync command which will copy only the file a.
[oracle@dev01Infor01 mod]$ rsync -av /home/oracle/source /home/oracle/target
building file list ... done
source/mod/a

sent 236 bytes  received 42 bytes  556.00 bytes/sec
total size is 44586  speedup is 160.38

Example for rsync across 2 servers
Syntax: rsync -av source_dir target_user@target_host:target_dir

$ rsync -av /home/oracle/source oracle@xx.xx.xxx.xxx:/home/oracle/target
The authenticity of host 'xx.xx.xxx.xxx (xx.xx.xxx.xxx)' can't be established.
RSA key fingerprint is 02:c0:84:10:84:c3:f4:96:17:47:9a:e7:65:4c:72:30.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'xx.xx.xxx.xxx' (RSA) to the list of known hosts.
oracle@xx.xx.xxx.xxx's password:
building file list ... done
source/
source/.swo
source/.swp
source/ls.txt
source/vi
source/mod/
source/mod/a
source/mod/sample

sent 45060 bytes  received 164 bytes  3349.93 bytes/sec
total size is 44586  speedup is 0.99

$ touch ls.txt
$ date
Wed Nov 21 09:13:48 EST 2012
$ ls -ltr
total 28
-rw-r--r-- 1 oracle oinstall  9703 Nov  4 01:14 vi
drwxr-xr-x 2 oracle oinstall  4096 Nov 21 07:00 mod
-rw-r--r-- 1 oracle oinstall 10307 Nov 21 09:13 ls.txt
$ rsync -av /home/oracle/source oracle@10.12.101.124:/home/oracle/target
oracle@10.12.101.124's password:
building file list ... done
source/ls.txt

sent 296 bytes  received 132 bytes  37.22 bytes/sec

OPTION 2 Using cp -u Command
The cp command with –u option will copy only files which are modified or new. See some examples below.

First run

$ cp -ur /home/oracle/source/* /home/oracle/target/
$ cd /home/oracle/source/
$ ls -ltr
total 28
-rw-r--r-- 1 oracle oinstall  9703 Nov  4 01:14 vi
-rw-r--r-- 1 oracle oinstall 10307 Nov  4 01:40 ls.txt
drwxr-xr-x 2 oracle oinstall  4096 Nov 21 07:00 mod

Modifying the file ls.txt and in the subsequent run it will copy only modified file.
$ touch ls.txt
$ ls -ltr
total 28
-rw-r--r-- 1 oracle oinstall  9703 Nov  4 01:14 vi
drwxr-xr-x 2 oracle oinstall  4096 Nov 21 07:00 mod
-rw-r--r-- 1 oracle oinstall 10307 Nov 21 08:17 ls.txt
$ cp -ur /home/oracle/source/* /home/oracle/target/
$ cd /home/oracle/target/
$ ls -ltr
total 28
-rw-r--r-- 1 oracle oinstall  9703 Nov 21 07:50 vi
drwxr-xr-x 2 oracle oinstall  4096 Nov 21 07:51 mod
-rw-r--r-- 1 oracle oinstall 10307 Nov 21 08:17 ls.txt
DBA Tips Data Pump Reference

0 comments:

Post a Comment

 

dba topics. Copyright 2011-16 All Rights Reserved | Site Map | Contact | Disclaimer | Google