-   Linux - General (
-   -   listing directories containing files of certain type (

David the H. 08-01-2009 12:45 PM

listing directories containing files of certain type
I'm looking to create a list of directories that contain files of a certain type, such as .mp3 files. I've got it mostly working with this command:

find . -name "*.mp3" -printf "%h\n"|sort -u
The problem is that it also lists parent directories as well. For example, "./dira/dirb/dirc/song.mp3" would give me


I only want the last entry. Is there any simple way to filter out the unwanted ones?

Note that sometimes the parent directories can contain files as well, so I can't just remove partial duplicates from the list after it's compiled (i.e. there are times when ./dira/dirb would also be a legitimate hit).

colucix 08-01-2009 12:55 PM

I cannot reproduce this behaviour. It prints only the name of the directories actually containing mp3 files, even if they are two or three levels below the current dir:

$ find . -name "*.mp3"
$ find . -name "*.mp3" -printf "%h\n"

Can you provide more details about the actual content of the directories in the search path?

David the H. 08-01-2009 01:05 PM

Doh! Actually, I've just discovered that it is working the way I want.

After posting I took a closer look at the list I had generated and found that all the "duplicates" it listed actually do have matching files in them. My memory of what was in the upper directories was a bit off and I failed to notice that it was only listing some of the upper directories.

Forget I ever asked anything. :( :)

All times are GMT -5. The time now is 09:24 PM.