Home Command line utilities How To Exclude Specific Directories From Copying In Linux

How To Exclude Specific Directories From Copying In Linux

By sk
Published: Last Updated on 46.8K views

A while ago, we discussed how to exclude certain size files from copying using rsync in Unix-like systems. That method helped us to 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 free time. Today, in this brief guide, we will see how to exclude specific directories from copying using Rsync and cp commands in Linux.

Exclude Specific Directories From Copying In Linux

There could be many ways to do this task. I am aware of following two methods.

Method 1- 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 dir1dir2, and dir3.

$ ls ostechnix/
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
 6,152,611 100% 58.36MB/s 0:00:00 (xfr#1, to-chk=7/11)
 2,597,261 100% 14.83MB/s 0:00:00 (xfr#2, to-chk=6/11)
 88,143 100% 470.37kB/s 0:00:00 (xfr#3, to-chk=5/11)
 66,374 100% 330.71kB/s 0:00:00 (xfr#4, to-chk=4/11)
 6,152,611 100% 18.39MB/s 0:00:00 (xfr#5, to-chk=3/11)
 2,597,261 100% 6.68MB/s 0:00:00 (xfr#6, to-chk=2/11)
 88,143 100% 224.16kB/s 0:00:00 (xfr#7, to-chk=1/11)
 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.

For more details, check man pages.

$ man rsync

Also read:

Method 2 - 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.

$ cd ostechnix/

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/

For more details, check man pages.

$ man cp

Thanks for stopping by!

Help us to help you:

Have a Good day!!

You May Also Like

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. By using this site, we will assume that you're OK with it. Accept Read More