I'm on Linux, using Bash.

I need to remove all files without a certain suffix, for example "dist". I know there are some tricks to do it, rather than removing all the unwanted files one by one.

link|improve this question

33% accept rate
feedback

2 Answers

Use the following to list all the files that would be removed (just to be sure):

find /path/to/directory -mindepth 1 ! -name "*dist"

Add -type f if you only want to delete regular files, not symbolic links or directories.


Run the command with additional -delete argument at the end to remove them.

link|improve this answer
@slhck Thanks, don't know what I was thinking (probably not much) – Daniel Beck Nov 15 '11 at 11:23
feedback

If you have extended globbing enabled, you can try:

rm !(*.dist)

Note: if you have directories without the .dist extension, this will will try and delete them too, but will fail.

To turn on extended globbing use: shopt -s extglob

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.