复制嵌套子目录中的文件列表

时间:2020-06-21 16:15:18

标签: bash subdirectory cp

我有一组子目录,其中包含文件:

├── dir1
│   ├── file_a.type
│   ├── file_b.type
│   ├── file_c.type
│   └── file_d.type
├── dir2
│   ├── file_e.type
│   ├── file_f.type
│   ├── file_g.type
│   └── file_h.type
└── README.md

我可以保证每个文件名的唯一性。目录命名约定为n[some unique random number]

我有一个文本文件,其中包含这些文件的子集

file_g.type
file_a.type
file_e.type

我想将与该文本文件中的名称匹配的所有文件复制到新目录中。

我尝试使用xargs进行复制,但是由于子目录的原因,这无法正常工作。

xargs -a files.txt cp -t new_dir each

我可以将子目录中的所有文件递归复制到一个新目录中,然后从那里开始。
但是,由于磁盘大小和带宽问题,这是不可能的。

使用标准bash实用程序执行此操作的有效方法是什么?

1 个答案:

答案 0 :(得分:2)

如果文件名为files.txt,则文件名:

while read file
do
    cp dir[12]/"${file}" -t new_dir
done < files.txt

使用xargs可以这样做:

xargs -a files.txt -IFILE bash -c 'cp dir[12]/"FILE" -t new_dir'