How to exclude Linux copying of specific directories using cp / scp / rsync

Introduction

Performing file copy operations between servers is a standard activity for any system administrator or a generic Linux working system person. Whereas copying information from one system to a different we’d have to exclude sure information and directories from being copied resulting from some particular purpose. This may very well be relevant even once we are transferring knowledge from one location to a different on the identical system. On this article we are going to reveal how one can exclude sure information or directories or being copied utilizing the three most typical and extensively used utilities employed for this goal i.e. rsync, cp and scp. In an earlier article, we mentioned the rsync command in depth with loads of examples.

Exclude particular Information/Directories from being copied utilizing cp command:
Contemplate the next situation whereby I’ve 5 directories in my present working listing.

[[email protected] 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 wish to copy the content material of all directories beginning with the identify dir besides the dir2 listing then I may do the next:

[[email protected] tmp]# cp -r `ls -A | grep dir| grep -v “dir2″` /tmp/sahil/

This copies over all of the dir directories besides dir2. If we have to copy all directories and skip a single listing then we may run the next command.

[[email protected] tmp]# cp -r !(dir2) /sahil

Right here all directories and sub-directories within the present working listing are copied to /sahil besides the listing dir2. Equally if we wished to repeat all information from the present working listing besides one file, we kind the next

[[email protected] dir2]# cp -r !(file3) /sahil

Within the above instance all information within the present working listing are copied to /sahil besides the file named file3.

Exclude particular Information/Directories from being copied utilizing scp command:
The information exclusion mechanism in scp works simimlar as earlier demonstrated with the cp command. Given beneath is an instance.

[[email protected] dir2]# scp -rp !(file4) 192.168.19.142:/sahil
file1 100% Zero 0.0KB/s 00:00
file2 100% Zero 0.0KB/s 00:00
file3 100% Zero 0.0KB/s 00:00
file5 100% Zero 0.0KB/s 00:00
[[email protected] dir2]#

The above command copied all information from inside the present working listing besides the file named file4.

How to exclude Linux copying of specific directories using cp / scp / rsync

Exclude particular Information/Directories from being copied utilizing rsync command:
Essentially the most versatile from among the many set of instructions getting used to repeat information is the rsync command. As we’ve already coated the rsync command in depth in a earlier article, on this article, we are going to restrict our dialogue to the potential to exclude sure information/directories from being copied. To exclude a file or folder from being copied utilizing the rysnc command we use the –exclude flag as proven within the beneath instance.

[[email protected] tmp]# rsync -av –progress –exclude=”dir2″ dir* /sahil
sending incremental file record
dir1/
dir3/
dir4/
dir5/

despatched 82 bytes acquired 28 bytes 220.00 bytes/sec
whole measurement is Zero speedup is 0.00
[[email protected] tmp]# cd /sahil/
[[email protected] sahil]# ls -l
whole 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
[[email protected] sahil]#

We may use the –exclude flag whereas copying information remotely as effectively. Given beneath is an instance.

[[email protected] tmp]# rsync -av –progress –exclude=”dir2″ dir* 192.168.19.142:/sahil
sending incremental file record
dir1/
dir3/
dir4/
dir5/

despatched 82 bytes acquired 28 bytes 220.00 bytes/sec
whole measurement is Zero speedup is 0.00
[[email protected] tmp]#

Conclusion

This concludes our demonstration of how you possibly can exclude sure content material from being copied whereas utilizing the cp, scp and rsync instructions. We hope that you just’ve discovered this text to be helpful and we glance ahead in direction of your ideas and suggestions.

Submit Views:
8,304

The next two tabs change content material beneath.

How to exclude Linux copying of specific directories using cp / scp / rsync

He began his profession in IT in 2011 as a system administrator. He has since labored with HP-UX, Solaris and Linux working techniques together with publicity to excessive availability and virtualization options.
He has a eager curiosity in shell, Python and Perl scripting and is studying the ropes on AWS cloud, DevOps instruments, and methodologies. He enjoys sharing the data he is gained over time with the remainder of the group.

How to exclude Linux copying of specific directories using cp / scp / rsync

cp exclude multiple directories,rsync exclude all except,how to copy all files except one directory in linux,rsync exclude not working,scp exclude multiple files,rsync include exclude,rsync –exclude directory,aix cp exclude directory