4

How can I know how many directories (in current directory) have at least 1 file with a .mp3 extension.

No need recursion -- directory structure from current dir is for example:

1/blabla.mp3
2
3/something.mp3
4
5

The command should return that there are 2 dirs with mp3 file(s).

4
  • 2
    Is this homework? :p Commented Feb 17, 2017 at 22:20
  • Can't this question be a legit one with purposes besides homework? :) I use this to know how many users on my website uploaded mp3's. Commented Feb 17, 2017 at 22:21
  • Yes, I was mostly kidding. The last line read to me like a school assignment :) Commented Feb 17, 2017 at 22:22
  • haha yeah, tried to word it as straight forward as I could :p Commented Feb 17, 2017 at 22:23

2 Answers 2

7
find . -type f -name "*.mp3" -exec dirname {} \; | uniq | wc -l
0
find . ! -name . -prune -type d -exec sh -c '
   yes | head -1 |
   find "$1/." ! -name . -prune -type f \
      \( -name '*.[mM][pP]3' -o -name '.*.[mM][pP]3' \) \
      -exec sh -c "read foo" \; -print
' {} {} \; | wc -l |
sh -c 'read n;echo "There are $n dirs with mp3 files."'

The outer "find" selects all the subdirs in the current dir. and then descends in each of these and finds non-recursively in them if they contain atleast 1 mp3 file including hidden and in a case insensitive manner.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.