I want to find unique files inside a directory, which also have sub directories.

There are specific types of files, say .lib files.

There are same .lib file inside different sub directoris. I need to find the list of .lib files inside my home directory, but only unique names. Are there any method to do so ?

Currently I am using

find -name "*.lib" > lib_file_list

But it gives duplicate results as some of the .lib files are in multiple sub directories. I am using CSH.

2 Answers 2


With GNU tools:

find . -name '*.lib' -print0 | awk -v RS='\0' -F/ '! seen[$NF]++'
  • This gives me the error seen[: Event not found error
    – ThisaruG
    Mar 15, 2017 at 16:38
  • @ThisaruGuruge, sorry forgot about that other idiosyncrasy of csh. See edit. Mar 15, 2017 at 16:40
  • 2
    BTW: find … -printf '%f\0' should give just the file names, null-separated, with GNU find.
    – derobert
    Mar 15, 2017 at 17:29
  • @derobert, yes, but AFAICT, the OP stills wants the full path (one path if there are several paths with the same name) to be printed. Mar 15, 2017 at 19:14
  • 1
    @ThisaruGuruge, that's -printf, not -print. For the ./path/to/foo.lib file, -printf '%f\0' prints foo.lib<NUL> as opposed to ./path/to/foo.lib<NUL> with -print0 Mar 16, 2017 at 9:43

Here's what I would do:

find -name '*.lib' -printf '%f\n' | sort | uniq > lib_file_list

A few things to keep in mind:

  • this won't give you the full paths of the files (I assume this doesn't matter since your question requires elimination of some paths anyway)
  • it will fail if you have newlines in any of your filenames
  • and of course assuming GNU tools.
  • cool (+1) ... or sort -u
    – JJoao
    Mar 16, 2017 at 8:52
  • Or -printf '%f\0' | sort -zu | tr '\0' '\n' Mar 16, 2017 at 9:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .