我有一个文本文件,它指定需要复制的文件:
...
b/bamboo/forest/00000456.jpg
b/bamboo/forest/00000483.jpg
...
c/corridor/00000334.jpg
c/corridor/00000343.jpg
... 但是,我想在保留它们的子目录结构的同时复制它们。因此,结果将是:
...
newfolder/b/bamboo/forest/00000483.jpg
newfolder/b/bamboo/forest/00000456.jpg
...
newfolder/c/corridor/00000334.jpg
newfolder/c/corridor/00000343.jpg
...我有这个cat /path/to/files.txt | xargs cp -t /dest/path/。但是它只是把所有的东西拷贝到一个目录中。
发布于 2016-03-21 22:40:34
您可以使用cp --parents
--parents -- append source path to target directory
cat /path/to/files | xargs cp --parents -t new_directory如果这不适用于您,那么您可以采用枯燥的方法,在/path/to/files.txt中遍历每个文件,并根据需要使用mkdir -p创建目标目录,然后简单地复制该文件:
while read -r file; do
new_dir="new_directory/$(dirname "$file")"
# ^ this is the new directory root
mkdir -p "$new_dir"
cp "$file" "$new_dir/$file"
done < <(cat /path/to/files.txt)https://stackoverflow.com/questions/36142838
复制相似问题