Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I have two directories that should contain the same files and have the same directory structure.

I think that something is missing in one of these directories.

Using the bash shell, is there a way to compare my directories and see if one of them is missing files that are present in the other?

share|improve this question
    
What is the output of bash --version? –  i08in Feb 16 at 17:10
add comment

3 Answers 3

You can use the diff command just as you would use it for files:

diff <directory1> <directory2>

If you want to see subfolders and -files too, you can use the -r option:

diff -r <directory1> <directory2>
share|improve this answer
1  
Didn't know diff works for directories as well(man diff confirmed that), but this doesn't recursively check for changes in subdirectories inside subdirectories. –  i08in Feb 16 at 17:04
    
@Jobin That's strange... For me, it does work. –  Alex R. Feb 16 at 17:07
    
I have something like this: a/b/c/d/a, x/b/c/d/b. See what diff a x gives you. –  i08in Feb 16 at 17:09
    
You have to use the -r option. That (diff -r a x) gives me: Only in a/b/c/d: a. only in x/b/c/d: b. –  Alex R. Feb 16 at 17:11
    
Cool! It works! +1. Diff just got more powerful(for me)! :) –  i08in Feb 16 at 17:12
show 1 more comment

Through you are not using bash, you can do it using diff with --brief and --recursive:

$ diff -rq dir1 dir2 
Only in dir2: file2
Only in dir1: file1

The man diff includes both options:

-q, --brief
report only when files differ

-r, --recursive
recursively compare any subdirectories found

share|improve this answer
add comment

Here is an alternative, to compare just filenames, and not their contents:

diff <(cd folder1 && find . | sort) <(cd folder2 && find . | sort)

This is an easy way to list missing files, but of course it won't detect files with the same name but different contents!

(Personally I use my own diffdirs script, but that is part of a larger library.)

share|improve this answer
1  
You'd better use process substitution, not temp files... –  mniip Feb 16 at 18:03
    
Good suggestion, thanks. –  joeytwiddle Feb 17 at 5:16
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.