Linux: How to Exclude Files While Using Scp or Rsync command

This post will guide you how to exclude files when using scp or rsync command to copy directory recursively in Linux system. How to filter files when using scp or rsync command Under Linux bash shell.

If you want to copy all files from your current system called osetc1.com to the remote system called osetc2.com, but excluding all *.bak files while using scp or rsync command under bash shell.

Using SCP Command to Exclude Files


the scp commmand can not filter or exclude the specific files when copying files in your directory to remote directory. You can enable shoopt extglob firstly, then using scp command to exclude files. Like the following command:

$ shopt -s extglob
$ scp !(*.bak) root@osetc2.com:/root

Using Rsync command to Exclude Files


You can also use rsync command to exclude files while copying files from local system to remote server. Type the following command:

$ rsync -av -e ssh --exclude='*.bak' ./ root@osetc2:/root

Advanced Useage For Rsync Command

The syntax for requesting multiple files from a remote host is done by specifying additional remote-host args in the same style as the first, or with the hostname omitted. For
instance, all these work:

rsync -av host:file1 :file2 host:file{3,4} /dest/
rsync -av host::modname/file{1,2} host::modname/file3 /dest/
rsync -av host::modname/file1 ::modname/file{3,4}

Older versions of rsync required using quoted spaces in the SRC, like these examples:

rsync -av host:'dir1/file1 dir2/file2' /dest
rsync host::'modname/dir1/file1 modname/dir2/file2' /dest

This word-splitting still works (by default) in the latest rsync, but is not as easy to use as the first method.

If you need to transfer a filename that contains whitespace, you can either specify the –protect-args (-s) option, or you’ll need to escape the whitespace in a way that the remote
shell will understand. For instance:

rsync -av host:'file\ name\ with\ spaces' /dest

More Info For Scp or Rsync Command


You can use the following command to get more help for those two commands:

$ rsync --help

$ scp --help

 

You might also like:

Sidebar



back to top