How to write a script that compares the specified (as parameters) directories by content based on information about the size of files and the date they were changed? It is necessary to output for each file available in both directories something like this:

$ diff-dir /tmp/dir1 /tmp/dir2/ < file1.txt (новее в dir2) < file2.txt (новее в dir2) > file3.txt (старее в dir2) + subdir1 (отсутствует в dir1) - subdir2 (отсутствует в dir2) 
  • looks like a TK, not a question - yapycoder
  • corrected - LackOfKnowledge

3 answers 3

The question is old, but it is not worth leaving unanswered.
Something like this. I am sure that there is a more elegant way (in bash it will always be found: D), but this is better than nothing.

 #!/bin/bash ls $1 | while read i do if [ -e "$2/$i" ] then if [ `stat -c %Y $1/$i` -lt `stat -c %Y $2/$i` ] then echo "> $i (старее в $2)" else echo "< $i (старее в $1)" fi else echo "- $i (отсутствует в $2)" fi done ls $2 | while read i do if [ ! -e "$1/$i" ] then echo "+ $i (отсутствует в $1)" fi done 

    diff -q -r DIR1 DIR2

    This is not exactly what is requested, but it seems. There is no older / newer information in the command output (only Files DIR1 / File-x and DIR2 / File-x are differ). You can analyze in your script.

      There are already ready fdupes that can compare directories and kill duplicates. But he does not know how to write which files are missing in one or another directory. But it can either be used as part of a shell script, or simply rewritten to fit your needs.

      • You do not understand, I need to write my own! - LackOfKnowledge