创建父文件夹时将文件列表复制到远程计算机

mcE*_*nge 3 command-line rsync scp

我有一个位于我通过调用创建的子目录中的文件列表find

for f in $(find . -name "myFile.txt"); do 
    echo "$f" >> filelist.txt;
done
Run Code Online (Sandbox Code Playgroud)

内容filelist.txt如下:

./First/Path/To/File/myFile.txt
./Second/Path/To/File/myFile.txt
./Third/Path/To/File/myFile.txt
...
Run Code Online (Sandbox Code Playgroud)

现在我想将所有这些文件复制到远程计算机,以便将它们放在相应的文件夹中:

remoteComputerName:/some/root/directory/First/Path/To/File/myFile.txt
remoteComputerName:/some/root/directory/Second/Path/To/File/myFile.txt
remoteComputerName:/some/root/directory/Third/Path/To/File/myFile.txt
Run Code Online (Sandbox Code Playgroud)

但是,在远程计算机上,文件夹结构First/Path/To/File/等还不存在,而且我不想复制整个目录,而只想复制其中的文件myFile.txt

我知道在本地计算机上这可以使用命令

while read p; do cp --parents $p /some/root/directory ; done < filelist.txt
Run Code Online (Sandbox Code Playgroud)

但是,对于使用scp此选项的远程计算机--parents不再起作用。另外rsync如果缺少不会创建父目录。有人知道解决方案吗?

hee*_*ayl 5

使用rsync

rsync -av --files-from=filelist.txt . remote:/some/location/
Run Code Online (Sandbox Code Playgroud)

从您运行的目录运行它,目录find的根目录,filelist.txt当前所在的位置。

--files-from读取要从文件中复制的文件(换行符分隔)filelist.txt

--files-from 应该始终使用相对路径(与直接提及不同),尽管您可以明确说明它:

rsync -av --relative --files-from=filelist.txt . remote:/some/location/
Run Code Online (Sandbox Code Playgroud)