7

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.

7 Answers 7

17

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
7

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
1
  • 2
    If you need subdirectories add the -r flag as well.
    – djhowell
    Oct 28, 2009 at 20:11
3

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 ;)

3

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.

1

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.

2
  • 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, 2009 at 20:08
  • Other people's answers of diff -q dir1 dir2 are much better than mine!
    – Josh
    Oct 28, 2009 at 20:24
1

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'.

0

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .