【问题标题】:Copy files from list while keeping subdirectory structure从列表中复制文件,同时保持子目录结构
【发布时间】:2016-07-08 15:48:02
【问题描述】:

我有一个指定需要复制的文件的文本文件:

...
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/。但它只是将所有内容复制到一个目录中。

【问题讨论】:

    标签: bash file directory copy subdirectory


    【解决方案1】:

    你可以使用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)
    

    【讨论】:

      猜你喜欢
      • 2012-08-17
      • 1970-01-01
      • 1970-01-01
      • 2013-03-16
      • 1970-01-01
      • 2018-11-01
      • 2017-04-07
      • 2020-05-21
      • 2017-08-14
      相关资源
      最近更新 更多