How To Exclude Specific Directories From Copying In Linux
A while ago, we discussed how to exclude certain size files from copying using rsync in Unix-like systems. That method helped us to simply exclude the big sized files or less important files in order to complete the copy process as soon as possible. So, we can copy the remaining files later when we have more free time. Today, in this brief guide, we will see how to exclude specific directories from copying using rsync in Linux. There could be many ways, but these are the only methods I am aware of now.
Exclude Specific Directories From Copying In Linux using rsync command
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 dir1, dir2, and dir3.
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 ostechnix/ 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
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.
- How To Quickly Transfer Large Files Over Network In Linux And Unix
- How to find the size of a directory in Linux from command line
Exclude Specific Directories From Copying In Linux using cp command
We can also use cp command to copy folders from one location to another excluding specific directories.
Go your source directory i.e ostechnix in our case.
And, run the following command:
cp -r `ls -A | grep -v "dir2"` /home/sk/backup/
The above command will copy all contents of the current folder ostechnix except the sub-directory dir2 and saves them to /home/sk/backup/ directory.
Even simpler, we can do this as follows.
cp -r !(dir2) /home/sk/backup/
And, that’s all. Hope this helps. If you find our articles useful, please share them on your social, professional networks and support OSTechNix.
Thanks for stopping by!
Help us to help you:
- Subscribe to our Email Newsletter : Sign Up Now
- Support OSTechNix : Donate Via PayPal
- Download free E-Books and Videos : OSTechNix on TradePub
- Connect with us: Facebook | Twitter | Google Plus | LinkedIn | RSS feeds
Have a Good day!!