Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I was trying to find the folders in my music collection that do not contain any mp3 files (some have only album art, or playlists and the like). So I had something like this in a shell script.

for DIR in *
        [[ -d "${DIR}" ]] || continue
        cd "${DIR}"

        ls *.mp3 > /dev/null 2>&1
        if [[ "$?" != "0" ]]
                echo $DIR has no MP3 files!

        cd ..

Yet, even though there are many folders to go through, this script only checks the first one before exiting... why is this? It's as though the continue inside the if is breaking out of the loop entirely.

share|improve this question
up vote 3 down vote accepted

The continue statement inside the if block is the source of your problem:

If no MP3 files were found, cd .. doesn't get executed and the acondition [[ -d "${DIR}" ]] will most likely fail for all remaining directories.

share|improve this answer
Yup! You were exactly right! Can't believe I didn't catch that earlier. Thank you :) – Alex Reinking Jan 23 '14 at 22:37

Your Answer


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.