Differences between revisions 3 and 8 (spanning 5 versions)
Revision 3 as of 2008-05-08 23:58:22
Size: 936
Editor: GreyCat
Comment: clean up
Revision 8 as of 2015-03-05 00:31:02
Size: 1469
Editor: izabera
Comment: syntax hl
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 ''extended globs''. In [:BASH:], you'll need the {{{extglob}}} option to be set. It can be checked with:
{{{
$ shopt extglob
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:

{{{#!highlight bash
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:

{{{#!highlight bash
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:

{{{#!highlight bash
shopt extglob
Line 10: Line 24:
{{{
$ shopt -s extglob
{{{#!highlight bash
shopt -s extglob
Line 15: Line 29:
{{{
$ mv foo!(*.d) foo_thursday.d
{{{#!highlight bash
mv foo!(*.d) foo_thursday.d
Line 21: Line 35:
{{{
$ rm !(!(*Pink_Floyd*)|*The_Final_Cut*)
{{{#!highlight bash
rm !(!(*Pink_Floyd*)|*The_Final_Cut*)
Line 27: Line 41:
For a more thorough explanation of extended globs, see [:glob:]. ----
CategoryShell

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:

   1 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:

   1 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:

   1 shopt extglob

and set with:

   1 shopt -s extglob

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

   1 mv foo!(*.d) foo_thursday.d

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

   1 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.


CategoryShell

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