41

I am trying to do something along the lines of:

diff `ls -1a ./dir1` `ls -1a ./dir2`

But that doesn't work for obvious reasons. Is there a better way of achieving this (in 1 line), than this?

ls -1a ./dir1 > lsdir1
ls -1a ./dir2 > lsdir2
diff lsdir1 lsdir2

Thanks

bguiz
  • 2,141

4 Answers4

66

You were close. In bash you want process substitution, not command substitution:

diff <(ls -1a ./dir1) <(ls -1a ./dir2)
15
diff -rq dir1 dir2

using the -r option, walk entire directory trees, recursively checking differences between subdirectories and files that occur at comparable points in each tree. The trick is to use the -q option to suppress line-by-line comparisons

fseto
  • 1,437
1

Expanding on this, since I don't have enough points to comment, the options -y (to show in two columns) and --suppress-common-lines make it even better!

diff -y --suppress-common-lines <(ls -1a ./dir1) <(ls -1a ./dir2)

You can remove -a from ls parts if not interested in dot/hidden files.

Unknown
  • 15
0

Neither answers did the work for me as :

  1. I needed the diff to be recursive
  2. My folders were not side by side

Inspired from @Ignacio Vazquez-Abrams I used :

diff <(find PATH1 | sed 's"PATH1""') <(find PATH2 | sed 's"PATH2""')

Which does the job just fine and can be adapted for a nifty script :

#!/bin/bash
# Usage : compDirs dir1 dir2
# compare the file/folder names recursively in Dir1 and Dir2

PATH1=$1 PATH2=$2

diff <(find $PATH1 | sed "s#$PATH1##") <(find $PATH2 | sed "s#$PATH2##")

cmbarbu
  • 149