0

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.

  • commands like : comm -23 <(ls dir1 |sort) <(ls dir2|sort) will display the files which are not present in either dir. – Hara Prasad Jul 27 '16 at 09:13
  • 1
    SO guidelines say that you should show us what you've tried so far, and how you've come up short (or the error it gives you). Make a best effort first, please. – SaintHax Jul 27 '16 at 09:16
  • This? [difference between 2 directories in linux](http://stackoverflow.com/q/16787916/1983854). – fedorqui Jul 27 '16 at 09:17
  • I have tried diff command .. like diff -qr dir1/ dir2/ . it displays the output like: Only in dir1/: abc.txt Only in dir1/: abf.txt Only in dir2/: abg.txt Only in dir2/: abh.txt Only in dir2/: abi.txt – Hara Prasad Jul 27 '16 at 09:20
  • @fedorqui have looked the page.. commands are not giving the desired result. – Hara Prasad Jul 27 '16 at 09:22
  • May be good to [edit] your question to show what exactly you have tried, together with the desired output. Check [ask]. – fedorqui Jul 27 '16 at 09:31

1 Answers1

0

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 :-)

Pintu
  • 278
  • 1
  • 6
  • Thanks @Pintu .. your command displays: diff -r dir1/abd.txt dir2/abd.txt and diff -r dir1/abe.txt dir2/abe.txt, which is ok. but is there any way to get only the file names? – Hara Prasad Jul 27 '16 at 09:30
  • You can pipe the outcome with cut command. `diff -r dir1 dir2 | grep "diff -r" | cut -d " " -f3 | xargs basename`. This should help. – Pintu Jul 27 '16 at 10:09
  • `diff -r dir1 dir2 | grep "diff -r" | cut -d " " -f3 | xargs basename -a` is the correct one.. Thanks @Pintu again. – Hara Prasad Jul 31 '16 at 17:57