Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I am using Mountain Lion on a Mac. I want to delete all the files in a directory which do NOT end with .psd, .jpg, or .nef using Terminal. How would I do this? Thanks in advance!

share|improve this question

1 Answer 1

up vote 1 down vote accepted

If you use Bash, you can use extended globbing functions:

shopt -s extglob

Then, to delete everything but .nef, .jpg or .psd files:

rm !(*.nef|*.jpg|*.psd)

You can test it by using ls instead of rm. Here, the ! inverts the match. Note that this matches directories as well, but they cannot be removed without passing the -r option to rm.


Another way with find:

find . -maxdepth 1 -type f \
! \( -name '*.nef' -or -name '*.psd' -or -name '*.jpg' \) -delete

Test this command by removing the -delete option first. Here, the ! negates the expression in parentheses.

share|improve this answer

Your Answer

 
discard

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.