Skip to content
Advertisement

Find out missing files between two directories (missing file names, but not extension) in Linux

Say I have dir1/ that has 1.a 2.a 3.a 5.a and dir2/ has 1.b 2.b 3.b 4.b 5.b.

I wonder how can I ignore the extension, and find the missing file (4.b in this case).

I assume that diff command doesn’t work as I don’t see relevant argument.

Advertisement

Answer

You can use the basename command to strip the basename:

find dir1 -type f -exec basename '{}' .a ; | sort > list1
find dir2 -type f -exec basename '{}' .b ; | sort > list2

Then use diff list1 list2 to compare the two. Or maybe use comm instead of diff.

As an alternative to basename (which requires to explicitly specify .a and .b) you could also use sed to strip everything after the last dot:

ls dir1 | sed -e 's,.[^.]*$,,' | sort > list1
ls dir2 | sed -e 's,.[^.]*$,,' | sort > list2
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement