How can I delete directories using the bash that do not contain directories named wav or mp3? I use macOS Sierra.

find . -type d \! -exec test -e '{}/wav' \; -print finds the directories not containing wav directories. How can I include mp3 to this command? And how can I delete the resulting directories?

My music library follows this structure: /Musik/<Artist>/<format>/<Artist---Album>/<Track_Titel.wav>, where format is wav or mp3. There are many directories without any audio file but covers, e.g. Thus I can not just search for empty directories to delete those directories not containing audio files.

  • 2
    Terminology point: Directories cannot contain strings. Directories can have names which contain strings, or they can contain text files which contain strings, or they can contain files/directories whose names contain strings. But a string is not a thing which a directory can contain. </pedantry>
    – Wildcard
    Nov 5, 2016 at 2:14
  • Also, it seems that bash is your shell, at which you can run commands to do things, like find and remove directories. Unless you have some need to use bash features for this task, you're simply looking for the right tools to do a certain job.
    – Jeff Schaller
    Nov 5, 2016 at 2:16
  • lol wav, learn to flac. Nov 5, 2016 at 2:20

2 Answers 2


First of all, before you do this, make a backup of your files.


To find and remove Artist directories (and their contents, recursively) which do not directly contain directories (or files) titled either wav or mp3 (case sensitive), try the following:

find /Musik -mindepth 1 -maxdepth 1 -exec test \! -e {}/wav \; -exec test \! -e {}/mp3 \; -print

Only after you have confirmed the output matches the directories you expect to be deleted (and double checked your backup) should you then run:

find /Musik -mindepth 1 -maxdepth 1 -exec test \! -e {}/wav \; -exec test \! -e {}/mp3 \; -exec rm -rf {} \;

For 2 tests run test twice, and if both of them failed then do something:

find . -type d -exec bash -c "test -f \"{}\"/*wav || test -f \"{}\"/*mp3 || test -d \"{}\"/* || rm -rf \"{}\"" \;
  • Never embed {} in the shell code. It's easy enough to pass the filename safely; see here for an example.
    – Wildcard
    Nov 5, 2016 at 2:17
  • Better, but the request was about directories titled wav or mp3. Since this runs recursively but your existence check isn't recursive, it will delete his entire music directory (since Musik isn't expected to directly contain mp3 or wav files.)
    – Wildcard
    Nov 5, 2016 at 2:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .