文件夹和文件的区别

文件夹和文件的区别

我有两个文件夹,里面有不同的文件夹。这些文件夹还包含不同的文件。我正在寻找可以提供这两个父文件夹之间差异的命令。不仅一个文件夹中包含的文件与另一个文件夹中不包含的文件存在差异,而且文件内容也存在差异。

到目前为止我已经这样做了:diff -rq fold1 fold2..但这并没有给我这些文件之间的区别。

我可以运行什么命令?

答案1

也许你可以使用同步作为做到这一点的技巧。

rsync --dry-run --delete --recursive --verbose dir1/ dir2

或者,简短版本

rsync -nrv --delete dir1/ dir2

不要忘记 --dry-run 或 -n 选项,否则目标目录 (dir2) 将与源目录 (dir1) 相同。

这将输出两个目录的差异,包括目录名和文件名和文件内容。 (您甚至可以比较两台不同机器中的 2 个目录)

sending incremental file list
deleting dir3-1/   # this directory (name) doesn't exist in source directory
deleting file2.txt # this file      (name) doesn't exist in source directory
file1.txt          # this file is different (content) from the source files
dir3/              # this directory (name) doesn't exist in destination directory

sent 95 bytes  received 21 bytes  232.00 bytes/sec
total size is 4  speedup is 0.03 (DRY RUN)

答案2

对于文件夹的差异,你可以尝试

ls -R fold1 > list1
ls -R fold2 > list2
diff list1 list2.

但是,对于diff文件,我认为您需要编写一个脚本(也许解析 ls -R 输出)。

一个有点黑客的解决方案:

find fold1 -type f|while read x; do [ -e fold2/$x ] && diff fold1/$x fold2/$x >> files_diff.out; done

但这可能不是最优雅或最有效的方法。我相信更熟悉 Bash 的人可以想出更好的方法。如果您精通 Perl,我建议使用它的 File::Find 模块。

相关内容