How to recursively exclude files when using the scp command
Q: I need to copy all the folders containing the *. c file from the local notebook named hostA to hostB. I use the followingscp
Command, but do not know how to exclude a specific file (such as *. out ):
$ scp-r ~/projects/ user@hostB:/home/delta/projects/
How to tellscp
Does the command exclude a specific file or directory in the Linux/Unix Command Line?
People can usescp
Commands securely copy files between network hosts. It uses ssh for data transmission and authentication. The typical syntax is:
scp file1 user@host:/path/to/dest/
scp-r /path/to/source/ user@host:/path/to/dest/
Scp exclusion File
I don't think you can filter or exclude files when using the scp command. However, there is a good solution to exclude files and use ssh to securely copy them. This page describes how to usescp
Filter or exclude files when copying directories recursively.
How to Use the rsync command to exclude files
Syntax:
rsync av -e ssh--exclude='*.out'/path/to/source/ user@hostB:/path/to/dest/
Here:
-a
: Recursion to the directory, that is, copying all files and subdirectories. In addition, open the archive mode and all other options (equivalent-rlptgoD
)
-v
: Detailed output
-e ssh
: Use ssh as the remote shell so that everything is encrypted.
--exclude='*.out'
: Exclude matching files, such as *. out or *. c.
Rsync command example
In this example~/virt/
Recursively copy all files in the directory, but exclude all *. new files:
$ rsync -av -e ssh--exclude='*.new'~/virt/ root@CentOS7:/tmp
Sample output:
Scp exclude files but using rsync exclude command
If the remote server cannot be foundrsync
, Thenrsync
Command will fail. In this case, try the followingscp
Command, which matches with the bash shell pattern in the current directory (it cannot match-r
):
$ ls
Sample output:
centos71.log centos71.qcow2 centos71.qcow2.new centos71.v2.qcow2.new meta-data user-data
Copy all contents in the current directory except. new:
$ shopt -s extglob
$ scp!(*.new) root@centos7:/tmp/
Sample output:
centos71.log 100%42621.3MB/s 00:00
centos71.qcow2 100%836MB32.7MB/s 00:25
meta-data 100%4718.5KB/s 00:00
user-data 100%1543569.7KB/s 00:00
For more information, see the following manual page:
$ man rsync
$ manbash
$ manscp
How to exclude file when using scp command recursively