Skip to content
Advertisement

Recursively check the difference between files in shell

I am trying to recursively check the difference between files in 2 separate directories in shell. NOTE :- both directories are identical only the root name is different

enter image description here

In the diagram shown above I want to “diff ROOT_A/A.md5 ROOT_B/A.md5” recursively in my script file for all files in the root directory.

for filename in ROOT_A/*.md5; do
    diff ($filename | cut -d '/' -f 2) (ROOT_B/$filename) >> logfile.txt done

However, This doesn’t work. can someone help me out with this. Thanks

Advertisement

Answer

Assuming that your shell is Bash:

ROOT_A='...'
ROOT_B='...'
for file in "$ROOT_A"/*.md5; do
  ! test -r "$file" || ! test -r "$ROOT_B/${file##*/}" && continue
  echo "Comparing $file with $ROOT_B/${file##*/}"
  diff "$file" "$ROOT_B/${file##*/}"
done

As @CharlesDuffy observes, if the files are truly MD5 sums (or other such kind of checksums), diff should be replaced with cmp because it doesn’t matter how different the files are, only if they are the same or different.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement