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).

  • 2
    Is this homework? :p – Eli Heady Feb 17 '17 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. – Dan P. Feb 17 '17 at 22:21
  • Yes, I was mostly kidding. The last line read to me like a school assignment :) – Eli Heady Feb 17 '17 at 22:22
  • haha yeah, tried to word it as straight forward as I could :p – Dan P. Feb 17 '17 at 22:23
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.

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.