在终端中,将多个文件夹合并为一个

发布于 2024-08-30 11:31:25 字数 749 浏览 5 评论 0原文

我有一个由 WDBackup(西部数据外置硬盘备份实用程序)创建的备份目录,其中包含每天备份的目录以及备份内容的增量内容。

因此层次结构如下所示:

20100101
  My Documents
    Letter1.doc
  My Music
    Best Songs Every
      First Songs.mp3
    My song.mp3 # modified 20100101
20100102
  My Documents
    Important Docs
      Taxes.doc
  My Music
    My Song.mp3 # modified 20100102

...etc...

仅备份已更改的内容,并且所做的第一个备份包含选择用于备份的所有文件。

我现在想做的是增量复制,同时保持文件夹结构,从最旧到最新,将每个带日期的文件夹复制到“合并”文件夹中,以便它覆盖旧内容并保留新内容。

例如,如果仅使用这两个示例文件夹,最终合并的文件夹将如下所示:

Merged
  My Documents
    Important Docs
      Taxes.doc
    Letter1.doc
  My Music
    Best Songs Every
      First Songs.mp3
    My Song.mp3 # modified 20100102

希望这是有道理的。

谢谢,

乔什

I have a backup directory created by WDBackup (western digital external HD backup util) that contains a directory for each day that it backed up and the incremental contents of just what was backed up.

So the hierarchy looks like this:

20100101
  My Documents
    Letter1.doc
  My Music
    Best Songs Every
      First Songs.mp3
    My song.mp3 # modified 20100101
20100102
  My Documents
    Important Docs
      Taxes.doc
  My Music
    My Song.mp3 # modified 20100102

...etc...

Only what has changed is backed up and the first backup that was ever made contains all the files selected for backup.

What I'm trying to do now is incrementally copy, while keeping the folder structure, from oldest to newest, each of these dated folders into a 'merged' folder so that it overrides the older content and keeps the new stuff.

As an example, if just using these two example folders, the final merged folder would look like this:

Merged
  My Documents
    Important Docs
      Taxes.doc
    Letter1.doc
  My Music
    Best Songs Every
      First Songs.mp3
    My Song.mp3 # modified 20100102

Hope that makes sense.

Thanks,

Josh

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

故事灯 2024-09-06 11:31:25

您可以在 bash for 循环中使用 rsync,例如:

$ for d in 2010* ; do rsync -av ./$d/ ./Merged/ ; done

请注意,在真正运行之前,您可能只想小心谨慎并测试它实际上会执行您想要的操作 - 为此,您可以使用 rsync 的 - n 标志进行“试运行”:

$ for d in 2010* ; do rsync -avn ./$d/ ./Merged/ ; done

You can use rsync in a bash for loop, e.g.:

$ for d in 2010* ; do rsync -av ./$d/ ./Merged/ ; done

Note that before running this for real you might just want to be cautious and test that it's actually going to do what you want it to - for this you can use rsync's -n flag to do a "dry run":

$ for d in 2010* ; do rsync -avn ./$d/ ./Merged/ ; done
夜无邪 2024-09-06 11:31:25

如果目录名称实际上采用 YYYYMMDD 形式,您可以对它们进行排序。
因此,只需从较旧的开始一次遍历它们并将所有内容复制到目的地,覆盖以前的内容即可。效率很低,但有效。

cd $my_disk_full_of_backups
for x in (ls | sort); do cp -Rf $x/* /my_destination/; done

尽管它很丑陋,但在几乎所有具有 bash 的 UNIX 系统上也应该具有相当的可移植性。

有关详细信息,请参阅 bash(1)、ls(1) 和 sort(1)。

If directory names are actually in the YYYYMMDD form you can just sort 'em.
So it's just a matter of traversing them one at a time starting from the older and copying everything to the destination, overwriting previous stuff. Pretty inefficient, but works.

cd $my_disk_full_of_backups
for x in (ls | sort); do cp -Rf $x/* /my_destination/; done

Ugly as it is, should also be fairly portable on nearly every unix system that has bash.

See bash(1), ls(1) and sort(1) for details.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文