Bash: Find Files that Exist in One Directory But Not in Another


Often you may want to find all files that exist in one directory but not in another directory in Bash.

You can use the following syntax to do so:

diff -r $dir1 $dir2 | grep $dir1 | awk '{ print $4 }'

This particular example will return all files that exist in the directory specified by the $dir1 variable but not in the directory specified by the $dir2 variable.

Here is what the various commands do:

  • diff -r: Recursively return differences between the two directories
  • grep $dir1: Only display lines in output that contain the path specified by $dir1
  • awk ‘{ print $4 }’ : Only print the fourth column in the output, which contains the filename only

The following example shows how to use this syntax in practice.

Example: Find Files that Exist in One Directory But Not in Another in Bash

Suppose that we have the following two directories on our computer:

  • c:/users/bobbi/old_data
  • c:/users/bobbi/new_data

Suppose that we would like to find all files that exist in the old_data directory but not in the new_data directory.

We can use the following syntax to first view the filenames in each directory:

dir1="c:/users/bobbi/old_data"
dir2="c:/users/bobbi/new_data"

ls $dir1
ls $dir2

The following screenshot shows how to use this syntax in practice:

We can then use the following syntax to find the files that only exist in the old_data directory and not in the new_data directory:

diff -r $dir1 $dir2 | grep $dir1 | awk '{ print $4 }'

The following screenshot shows how to use this syntax in practice:

Bash find files that exist in one directory but not in another

Notice that this returns the following two filenames:

  • coaches.xlsx
  • owners.xlsx

By viewing the output from the ls commands, we can confirm that these two files exist only in the old_data directory and not in the new_data directory.

If you’d like, you can also use the > command to save the names of these files to a new file:

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

This particular example will find all filenames that exist in the old_data directory and not the new_data directory, then save the results to a new file named only_old_data.txt.

Related Tutorials

The following tutorials explain how to perform other common tasks in Bash:

Bash: How to List All Files in Folder by Date
Bash: How to List All Files in Folder by Size
Bash: How to Delete All Files in Directory

Leave a Reply