MacOS – Terminal command to delete all images in folder and subfolders

macosterminal

I need a terminal command to delete all images (*.jpg, *.png, *.gif, *.JPG, *.PNG, *.GIF) stored in myfolder and its subfolders, preserving the folders structure. I do not need to move them to trash folder.

I guess that some regular expressions are needed, but this overcomes my skills.

Best Answer

To just list them (for review/verification):

find MYFOLDER -type f \( -iname '*.jpg' -o -iname '*.png' -o -iname '*.gif' \) -ls

To move them to the Trash:

find MYFOLDER -type f \( -iname '*.jpg' -o -iname '*.png' -o -iname '*.gif' \) -exec mv '{}' ~/.Trash/ \;

To delete them directly (no way to get them back unless you have a backup):

find MYFOLDER -type f \( -iname '*.jpg' -o -iname '*.png' -o -iname '*.gif' \) -delete