Linux – How to find the files existing in one directory but not in the other directory?

Let's consider a case where we have two directories, say d1 and d2, and both these directories contain some files, which may be the same or different. Now we want to list out the names of those files that are present in one of these directories (say d1) and not present in the other directory (say d2).

In order to do that we must be familiar with either the diff command or the comm command, as both these commands can be used to solve the above problem.

Using the diff Command

Let's first explore the diff command, which is short for difference. This command is used to find the difference between two files as it compares both the files line by line. When used with the -r flag, it can recursively compare directories.

Example Setup

Consider the terminal output to depict these two directories shown below:

immukul@192 linux-questions-code % ls -ltr
total 0
drwxr-xr-x 5 immukul staff 160 Jul 3 20:03 d1
drwxr-xr-x 4 immukul staff 128 Jul 3 20:03 d2

The contents of the first directory d1 looks like this:

immukul@192 d1 % ls -ltr
total 0
-rw-r--r-- 1 immukul staff 0 Jul 3 20:03 1.txt
-rw-r--r-- 1 immukul staff 0 Jul 3 20:03 3.txt
-rw-r--r-- 1 immukul staff 0 Jul 3 20:03 5.txt

The contents of the second directory d2 looks like this:

immukul@192 d2 % ls -ltr
total 0
-rw-r--r-- 1 immukul staff 0 Jul 3 20:03 2.txt
-rw-r--r-- 1 immukul staff 0 Jul 3 20:03 3.txt

Finding Files in d1 but not in d2

To find files present in the first directory but not in the second directory, use the following command:

diff -r d1 d2 | grep d1 | awk '{print $4}'

Let's break down this command:

  • The diff -r d1 d2 command shows the files present in d1 and not in d2 along with the changes of the files present in both directories.

  • The grep d1 command filters the output to show only lines containing "d1".

  • The awk '{print $4}' command extracts and prints the filename from the fourth column of the output.

immukul@192 linux-questions-code % diff -r d1 d2 | grep d1 | awk '{print $4}'
1.txt
5.txt

Using the comm Command

We can also achieve the same output with the help of the comm command. The comm command compares two sorted files line by line and produces three columns of output: lines unique to file1, lines unique to file2, and lines common to both files.

comm -23 <(ls d1 | sort) <(ls d2 | sort)

In this command:

  • -23 suppresses columns 2 and 3, showing only lines unique to the first file (d1)

  • <(ls d1 | sort) creates a sorted list of files in directory d1

  • <(ls d2 | sort) creates a sorted list of files in directory d2

immukul@192 linux-questions-code % comm -23 <(ls d1 | sort) <(ls d2 | sort)
1.txt
5.txt

Alternative Methods

You can also use other approaches like combining find with test conditions:

find d1 -type f -exec basename {} \; | while read file; do
  [ ! -f "d2/$file" ] && echo "$file"
done

Or using ls with grep:

ls d1 | grep -v -F -x -f <(ls d2)

Conclusion

Both diff and comm commands provide effective ways to find files existing in one directory but not in another. The comm command is generally more efficient for simple file listing comparisons, while diff provides more detailed information about differences between directories.

Updated on: 2026-03-17T09:01:38+05:30

953 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements