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.

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.

Thanks

share|improve this question

migrated from stackoverflow.com Oct 24 '12 at 12:02

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

2  
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

1 Answer 1

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

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.