Compare two directories for missing files in Linux

Curious Dev
Curious Dev Grail
Published in
2 min readAug 2, 2020

When you are working in a team there are chances that other than version control, you are using some common share (for eg. NFS Share) where you all are keeping certain content independently, but to same location. If you don’t copy your changes to that share carefully there is chances that you may override some latest changes done by other developer. And if the file newly created by you are large in numbers then handpicking manually will waste lots of productive time. In order to avoid that you can use the command comm to compare the content of two directories. Let me show you how.

comm <(ls dir1) <(ls dir2)

This will print the three column

  • First column designate the file unique in dir1
  • Second column will print file unique in dir2
  • Third column will print the files that are common in both the directory dir1 & dir2 .

To print only one or two column this command comes with three flag -1 , -2, -3 that can be used to skip printing one of the three column. For eg.:

To print file unique in Dir1

comm -23 <(ls dir1) <(ls dir2)

To print file unique in Dir2

comm -13 <(ls dir1) <(ls dir2)

To print file common in both directory

comm -12 <(ls dir1) <(ls dir2)

If it saves your day share with your fellow teammates.

--

--