1

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

Graham Miln
  • 43,776
Edgard
  • 11
  • 1
    What type of script do you want to write? AppleScript, Automator workflow, or shell script? – Graham Miln Mar 05 '14 at 08: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 05 '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 05 '14 at 12:30

1 Answers1

1

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

Lri
  • 105,117