Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

Are there any Linux/Unix tools which find all the files in one directory not present in another? Basically I'm looking for diff which works on the output of ls.

Short and sweet scripts are also appreciated.

share|improve this question
add comment

6 Answers

diff does this already:

diff dir1 dir2

Example output:

Only in dir1: some_file.txt
Only in dir1: some_other_file.txt
Only in dir2: third_file.txt
share|improve this answer
1  
This is good. One gripe: diff is actually running on each of the files that are in both. Is there an obscure option to just run against file names (I may have missed it)? Otherwise, I suggest diff dir1 dir2 | grep "Only" –  Willi Ballenthin Sep 1 '10 at 16:12
add comment

Bash:

diff <(cd dir1; ls) <(cd dir2; ls)

Compare only the filenames - not the contents of the files.

share|improve this answer
    
Also ksh and zsh. –  Dennis Williamson Sep 1 '10 at 18:37
add comment

Like people told you here, you can use DIFF in various usage variations. Or you just use dirdiff instead, which is meant for what you're trying! :-)

But if you want to keep some directories in sync then you really should take a look on rsync.

Regards

share|improve this answer
add comment

This works..

ls -a1 /dir1 | sort > /tmp/1
ls -a1 /dir2 | sort > /tmp/2
diff /tmp/1 /tmp/2
share|improve this answer
add comment

untested:

find /dir/A -printf "%P" | while read f; do
  if [ ! -e "/dir/B/$f" ]; then
    echo $f
  fi
done
share|improve this answer
add comment

for i in $(ls -1 directory1); do if (test -f directory2/$i) then echo $i; fi; done

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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