shell commands - exclusion

Lars Eighner luvbeastie at larseighner.com
Wed Feb 4 07:27:12 PST 2009


On Wed, 4 Feb 2009, t-u-t wrote:

> hi, i don't know if this is a freak question, but i was looking around to
> see if this is possible, and what the convention would be.
>
> if i have say one (or even two) single file/directories among many others,
> and i want to perform any said function like cp, mv, rm, etc.. , to all
> other files except that one or two, is there a way to do that in a single
> command?
> e.g
> rm -r * {-except foo1 foo15}

In general this is not possible.  A few commands have exclusion options, but
not many.  Some shells have ways of managing glob exclusion (it's the shell
that expands wildcard patterns).  Setting GLOBIGNORE works in BASH, whether
something similar works in others, you will have to investigate yourself. 
But that isn't one line as you have to set GLOBIGNORE.  BASH also has an
extended pattern matching option which includes negation, so you might want
to look into that.

> and if there is, could the same be applied to other similar batch (?)
> operations, like pkg_delete -f "*" { except firefox3 wine thunderbird }
> etc..

pkg_delete can take regular expression arguments (see -x).  Perhaps you
can devise one that will do the trick.  Beware, however: it can take
multiple regular expressions and deletes package which match ANY (not all)
of them.

> i'm a bit new to the shell (took me a while to figure out *ls* and *ls |
> more*), but i can't find anything from google cuz i don't know what this
> would be called in the first place.

Shell globbing is the operation by which the shell expands wildcards and
finds matches.  What you want to do exclude things from shell globbing.

> otherwise is it better to protect them with chflags or other trickery?

watch out anything involving recursion --- things can happen that you don't
expect unless you really know what you are doing.

-- 
Lars Eighner
http://www.larseighner.com/index.html
8800 N IH35 APT 1191 AUSTIN TX 78753-5266



More information about the freebsd-questions mailing list