Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I would like to set up a simple filtering system on my FreeBSD server that allows me to create arbitrary "views" of directories.

For instance, I'd like to be able to list all directories that match the pattern "*.mp3" but only display the directory name.

For instance, if I ran the command on my music folder I would like to be able to show all directories that have mp3s in them in one command, and all directories that have flac files in them as a separate command.

The command find . -name "*.mp3" almost does what I want but it displays one entry for each file. Is there a way to limit find to one result per directory?

share|improve this question
add comment

2 Answers

up vote 2 down vote accepted

Use this script:

find / -name "*.mp3" | grep -o '.*/' | sort | uniq > mp3files
share|improve this answer
    
Perfect, thanks. I prefer avoiding xargs if possible for clarity of command. –  javanix Nov 14 '11 at 18:49
add comment

Hows about:

find . -name '*.mp3' -print0 |xargs -0 -i dirname {} |uniq

I get something like:

./mnt/mp3/Adicts/Complete Singles Collection(1995)

./mnt/mp3/Adicts/Rise and Shine(2002)

./mnt/mp3/Adicts/Songs of Praise(1981)

./mnt/mp3/Adicts/Twenty-seven(1992)

./mnt/mp3/Adicts/Ultimate Addiction

./mnt/mp3/Adicts/Very Best Of Adicts(1998)

share|improve this answer
2  
Shorter on Linux: find . -name '*.mp3' -printf '%h\n' | uniq Unfortunately I don't know if FreeBSD's find has -printf. –  ephemient Nov 10 '11 at 22:36
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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