Ask Different is a question and answer site for power users of Apple hardware and software. Join them; it only takes a minute:

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 need a script to delete a folder, only if the folder does not contain media files with extension avi or mp4.

share|improve this question
What type of script do you want to write? AppleScript, Automator workflow, or shell script? – Graham Miln Mar 5 '14 at 8:20
This question appears to be off-topic because it is about shell scripting and not specific to Apple software or hardware. This question would be better answered on superuser or another Stack Exchange site. – Graham Miln Mar 5 '14 at 12:01
I'd say its fine here whatever script is called for. Our users might be more likely to answer with other options but we'd only migrate if the op asked. – bmike Mar 5 '14 at 12:30

If all folders to be removed are directly under the containing folder and all mp4 and avi files are directly under the kept folders:

for d in */;do ls "$d"|grep -Eq '.*\.(mp4|avi)$'||echo rm -r "$d";done

If the mp4 and avi files can be in subfolders of the kept folders:

for d in */;do [[ $(find "$d" -iname \*.mp4 -o -iname \*.avi) ]]||echo rm -r "$d";done

If the folders to be removed can be in subfolders of the containing folder:

find . -type d|while read d;do ls "$d"|grep -Eq '.*\.(mp4|avi)$'||echo rm -r "$d";done

share|improve this answer

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.