When I set the -s parameter, diff also print files, that are different.

  • Please edit your question and clarify. What do you want to happen when files are identical? What do you want to see when they are not identical? – terdon Feb 25 '16 at 16:32
  • I want to display message, when files are Identical.I solved this problem, by using $?, but maybe there is another solution – FezZ Feb 25 '16 at 21:32
  • 1
    do you mean something like diff a b >/dev/null && echo different? – terdon Feb 25 '16 at 22:11
  • @terdon if that's all that's desired then cmp would be a better tool. cmp -s file1 file2 && echo identical (Also, you wrote different instead of identical...) – Wildcard Feb 25 '16 at 23:15

One possible solution may be:

diff -s $FIRST_FILE $SECOND_FILE > /dev/null
if [ $? -eq 0 ]; then
    echo "The files are identical"

NOTE: It changed the question text.

  • As has been pointed out, cmp really is better in this case, because it won't waste time trying to construct a nice patch. – Stephen Kitt Feb 26 '16 at 8:28
  • I base my answer on the instructions given. I thought he needed to use diff something I know about what he wanted to do. Naturally, it is better to use cmp. – Juan Antonio Clavero Garcia Feb 26 '16 at 10:23

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"

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
  • Yeah, I know it, but i want, that it print only identical (ignore differ) – FezZ Feb 25 '16 at 21:34
  • @FezZ - nothing stopping you to pipe this to sed '/ differ$/d' – don_crissti Feb 25 '16 at 22:16

I have put quotes around the filenames, you should also change the filenames to lowercase.

diff "${FIRST_FILE}" "${SECOND_FILE}" > /dev/null && echo "Same"
  • Good point about filenames to lowercase but you didn't explain why. @FezZ, by convention all-caps variable names in bash are for environment variables only. For non-environment variables you should use lowercase names. – Wildcard Feb 25 '16 at 23:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.