Super User is a question and answer site for computer enthusiasts and power users. 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 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
up vote 2 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

You must log in to answer this question.

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