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

On Unix or Mac:

How do you delete all files NOT of a specific type. Or How to copy all files of a specific type with the containing folder.


share|improve this question

migrated from Oct 24 '12 at 12:02

This question came from our site for professional and enthusiast programmers.

I'm not sure how this question is related to programming, do you wish to achieve this functionality in a program you are developing? – ginja Oct 24 '12 at 11:26
up vote 1 down vote accepted

Use find, e.g. to delete all files which are not .txt files:

$ find . -type f -not -name \*.txt -exec rm -f {} \;

WARNING: use this kind of command with care as it's easy to delete a lot of files unintentionally. Do a "dry run" first if you're not confident, e.g. to preview the above command:

 $ find . -type f -not -name \*.txt -exec echo "Delete {}" \;
share|improve this answer

You must log in to answer this question.

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