I need a script to delete a folder, only if the folder does not contain media files with extension avi or mp4.

  • 1
    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

You must log in to answer this question.

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