Home> System Tutorial> LINUX> body text

SCP usage tips-recursively exclude files

WBOY
Release: 2024-04-22 09:04:01
forward
375 people have browsed it

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/
Copy after login
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/
Copy after login

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
Copy after login

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
Copy after login

Example output:

centos71.log centos71.qcow2 centos71.qcow2.new centos71.v2.qcow2.new meta-data user-data
Copy after login

Copy everything in the current directory except .new:

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

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
Copy after login

For more information, see the following man pages:

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

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!

source:linuxprobe.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!