简体   繁体   English

文件夹和文件的副本太多

[英]Too many copies of folders and files

I think everyone is familiar with this script: 我认为每个人都熟悉此脚本:

find /dir1/dir2/dir3/dir4/* -mtime +5 -exec cp -rf {} /dirA/dirB/dirC/ \;

My problem is that I want the contents of dir4 that are older than 5 days, which will be more subdirectories and their contents, to be copied into dirC with their directory structures intact. 我的问题是,我希望将超过5天的dir4内容复制到dirC中,且目录结构完好无损,这些内容将是更多子目录及其内容。 Sounds good so far and that script should do the job I thought. 到目前为止听起来不错,该脚本应该可以完成我认为的工作。

But it isn't doing what I thought it should. 但是它没有按照我的预期去做。 Instead, it starts in dir1, drills down all the way to the lowest folder and starts copying, then it goes up and starts over in dir4, and so on and so on. 相反,它从dir1开始,一直向下钻取到最低的文件夹并开始复制,然后上升并在dir4中重新开始,依此类推。 The end result is everything in the folder structure is copied multiple times. 最终结果是多次复制了文件夹结构中的所有内容。

I've tried rsync, cpio, and pax in place of cp as well with the same results whether I'm doing rsync -r or cpio -r or pax -r. 无论我正在执行rsync -r还是cpio -r或pax -r,我都尝试使用rsync,cpio和pax代替cp并获得相同的结果。 They all start copying every portion of the directory path. 它们都开始复制目录路径的每个部分。

Any ideas? 有任何想法吗?

You have two problems: 您有两个问题:

  1. You try to copy a recursive list recursively (double recursion), thereby including files you don't want 您尝试递归地复制递归列表(两次递归),从而包含不需要的文件
  2. You copy without preserving directory structure in relation to the source base directory, thereby ending up with a mangled tree 您在复制时未保留相对于源基本目录的目录结构,因此最终出现了混乱的树

Instead, you should non-recursively your a recursive list of files to their corresponding directories. 相反,您应该以非递归方式将文件的递归列表添加到其对应的目录中。 You can do this with rsync 's --files-from and process substitution: 您可以使用rsync--files-from和进程替换来执行此操作:

rsync --from0 --files-from <(find ./src -mtime +5 -print0) \
  ./  ./target

Alternatively, through cpio : 或者,通过cpio

find src/ -mtime +5 -print0 | cpio -0 -o | { cd target && cpio -i; }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM