Operations 6 min read

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.

ITPUB
ITPUB
ITPUB
How to Exclude Files and Directories When Copying with cp, scp, and rsync

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 dir5

To 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) /sahil

Using 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

rsync

offers 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.00

Conclusion

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.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Linuxrsyncscpcpfile copyexclude
ITPUB
Written by

ITPUB

Official ITPUB account sharing technical insights, community news, and exciting events.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.