Chujun Song
Chujun Song

Reputation: 158

How to remove all files with same extension except one with rm

I want to remove all files with .o extension except the specific example.o, how can I do that with rm? Edit: Environment: zsh

Upvotes: 1

Views: 1998

Answers (2)

anubhava
anubhava

Reputation: 785058

In zsh, you may use KSH_GLOB that works as extglob of bash:

setopt KSH_GLOB

echo rm !(example).o

Other option is to use extended_glob with a slightly different globbing syntax:

setopt extended_glob

echo rm (^example).o

Where ^ is used for negation.

Once you're satisfied with the output, remove echo before rm.

Upvotes: 2

Raul Luna
Raul Luna

Reputation: 2046

Could be something like this?

 find -iname '*.o' -not -iname 'example.o' -execdir rm {} \;

Upvotes: 1

Related Questions