home icon contact icon rss icon

Textmode

tools I use

Shell: Deleting files with exclusion

How do you delete a bunch of files, while excluding specific matches?

For example, all files that are no jpeg images.

General way


ls -I "*.jpg"|xargs rm

ZSH, negate the pattern:


setopt extended_glob
rm ^*.jpg


rainer said

May 08, 2009 @ 10:45 AM

Hello,

One can use a similar way in bash, too:

shopt -s extglob rm !(*.jpg)

More Patterns are possible, you can read about them in the bash-manpage, search for “extglob”.

bye, rainer

rainer said

May 08, 2009 @ 10:52 AM

Hello, again,

The linebreaks in the above comment were screwed :-)

“shopt -s extglob” enables extendet pattern matching and “rm !(*.jpg)” then deletes everything but .jpg-files.

bye,

rainer

RSS feed for comments on this post

Leave a Comment