I'm trying to find the files existing in one directory but not in the other, I tried to use this command:
diff -q dir1 dir2
The problem with the above command that it finds both the files in dir1
but not in dir2
as well as the files in dir2
but not in dir1
,
I am trying to find the files in dir1
but not in dir2
only.
Here's a small sample 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 question on my mind is how can I find the files in dir1
but not in dir2
or dir3
in a single command?
This is the bash script to print commands for syncing two directories
This should do the job:
Options explained (via diff(1) man page):
-r
- Recursively compare any subdirectories found.-q
- Output only whether files differ.Unsatisfied with all the replies, since most of them work very slowly and produce unnecessarily long output for large directories, I wrote my own Python script to compare two folders.
Unlike many other solutions, it doesn't compare contents of the files. Also it doesn't go inside subdirectories which are missing in another directory. So the output is quite concise and the script works fast.
Sample usage:
Or if you want to see only files from the first directory:
P.S. If you need to compare file sizes and file hashes for potential changes, I published an updated script here: https://gist.github.com/amakukha/f489cbde2afd32817f8e866cf4abe779
This answer optimizes one of the suggestions from @Adail-Junior by adding the
-D
option, which is helpful when neither of the directories being compared are git repositories:If you use
-D
then you won't see comparisons to/dev/null
:text Binary files a/whatever and /dev/null differ
Explanation:
diff -r dir1 dir2
shows which files are only in dir1 and those only in dir2 and also the changes of the files present in both directories if any.diff -r dir1 dir2 | grep dir1
shows which files are only in dir1awk
to print only filename.vim's DirDiff plugin is another very useful tool for comparing directories.
It not only lists which files are different between the directories, but also allows you to inspect/modify with vimdiff the files that are different.