如何`scp`目录保留结构但只挑选某些文件?

时间:2015-06-12 15:01:28

标签: bash scp

我需要保护copy(scp)以远程复制目录,并保留从UNIX命令行保留的子结构。子目录有相同名称的文件,我想要和其他一些我不喜欢的东西。这是结构的样子。

directorytocopy
  subdir1
    1.wanted
    2.wanted
    ...
    1.unwanted
    2.notwanted
  subdir2
    1.wanted
    2.wanted
    ...
    1.unwanted
    2.notwanted
  ..

我只想保留目录结构的.wanted文件。我意识到可以编写一个shell(我使用bash)脚本来执行此操作。是否有可能以较少暴力的方式做到这一点?我无法复制整个内容并删除不需要的文件,因为我没有足够的空间。

3 个答案:

答案 0 :(得分:2)

Adrian最好使用rsync。您还可以使用tar捆绑所需文件:

cd directorytocopy
shopt -s nullglob globstar
tar -cf - **/*.wanted | ssh destination 'cd dirToPaste && tar -xvf -'

这里,使用tar -f选项,文件名为-,使用stdin / stdout作为存档文件。

这是未经测试的,可能会失败,因为存档可能不包含保存"想要"的实际子目录。文件。

答案 1 :(得分:2)

假设源计算机上有GNU tar,并假设所需文件的文件名不包含换行符,并且它们足够短以适合tar标题:

find /some/directory -type f -name '*.wanted' | \
    tar cf - --files-from - | \
    ssh user@host 'cd /some/other/dir && tar xvpf -'

答案 2 :(得分:1)

rsync with and -exclude/include list follwing @Adrian Frühwirth's suggestion would be a to do this.

相关问题