Coder Perfect

Find the files existing in one directory but not in the other [closed]

Problem

I’m attempting to locate files that exist in one directory but not in the other, and I’ve tried using the following command:

diff -q dir1 dir2

The issue with the preceding command is that it finds both files in dir1 but not in dir2 and files in dir2 but not in dir1.

I’m looking for files in directory 1, but not in directory 2.

Here’s a taste of what my data looks like.

dir1    dir2    dir3
1.txt   1.txt   1.txt
2.txt   3.txt   3.txt
5.txt   4.txt   5.txt
6.txt   7.txt   8.txt

Another thing that comes to mind is how can I use a single command to find files in dir1 but not in dir2 or dir3?

Asked by Error404

Solution #1

diff -r dir1 dir2 | grep dir1 | awk '{print $4}' > difference1.txt

Explanation:

Answered by asclepix

Solution #2

This should suffice:

diff -rq dir1 dir2

The following options are explained (from the diff(1) man page):

Answered by tokhi

Solution #3

comm -23 <(ls dir1 |sort) <(ls dir2|sort)

This command will return files in dir1 that are not in dir2.

Google ‘process substitution’ to learn more about the () sign.

Answered by plhn

Solution #4

Using find with md5sum and then diff is a nice approach to make this comparison.

Example:

Find all the files in the directory, then compute the md5 hash for each one and save it to a file:

find /dir1/ -type f -exec md5sum {} \; > dir1.txt

Apply the same steps to the other directory:

find /dir2/ -type f -exec md5sum {} \; > dir2.txt

Then use “diff” to compare the two files:

diff dir1.txt dir2.txt

When the two directories to be compared are not on the same machine and you need to ensure that the files in both folders are equal, this method comes in handy.

Using git is another viable option for completing the task.

git diff --no-index dir1/ dir2/

Best regards!

Answered by Adail Junior

Solution #5

Meld (http://meldmerge.org/) is an excellent tool for comparing directories and their contents.

Answered by Catalin Hritcu

Post is based on https://stackoverflow.com/questions/16787916/find-the-files-existing-in-one-directory-but-not-in-the-other