Hara Prasad Hara Prasad - 8 months ago 32
Bash Question

how to find filenames which are having different contents in two directories

I have two directories. dir1 and dir2.

In dir1 files are: abc.txt, abd.txt, abe.txt, abf.txt

In dir2 files are: abi.txt, abd.txt, abe.txt, abg.txt, abh.txt

abd.txt and abe.txt are present in both directories and are having different contents. How to write a script which will return only these two file name?
I dont want files which are missing in dir1 or dir2. I only need file names which has same names and are having different contents.

Thank you in advance.


One indirect way to do so is use diff command

diff -r dir1 dir2 | grep "diff -r"

diff -r dir1 dir2 will show differences of files with same name with filenames as diff -r dir1/file dir2/file. Rest of the files will be listed with prefix Only.

Hope someone suggests a simpler solution :-)