Fastest way to tell if two files have the same contents in Unix/Linux? Fastest way to tell if two files have the same contents in Unix/Linux? unix unix

Fastest way to tell if two files have the same contents in Unix/Linux?


I believe cmp will stop at the first byte difference:

cmp --silent $old $new || echo "files are different"


I like @Alex Howansky have used 'cmp --silent' for this. But I need both positive and negative response so I use:

cmp --silent file1 file2 && echo '### SUCCESS: Files Are Identical! ###' || echo '### WARNING: Files Are Different! ###'

I can then run this in the terminal or with a ssh to check files against a constant file.


To quickly and safely compare any two files:

if cmp --silent -- "$FILE1" "$FILE2"; then  echo "files contents are identical"else  echo "files differ"fi

It's readable, efficient, and works for any file names including "` $()