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.

There are two directories I'd like to compare. I tried diff but it includes the changes inside the files. All I want is something like this

file a is just in /A 
file b is missing in /A 
file c changed
directory d is missing in /A 
directory e is just in /A

I think this is common when doing full file patching but I don't know a good solution.

share|improve this question

7 Answers 7

up vote 16 down vote accepted

You're looking for

diff -rq (dir1) (dir2)

Proof of concept:

#!/bin/sh
#create our test
mkdir -p /tmp/a/b
echo "test" >> /tmp/a/c
mkdir -p /tmp/a/d/e
echo "blah" >> /tmp/a/d/e/f #only exists here
mkdir -p /tmp/q/b
echo "testing" >> /tmp/q/c #/tmp/a/c shouldnt match
mkdir -p /tmp/q/d/e
echo "blah" >> /tmp/q/d/e/g #only exists here
diff -rq /tmp/a /tmp/q

results in :

Files /tmp/a/c and /tmp/q/c differ
Only in /tmp/a/d/e: f
Only in /tmp/q/d/e: g
share|improve this answer

You just need to include the -q flag to make it brief:

# diff -q dir1 dir2
Files dir1/both and dir2/both differ
Only in dir1/: one
Only in dir2:/ two
share|improve this answer
2  
If you need subdirectories add the -r flag as well. –  djhowell Oct 28 '09 at 20:11

For finding duplications, you one use:

fdupes -r1 dir1 dir2

Although the others gave you numerous good tips, you should give it it a try too.

If you use

fdupes -rd dir1 dir2

it will prompt you which file to keep (the others will be deleted). Extremely useful for removing duplications (I did make a good use of it with my photos)

NOTE: yes, I know the question wasn't exactly about this, but maybe it can help him or others ;)

share|improve this answer

If you want compare files based on e.g. size you can do:

# ls -al DIR_1 |awk '{print $5, $9}'|sort > 1.txt
# ls -al DIR_2 |awk '{print $5, $9}'|sort > 2.txt

and than:

# diff 1.txt 2.txt

to find out which files have different or are missing. I used this when I had to compare two directories with very large files to see which files are not fully downloaded.

share|improve this answer

You could try a diff of ls:

ls A > a.txt
ls B > B.txt
diff a.txt b.txt

Not a single command but it should work.

share|improve this answer
    
In fact it wouldn't be too hard to make a script that does this automatically and cleans up after itself. I could write such a script but someone else might provide a better answer. –  Josh Oct 28 '09 at 20:08
    
Other people's answers of diff -q dir1 dir2 are much better than mine! –  Josh Oct 28 '09 at 20:24

The classic answer is the 'dircmp' command. It has its warts (piping the output through 'pr' to paginate it, for example), but if would give you a list of objects only in directory one or directory two, and then for the common files, it report 'same' or 'different' (and the file type for non-files - directories, etc).

The 'diff -rq' looks equivalent or better than the output from 'dircmp'.

share|improve this answer

I asked a somewhat similar question on Stack Overflow, and the answers I got may be of interest to you. I was particularly interested in finding missing files when comparing two directories.

I also specified that the solution should deal with renamed files (e.g. if file A which was in directory 1 is also present in directory 2 but has been renamed, the script should be aware of that).

The answer I chose (it's at the top) is quite useful. It might be worth a look as a starting point for your script.

share|improve this answer

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.