4

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.

10

With GNU tools:

find . -name '*.lib' -print0 | awk -v RS='\0' -F/ '! seen[$NF]++'
  • This gives me the error seen[: Event not found error – Thisaru Guruge Mar 15 '17 at 16:38
  • @ThisaruGuruge, sorry forgot about that other idiosyncrasy of csh. See edit. – Stéphane Chazelas Mar 15 '17 at 16:40
  • 2
    BTW: find … -printf '%f\0' should give just the file names, null-separated, with GNU find. – derobert Mar 15 '17 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. – Stéphane Chazelas Mar 15 '17 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 – Stéphane Chazelas Mar 16 '17 at 9:43
2

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 '17 at 8:52
  • Or -printf '%f\0' | sort -zu | tr '\0' '\n' – Stéphane Chazelas Mar 16 '17 at 9:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.