Differences between revisions 2 and 6 (spanning 4 versions)
Revision 2 as of 2007-12-13 21:06:11
Size: 854
Editor: GreyCat
Comment: more on globs, including link
Revision 6 as of 2009-09-15 17:44:30
Size: 1350
Editor: GreyCat
Comment: rm -- *glob*
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
[[Anchor(faq16)]]
== How can I use a logical AND in a shell pattern (glob)? ==
<<Anchor(faq16)>>
== How can I use a logical AND/OR/NOT in a shell pattern (glob)? ==
[[glob|"Globs"]] are simple patterns that can be used to match filenames or strings. They're generally not very powerful. If you need more power, there are a few options available.
Line 4: Line 5:
[:glob:"Globs"] are simple patterns that can be used to match filenames or strings. They're generally not very powerful. If you need more power, you can use the !() extglob operator. You'll need {{{extglob}}} set. It can be checked with: If you want to operate on all the files that match glob A ''or'' glob B, just put them both on the same command line:
Line 6: Line 8:
$ shopt extglob rm -- *.bak *.old
}}}

If you want to use a logical OR in just part of a glob (larger than a single charcter -- for which square-bracketed character classes suffice), in Bash, you can use BraceExpansion:

{{{
rm -- *.{bak,old}
}}}

If you need something still more general/powerful, in KornShell or [[BASH]] you can use [[glob|extended globs]]. In Bash, you'll need the {{{extglob}}} option to be set. It can be checked with:

{{{
shopt extglob
Line 11: Line 25:
$ shopt -s extglob shopt -s extglob
Line 16: Line 30:
$ mv foo!(*.d) foo_thursday.d mv foo!(*.d) foo_thursday.d
Line 19: Line 33:
For the general case:

D
elete all files containing Pink_Floyd AND not containing The_Final_Cut:
A more complex example -- delete all files containing Pink_Floyd AND not containing The_Final_Cut:
Line 24: Line 36:
$ rm !(!(*Pink_Floyd*)|*The_Final_Cut*) rm !(!(*Pink_Floyd*)|*The_Final_Cut*)
Line 27: Line 39:
By the way: these kind of patterns can be used with KornShell and KornShell93, too. They don't have to be enabled there, but are the default patterns. By the way: these kind of patterns can be used with the KornShell, too. They don't have to be enabled there, but are the default patterns.

How can I use a logical AND/OR/NOT in a shell pattern (glob)?

"Globs" are simple patterns that can be used to match filenames or strings. They're generally not very powerful. If you need more power, there are a few options available.

If you want to operate on all the files that match glob A or glob B, just put them both on the same command line:

rm -- *.bak *.old

If you want to use a logical OR in just part of a glob (larger than a single charcter -- for which square-bracketed character classes suffice), in Bash, you can use BraceExpansion:

rm -- *.{bak,old}

If you need something still more general/powerful, in KornShell or BASH you can use extended globs. In Bash, you'll need the extglob option to be set. It can be checked with:

shopt extglob

and set with:

shopt -s extglob

To warm up, we'll move all files starting with foo AND not ending with .d to directory foo_thursday.d:

mv foo!(*.d) foo_thursday.d

A more complex example -- delete all files containing Pink_Floyd AND not containing The_Final_Cut:

rm !(!(*Pink_Floyd*)|*The_Final_Cut*)

By the way: these kind of patterns can be used with the KornShell, too. They don't have to be enabled there, but are the default patterns.

BashFAQ/016 (last edited 2015-03-05 00:31:02 by izabera)