Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 '14 at 17:10

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
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 '14 at 17:04
@Jobin That's strange... For me, it does work. – Alex R. Feb 16 '14 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 '14 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 '14 at 17:11
Cool! It works! +1. Diff just got more powerful(for me)! :) – i08in Feb 16 '14 at 17:12

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
You'd better use process substitution, not temp files... – mniip Feb 16 '14 at 18:03
Good suggestion, thanks. – joeytwiddle Feb 17 '14 at 5:16
Note that this does not support file names with certain special characters, in that case you might want to use zero-delimiters which AFAIK diff is not supporting as of now. But there is comm which is supporting it since… so once it comes to a coreutils near you, you can do comm -z <(cd folder1 && find -print0 | sort) <(cd folder2 && find -print0 | sort -z) (whose output you might have to further convert in the format you need using the --output-delimiterparameter and additional tools). – phk Mar 5 at 21:52

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

If you want to make each file expandable and collapsible, you can pipe the output of diff -r into Vim.

First let's give Vim a folding rule:

mkdir -p ~/.vim/ftplugin
echo "set foldexpr=getline(v:lnum)=~'^diff.*'?'>1':1 foldmethod=expr fdc=2" >> ~/.vim/ftplugin/diff.vim

Now just:

diff -r dir1 dir2 | vim -

You can hit zo and zc to open and close folds. To get out of Vim, hit :q<Enter>

share|improve this answer

Your Answer


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.