How to Exclude Files and Directories When Copying with cp, scp, and rsync
This guide shows Linux administrators how to copy files while excluding specific files or directories using three common utilities—cp, scp, and rsync—by demonstrating practical command‑line examples and the necessary syntax for selective transfers.
Copying files between Linux systems is a routine task for system administrators, but there are situations where certain files or directories must be omitted from the transfer. This article demonstrates three widely used utilities— cp, scp, and rsync —and shows how to apply exclusion patterns with each.
Using cp with shell expansion
Assume a working directory contains five directories named dir1 through dir5:
[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 dir5To copy all dir* directories except dir2:
[root@linuxnix tmp]# cp -r `ls -A | grep dir | grep -v "dir2"` /tmp/sahil/Alternatively, with Bash extglob enabled, a single pattern can be used: [root@linuxnix tmp]# cp -r !(dir2) /sahil The same syntax works for excluding a single file:
[root@linuxnix dir2]# cp -r !(file3) /sahilUsing scp for remote copies
The exclusion mechanism mirrors the cp approach. The example below copies everything from the current directory to a remote host except file4:
[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]#Using rsync with the --exclude flag
rsyncoffers the most flexible exclusion capabilities. To copy all directories matching dir* while skipping dir2 locally:
[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]#The same --exclude option works for remote destinations:
[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.00Conclusion
The examples above demonstrate practical ways to exclude specific files or directories when copying with cp, scp, and rsync. By using shell pattern matching or the built‑in --exclude flag, administrators can tailor file transfers to avoid unwanted data.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
ITPUB
Official ITPUB account sharing technical insights, community news, and exciting events.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
