3

I was running diff --brief -r /home/mateusz/ /media/mateusz/Database/backup_test_tmp_folder/home/mateusz/ command that completed with error code 2. According to documentation "Exit status is 0 if inputs are the same, 1 if different, 2 if trouble."

Unfortunately command exited without giving any clue why it failed. I tried using straces but unfortunately I am unable to diagnose anything based on produced log (last 150 lines of 3.5GB strace file are posted to https://gist.github.com/matkoniecz/15ed855bd3f161ad6354c7d637234804 - and also for diff running with sudo that failed after producing 78KB of strace log).

I considered modifying diff command to output some explanation before dying and returning 2, but given that it is unlikely that my program is unique lack of support for such debugging seems to indicate that there are better solutions (hopefully it is not "learn how to interpret strace").

So how I am supposed to check why my diff command failed? Is there any better tool than strace or adding printf debugging to diff command?

1 Answers1

2

You can read the output. diff doesn't give different exit status codes other than:

  • 0 if inputs are the same,
  • 1 if different,
  • 2 if trouble.

(from man diff)

"Trouble" means it couldn't read a file, or pretty much anything else.

From a little testing using diff (GNU diffutils) 3.3 and comparing folders, diff outputs messages to stdout or stderr:

  • If one file is missing then the exit status is 1, and this appears on stdout:

      Only in folder1: file-a
    
  • If one file is unreadable then the exit status is 2, and this appears on stderr:

      diff: folder1/file-b: Permission denied
    

(If both errors occur, the exit status is 2.)

So, read / parse stdout and stderr to find out what the problems are.

Note that diff will continue processing further files after encountering "trouble", so the line reporting the reason for the exit status 2 may be at any part of the output.


See this GNU "diffutils manual" link for more info on comparing directories (and using diff in general , it's insanely more detailed than just man diff). Info like:

... if you use the --report-identical-files (-s) option, it reports pairs of identical files

If only one file exists, diff normally does not show its contents; it merely reports that one file exists but the other does not. You can make diff act as though the missing file is empty, so that it outputs the entire contents of the file that actually exists... use --new-file (-N)... If the older directory contains large files that are not in the newer directory, you can make the patch smaller by using the --unidirectional-new-file option instead of -N.

To ignore some files while comparing directories, use the --exclude=pattern (-x pattern) option

If you have been comparing two directories and stopped partway through, later you might want to continue where you left off. You can do this by using the --starting-file=file (-S file) option. This compares only the file file and all alphabetically later files in the topmost directory level.

Or these related Q's from other sites:

Xen2050
  • 8,943