translated

This commit is contained in:
geekpi 2018-01-10 08:51:20 +08:00
parent 3fcc6e8584
commit 1a72470dfa
2 changed files with 86 additions and 89 deletions

View File

@ -1,89 +0,0 @@
translating---geekpi
How to exclude file when using scp command recursively
======
I need to copy all the *.c files from local laptop named hostA to hostB including all directories. I am using the following scp command but do not know how to exclude specific files (such as *.out):
```
$ scp -r ~/projects/ user@hostB:/home/delta/projects/
```
How do I tell scp command to exclude particular file or directory at the Linux/Unix command line?
One can use scp command to securely copy files between hosts on a network. It uses ssh for data transfer and authentication purpose. 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 so you can filter or exclude files when using scp command. However, there is a great workaround to exclude files and copy it securely using ssh. This page explains how to filter or excludes files when using scp to copy a directory recursively.
## How to use rsync command to exclude files
The syntax is:
`rsync av -e ssh --exclude='*.out' /path/to/source/ [[email protected]][1]:/path/to/dest/`
Where,
1. **-a** : Recurse into directories i.e. copy all files and subdirectories. Also, turn on archive mode and all other options (-rlptgoD)
2. **-v** : Verbose output
3. **-e ssh** : Use ssh for remote shell so everything gets encrypted
4. **\--exclude='*.out'** : exclude files matching PATTERN e.g. *.out or *.c and so on.
### Example of rsync command
In this example copy all file recursively from ~/virt/ directory but exclude all *.new files:
`$ rsync -av -e ssh --exclude='*.new' ~/virt/ [[email protected]][1]:/tmp`
Sample outputs:
[![Scp exclude files but using rsync exclude command][2]][2]
Rsync command will fail if rsync not found on the remote server. In that case try the following scp command that uses [bash shell pattern matching][3] in the current directory (it won't work with the -r option):
`$ ls `
Sample outputs:
```
centos71.log centos71.qcow2 centos71.qcow2.new centos71.v2.qcow2.new meta-data user-data
```
centos71.log centos71.qcow2 centos71.qcow2.new centos71.v2.qcow2.new meta-data user-data
Copy everything in the current directory except .new file(s):
```
$ shopt -s extglob
$ scp !(.new)* [[email protected]][1]:/tmp/
```
Sample outputs:
```
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
```
See the following man pages for more info:
```
$ [man rsync][4]
$ man bash
$ [man scp][5]
```
--------------------------------------------------------------------------------
via: https://www.cyberciti.biz/faq/scp-exclude-files-when-using-command-recursively-on-unix-linux/
作者:[Vivek Gite][a]
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]:https://www.cyberciti.biz
[1]:https://www.cyberciti.biz/cdn-cgi/l/email-protection
[2]:https://www.cyberciti.biz/media/new/faq/2017/12/scp-exclude-files-on-linux-unix-macos-bash-shell-command-line.jpg
[3]:https://www.gnu.org/software/bash/manual/html_node/Pattern-Matching.html#Pattern-Matching
[4]:https://www.samba.org/ftp/rsync/rsync.html
[5]:https://man.openbsd.org/scp

View File

@ -0,0 +1,86 @@
如何在递归地使用 scp 命令时排除文件
======
我需要将所有包含 *.c 文件的文件夹从名为 hostA 的本地笔记本复制到 hostB。我使用的是下面的 scp 命令,但不知道如何排除特定的文件(如 \*.out
```
$ scp -r ~/projects/ user@hostB:/home/delta/projects/
```
如何告诉 scp 命令在 Linux/Unix 命令行中排除特定的文件或目录?
人们可以使用 scp 命令在网络主机之间安全地复制文件。它使用 ssh 进行数据传输和身份验证。典型的语法是:
```
scp file1 user@host:/path/to/dest/
scp -r /path/to/source/ user@host:/path/to/dest/
```
## Scp 排除文件
我不认为你可以在使用 scp 命令时过滤或排除文件。但是,有一个很好的解决方法来排除文件并使用 ssh 安全地复制它。本页面说明如何在使用 scp 递归复制目录时过滤或排除文件。
## 如何使用 rsync 命令排除文件
语法是:
`rsync av -e ssh --exclude='*.out' /path/to/source/ [[email protected]][1]:/path/to/dest/`
这里:
1. **-a** :递归到目录,即复制所有文件和子目录。另外,打开归档模式和所有其他选项(-rlptgoD
2. **-v** :详细输出
3. **-e ssh** :使用 ssh 作为远程 shell这样所有的东西都被加密
4. **\--exclude='*.out'** :排除匹配模式的文件,例如 \*.out 或 \*.c 等。
### rsync 命令的例子
在这个例子中,从 ~/virt/ 目录递归地复制所有文件,但排除所有 \*.new 文件:
`$ rsync -av -e ssh --exclude='*.new' ~/virt/ [[email protected]][1]:/tmp`
示例输出:
[![Scp exclude files but using rsync exclude command][2]][2]
如果远程服务器上找不到 rsync那么 rsync 命令将失败。在这种情况下,请尝试使用以下 scp 命令,该命令在当前目录中使用[ bash shell 模式匹配] [3](它不与 -r 选项一起使用):
`$ ls `
示例输出:
```
centos71.log centos71.qcow2 centos71.qcow2.new centos71.v2.qcow2.new meta-data user-data
```
centos71.log centos71.qcow2 centos71.qcow2.new centos71.v2.qcow2.new meta-data user-data
复制除 .new 之外的当前目录中的所有内容:
```
$ shopt -s extglob
$ scp !(.new)* [[email protected]][1]:/tmp/
```
示例输出:
```
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
```
有关更多信息,请参阅以下手册页:
```
$ [man rsync][4]
$ man bash
$ [man scp][5]
```
--------------------------------------------------------------------------------
via: https://www.cyberciti.biz/faq/scp-exclude-files-when-using-command-recursively-on-unix-linux/
作者:[Vivek Gite][a]
译者:[geekpi](https://github.com/geekpi)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]:https://www.cyberciti.biz
[1]:https://www.cyberciti.biz/cdn-cgi/l/email-protection
[2]:https://www.cyberciti.biz/media/new/faq/2017/12/scp-exclude-files-on-linux-unix-macos-bash-shell-command-line.jpg
[3]:https://www.gnu.org/software/bash/manual/html_node/Pattern-Matching.html#Pattern-Matching
[4]:https://www.samba.org/ftp/rsync/rsync.html
[5]:https://man.openbsd.org/scp