5

Possible Duplicate:
Remove all files but one with rm

In unix, to remove zip files in a directory one can simply type

rm *.zip

How can one remove everything except zip files?

0

2 Answers 2

8
rm !(*.zip)

This appears to work. The ! represents "not".

extglob needs to be enabled in the shell behavior settings for this to work. It is probably already enabled... but if not, enable it with:

shopt -s extglob

And after answering this... indeed Ignacio Vazquez-Abrams you are correct. It is pretty much duplicate.

1
  • Thank you for adding the mention of extglob! I was able to run this command in the normal interactive prompt without a hitch, but then when trying something like echo !(*.zip) in a script, I would always get a syntax error. Adding that option fixed it! May 4, 2014 at 13:57
0

Try creating a list then removing the file from the list.

Ex.

ls $yourDir | grep -v .zip > deleteThis.list

the call

xargs rm < deleteThis.list

3
  • 3
    Why not just rm $(ls | grep -v *.zip) ? :)
    – user1931
    Jan 17, 2011 at 4:02
  • There's that too! :)
    – user61724
    Jan 17, 2011 at 16:28
  • 1
    @JohnT I know you posted that answer more than six years ago, but it helped me today. Thank you!!! Aug 17, 2017 at 20:17

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