我即将删除一个旧的备份目录,但在此之前,我想确保所有这些文件存在一个新的目录。
有这个工具吗? 或者我最好做这个“手动”使用find , md5sum ,sorting,比较等?
澄清:
如果我有以下目录列表
/path/to/old_backup/dir1/fileA /path/to/old_backup/dir1/fileB /path/to/old_backup/dir2/fileC
和
/path/to/new_backup/dir1/fileA /path/to/new_backup/dir2/fileB /path/to/new_backup/dir2/fileD
那么fileA和fileB存在于new_backup ( fileA的原始目录中, fileB已经从dir1移到dir2 )。 另一方面, new_backup在new_backup丢失,并且fileD已经创build。 在这种情况下,我想输出是类似的东西
fileC exists in old_backup, but not in new_backup.
Python有一些很好的标准库模块,称为dircmp / filecmp。
来自Doug Hellmann的PyMOTW,这一小段代码给你:
import filecmp filecmp.dircmp('example/dir1', 'example/dir2').report()
给你:
diff example/dir1 example/dir2 Only in example/dir1 : ['dir_only_in_dir1', 'file_only_in_dir1'] Only in example/dir2 : ['dir_only_in_dir2', 'file_only_in_dir2'] Identical files : ['common_file', 'not_the_same'] Common subdirectories : ['common_dir'] Common funny cases : ['file_in_dir1']
Doug在filecmp / dircmp方式上解释了比我更好的方式:
http://www.doughellmann.com/PyMOTW/filecmp/
我喜欢python这样的事情,因为它比Linux上的任何shell基于Linux / Windows / Solaris更容易端口。
我会做我自己的工具。 去一些奇特的发现去这个手动路线:
find /oldbackup -exec basename {} ";" > /tmp/old.txt find /newbackup -exec basename {} ";" > /tmp/new.txt for $filename in `cat /tmp/old.txt` do grep $filename /tmp/new.txt if [ "$?" -ne "0" ]; then echo "$filename not in new backup" fi done
这是令人难以置信的马虎,但基本的algorithm应该没问题。 你也可以做一些猜测,找出哪些文件没有副本,就像这样:
find /oldbackup -exec basename {} ";" > /tmp/old.txt find /newbackup -exec basename {} ";" > /tmp/new.txt cat /tmp/old.txt /tmp/new.txt | sort | uniq -c | grep -v 2
如果你需要,我可以解释一下。
使用diff -uRN dir1 dir2
如果文件是完美的,差异将是空的…或者它会显示!