区分多个文件,如果不相等则为真

Nic*_*oul 18 bash diff

我有许多文件,我想检查所有这些文件是否具有相同的内容。

我可以使用什么命令行来检查?

用法可能是这样的:

$ diffseveral file1 file2 file3 file4
Run Code Online (Sandbox Code Playgroud)

结果:

All files equals
Run Code Online (Sandbox Code Playgroud)

或者

Files are not all equals
Run Code Online (Sandbox Code Playgroud)

Gil*_*il' 28

使用 GNU diff,将其中一个文件作为参数传递给--from-file任意数量的其他文件作为操作数:

$ diff -q --from-file file1 file2 file3 file4; echo $?
0
$ echo >>file3
$ diff -q --from-file file1 file2 file3 file4; echo $?
Files file1 and file3 differ
1
Run Code Online (Sandbox Code Playgroud)

您可以像往常一样使用通配符。例如,如果当前目录包含file1, file2, file3and file4,下面的例子比较file2,file3file4to file1

$ diff -q --from-file file*; echo $?
Run Code Online (Sandbox Code Playgroud)

请注意,“来自文件”必须是常规文件,而不是管道,因为 diff 会多次读取它。


Arc*_*ege 5

怎么样:

md5sum * | awk 'BEGIN{rc=1}NR>1&&$1!=last{rc=0}{last=$1}END{exit rc}'
Run Code Online (Sandbox Code Playgroud)

计算每个文件的 MD5 值,然后将每个条目与下一个条目进行比较,如果有任何不同,则返回零(真)退出状态。如果不同,则返回 false 会更短:

md5sum * | awk 'NR>1&&$1!=last{exit 1}{last=$1}'
Run Code Online (Sandbox Code Playgroud)

没有必要排序,因为我们只是检查是否有任何不同。