Silent result with two identical files in diff: how to show them?
The Unix philosophy is to have one tool per job, and the shell to glue them together. So: one tool to compare, and one tool to get the desired output format.
In this case, the output format is sufficiently simple that this part can be done directly with the shell.
To compare two files, if you're only interested in whether they have the same content and not in listing out the differences, use cmp
.
if cmp -s -- "$FIRST_FILE" "$SECOND_FILE"; then
printf '%s\n' "$FIRST_FILE = $SECOND_FILE"
fi
By default, diff
is silent when given identical files; that's the only aspect of its behaviour that -s
changes. So it always compares files, and outputs differences; with -s
it also outputs a message when files are identical, without -s
it doesn't mention identical files at all.
You can get the behaviour I think you're looking for by combining -q
and -s
; -q
instructs diff
to only indicate that files differ (when they do), without detailing the differences.
Here's an example:
$ echo 1 > a
$ echo 2 > b
$ echo 2 > c
$ diff -qs a b
Files a and b differ
$ diff -qs b c
Files b and c are identical
One possible solution may be:
diff -s $FIRST_FILE $SECOND_FILE > /dev/null
if [ $? -eq 0 ]; then
echo "The files are identical"
fi
NOTE: It changed the question text.