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.

Wienczny
  • 1,043
  • 10
  • 13

7 Answers7

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
Rizwan Kassim
  • 529
  • 6
  • 20
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
djhowell
  • 1,162
  • 7
  • 9
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.

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

asdmin
  • 2,020
  • 16
  • 28
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.

Josh
  • 9,001
  • 27
  • 78
  • 124
  • 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
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'.

Jonathan Leffler
  • 1,035
  • 11
  • 20
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.

grw
  • 168
  • 6