Bash – Diff several files, true if all not equal

bashdiff()

I have a number of files, I want to check that all those files have the same content.

What command line could I use to check that?

Usage could be something like:

$ diffseveral file1 file2 file3 file4

Result:

All files equals

OR

Files are not all equals

Best Answer

With GNU diff, pass one of the files as an argument to --from-file and any number of others as operand:

$ 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

You can use globbing as usual. For example, if the current directory contains file1, file2, file3 and file4, the following example compares file2, file3 and file4 to file1.

$ diff -q --from-file file*; echo $?

Note that the “from file” must be a regular file, not a pipe, because diff will read it multiple times.

Related Question