Shell globs: Difference between revisions

From Helpful
Jump to navigation Jump to search
mNo edit summary
mNo edit summary
 
(8 intermediate revisions by the same user not shown)
Line 1: Line 1:
<!--
{{#addbodyclass:tag_tech}}


globs (sometimes ''shell globs, because they are mainly seen in command lines), is associated with certain kinds of 'match this pattern'.
Globs, is associated with certain kinds of 'match this pattern'.
: also called ''shell globs'', because they are mainly seen in command lines


...very simple ones, usually only:
Compare with [[regular expressions]], which are more powerful (but still single-string) ways of expressing patterns.
: {{inlinecode|?}} meaning ''one'' character of anything, and
: {{inlinecode|*}} meaning ''any'' amount of characters of anything
:: ...anything except the directory separateor, {{inlinecode|/}} {{(or presumably {{inlinecode|\}} if imitated in windows)}}




see also fnmatch(), which is a "does a single name match this glob"
whereas glob() both walks your filesystem and returns ''all'' matching filenames
: apparently fnmatch() is ''used'' in the glob() implementation




Shell globs match fairly simple patterns. you can only use:
: {{inlinecode|?}} meaning ''one'' character of anything, and
: {{inlinecode|*}} meaning ''any'' amount of characters of anything
:: ...anything except the directory separateor, {{inlinecode|/}} {{(or presumably {{inlinecode|\}} if imitated in windows)}}
: {{inlinecode|[chars]}} meaning any one of the characters in this set
:: can also be used to match a literal ? or *
: {{inlinecode|[!chars]}} meaning anything other than the characters in this set
:: I've seen [^chars] manage the same, though this isn't ''quite'' standard [https://pubs.opengroup.org/onlinepubs/007904975/utilities/xcu_chap02.html#tag_02_13_01]


Compare with [[regular expressions]], which are more powerful (but still single-string) ways of expressing patterns.


That said:
Some people, when confronted with a problem, think "I know, I'll use regular expressions."
Now they have two problems. [https://blog.codinghorror.com/regular-expressions-now-you-have-two-problems/]


If you have both a fnmatch and glob function:
* {{search|C fnmatch|fnmatch()}} is a "matches a single given string with this pattern"
:: (probably nothing related to the filesystem unless you do it yourself)


-->
* {{search|C glob|glob()}} is likely to read directory entries from your filesystem and returns ''all'' matching filenames
:: apparently fnmatch() is ''used'' in the glob() implementation

Latest revision as of 00:32, 21 April 2024

Globs, is associated with certain kinds of 'match this pattern'.

also called shell globs, because they are mainly seen in command lines

Compare with regular expressions, which are more powerful (but still single-string) ways of expressing patterns.



Shell globs match fairly simple patterns. you can only use:

? meaning one character of anything, and
* meaning any amount of characters of anything
...anything except the directory separateor, / {{(or presumably \ if imitated in windows)}}
[chars] meaning any one of the characters in this set
can also be used to match a literal ? or *
[!chars] meaning anything other than the characters in this set
I've seen [^chars] manage the same, though this isn't quite standard [1]


If you have both a fnmatch and glob function:

  • fnmatch() is a "matches a single given string with this pattern"
(probably nothing related to the filesystem unless you do it yourself)
  • glob() is likely to read directory entries from your filesystem and returns all matching filenames
apparently fnmatch() is used in the glob() implementation