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 a bit late but may help someone. Not sure if diff or rsync spit out just filenames in a bare format like this. Thanks to plhn for giving that nice solution which I expanded upon below.
If you want just the filenames so it's easy to just copy the files you need in a clean format, you can use the find command.
This assumes that both dir1 and dir2 are in the same parent folder. sed just removes the parent folder so you can compare apples with apples. The last sed just puts the dir1 name back.
If you just want files:
Similarly for directories:
GNU
grep
can inverse the search with the option-v
. This makesgrep
reporting the lines, which do not match. By this you can remove the files indir2
from the list of files indir1
.The options
-F -x
tellgrep
to perform a string search on the whole line.kdiff3 has a nice diff interface for files and directories.
It works on Windows, Linux, and macOS.
You can install it in multiple ways:
brew cask install kdiff3
This command will give you files those are in dir1 and not in dir2.
About
<( )
sign, you can google it as 'process substitution'.Meld (http://meldmerge.org/) does a great job at comparing directories and the files within.
A simplified way to compare 2 directories using the DIFF command
open filename.dat after the run is complete
and you will see: Only in filename.1: filename.2 Only in: directory_name: name_of_file1 Only in: directory_Name: name_of_file2