Home  >  Article  >  System Tutorial  >  SCP usage tips-recursively exclude files

SCP usage tips-recursively exclude files

WBOY
WBOYforward
2024-04-22 09:04:01580browse

SCP usage tips-recursively exclude files

One can use the scp command to securely copy files between network hosts. It uses ssh for data transfer and authentication. Typical syntax is:

scp file1 user@host:/path/to/dest/
scp -r /path/to/source/ user@host:/path/to/dest/
scp exclude files

I don't think you can filter or exclude files when using the scp command. However, there is a good workaround to exclude the file and copy it securely using ssh. This page explains how to filter or exclude files when copying a directory recursively using scp.

How to use the rsync command to exclude files

The syntax is:

rsync av -e ssh --exclude='*.out' /path/to/source/ user@hostB:/path/to/dest/

here:

  1. -a: Recurse into the directory, i.e. copy all files and subdirectories. Also, turns on archive mode and all other options (equivalent to -rlptgoD)
  2. -v: Verbose output
  3. -e ssh: Use ssh as the remote shell so everything is encrypted
  4. --exclude='*.out': Exclude files matching the pattern, such as *.out or *.c, etc.
rsync command example

In this example, all files are copied recursively from the ~/virt/ directory, but all *.new files are excluded:

	
$ rsync -av -e ssh --exclude='*.new' ~/virt/ root@centos7:/tmp

Example output:

SCP usage tips-recursively exclude files

Scp exclude files but using rsync exclude command

If rsync is not found on the remote server, the rsync command will fail. In this case, try using the following scp command, which uses bash shell pattern matching in the current directory (it does not work with the -r option):

$ ls

Example output:

centos71.log centos71.qcow2 centos71.qcow2.new centos71.v2.qcow2.new meta-data user-data

Copy everything in the current directory except .new:

$ shopt -s extglob
$ scp !(*.new) root@centos7:/tmp/

Example output:

centos71.log 100 % 4262 1.3MB/s 00:00
centos71.qcow2 100 % 836MB 32.7MB/s 00: 25
meta-data 100 % 47 18.5KB/s 00:00
user-data 100 % 1543 569.7KB/s 00:00

For more information, see the following man pages:

$ rsync -av -e ssh --exclude='*.new' ~/virt/ root@centos7:/tmp

The above is the detailed content of SCP usage tips-recursively exclude files. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:linuxprobe.com. If there is any infringement, please contact admin@php.cn delete