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?
The accepted answer will also list the files that exist in both directories, but have different content. To list ONLY the files that exist in dir1 you can use:
Explanation:
A good way to do this comparison is to use
find
withmd5sum
, then adiff
.Example:
Use
find
to list all the files in the directory then calculate the md5 hash for each file and pipe it to a file:Do the same procedure to the another directory:
Then compare the result two files with "diff":
This strategy is very useful when the two directories to be compared are not in the same machine and you need to make sure that the files are equal in both directories.
Another good way to do the job is using git
Best regards!
Another (maybe faster for large directories) approach:
The
sed
command removes the first directory component thanks to Erik`s post)