Wednesday, May 31, 2017

How To Exclude Specific Directories From Copying In Linux

How To Exclude Specific Directories From Copying In Linux

As you may already know, Rsync is a fast and versatile tool to copy files and folders locally, or to/from another host over any remote shell, or to/from a remote rsync daemon. Rsync has lot useful options. One of the useful option is –exclude. Using exclude option, we can exclude certain files/directories from copying. Let us see how to do it with an example.
Let us say you have a folder called ostechnix. Inside the directory ostechnix, there are three sub-directories namely dir1dir2, and dir3.
ls osnix/
Sample output:
dir1 dir2 dir3
You want to copy dir1 and dir3 to different location, but not dir2. How? Here is where rsync comes in help.
To exclude a specific directory from copying using rsync command, just run:
rsync -av --progress osnix/ backup/ --exclude dir2
Sample output would be:
sending incremental file list
./
dir1/
dir1/file1.pdf
 6,152,611 100% 58.36MB/s 0:00:00 (xfr#1, to-chk=7/11)
dir1/file2
 2,597,261 100% 14.83MB/s 0:00:00 (xfr#2, to-chk=6/11)
dir1/file3.doc
 88,143 100% 470.37kB/s 0:00:00 (xfr#3, to-chk=5/11)
dir1/file4.txt
 66,374 100% 330.71kB/s 0:00:00 (xfr#4, to-chk=4/11)
dir3/
dir3/file1.pdf
 6,152,611 100% 18.39MB/s 0:00:00 (xfr#5, to-chk=3/11)
dir3/file2
 2,597,261 100% 6.68MB/s 0:00:00 (xfr#6, to-chk=2/11)
dir3/file3.doc
 88,143 100% 224.16kB/s 0:00:00 (xfr#7, to-chk=1/11)
dir3/file4.txt
 66,374 100% 164.10kB/s 0:00:00 (xfr#8, to-chk=0/11)

sent 17,813,759 bytes received 187 bytes 11,875,964.00 bytes/sec
total size is 17,808,778 speedup is 1.00
The above command will copy all contents of the folder ostechnix to a folder namely backup, and it will exclude the directory dir2 from copying.
Alternatively, you can use the same command as below.
rsync -av --progress --exclude="folder_to_exclude" source_dirctory dest_directory
Example:
rsync -av --progress --exclude="dir2" ostechnix/ backup/
It is actually a good idea to omit or exclude some big or less important directories from copying while backup your system using rsync in order to save your time.

No comments: