Introduction

Performing file copy operations between servers is a common task for any system administrator or a generic Linux operating system user. While copying files from one system to another we might need to exclude certain files and directories from being copied due to some specific reason. This could be applicable even when we are transferring data from one location to another on the same system. In this article we will demonstrate how you can exclude certain files or directories or being copied using the three most common and widely used utilities employed for this purpose i.e. rsync, cp and scp. In an earlier article, we discussed the rsync command in depth with a lot of examples.

Exclude specific Files/Directories from being copied using cp command:
Consider the following scenario wherein I have five directories in my current working directory.

[root@linuxnix tmp]# ls -ld dir*
drwxr-xr-x 2 root root 6 Aug 29 22:47 dir1
drwxr-xr-x 2 root root 71 Aug 29 22:47 dir2
drwxr-xr-x 2 root root 6 Aug 29 22:47 dir3
drwxr-xr-x 2 root root 6 Aug 29 22:47 dir4
drwxr-xr-x 2 root root 6 Aug 29 22:47 dir5

I would like to copy the content of all directories starting with the name dir except the dir2 directory then I could do the following:

[root@linuxnix tmp]# cp -r `ls -A | grep dir| grep -v "dir2"` /tmp/sahil/

This copies over all the dir directories except dir2. If we need to copy all directories and skip a single directory then we could run the following command.

[root@linuxnix tmp]# cp -r !(dir2) /sahil

Here all directories and sub-directories in the current working directory are copied to /sahil except the directory dir2. Similarly if we wanted to copy all files from the current working directory except one file, we type the following

[root@linuxnix dir2]# cp -r !(file3) /sahil

In the above example all files in the current working directory are copied to /sahil except the file named file3.

Exclude specific Files/Directories from being copied using scp command:
The data exclusion mechanism in scp works simimlar as earlier demonstrated with the cp command. Given below is an example.

[root@linuxnix dir2]# scp -rp !(file4) 192.168.19.142:/sahil
file1 100% 0 0.0KB/s 00:00
file2 100% 0 0.0KB/s 00:00
file3 100% 0 0.0KB/s 00:00
file5 100% 0 0.0KB/s 00:00
[root@linuxnix dir2]#

The above command copied all files from within the current working directory except the file named file4.

Exclude specific Files/Directories from being copied using rsync command:
The most flexible from among the set of commands being used to copy files is the rsync command. As we’ve already covered the rsync command in depth in a previous article, in this article, we will limit our discussion to the capability to exclude certain files/directories from being copied. To exclude a file or folder from being copied using the rysnc command we use the –exclude flag as shown in the below example.

[root@linuxnix tmp]# rsync -av --progress --exclude="dir2" dir* /sahil
sending incremental file list
dir1/
dir3/
dir4/
dir5/

sent 82 bytes received 28 bytes 220.00 bytes/sec
total size is 0 speedup is 0.00
[root@linuxnix tmp]# cd /sahil/
[root@linuxnix sahil]# ls -l
total 0
drwxr-xr-x 2 root root 6 Aug 29 22:47 dir1
drwxr-xr-x 2 root root 6 Aug 29 22:47 dir3
drwxr-xr-x 2 root root 6 Aug 29 22:47 dir4
drwxr-xr-x 2 root root 6 Aug 29 22:47 dir5
[root@linuxnix sahil]#

We could use the –exclude flag while copying files remotely as well. Given below is an example.

[root@linuxnix tmp]# rsync -av --progress --exclude="dir2" dir* 192.168.19.142:/sahil
sending incremental file list
dir1/
dir3/
dir4/
dir5/

sent 82 bytes received 28 bytes 220.00 bytes/sec
total size is 0 speedup is 0.00
[root@linuxnix tmp]#


Conclusion

This concludes our demonstration of how you could exclude certain content from being copied while using the cp, scp and rsync commands. We hope that you’ve found this article to be useful and we look forward towards your suggestions and feedback.

The following two tabs change content below.

Sahil Suri

He started his career in IT in 2011 as a system administrator. He has since worked with HP-UX, Solaris and Linux operating systems along with exposure to high availability and virtualization solutions. He has a keen interest in shell, Python and Perl scripting and is learning the ropes on AWS cloud, DevOps tools, and methodologies. He enjoys sharing the knowledge he's gained over the years with the rest of the community.