个人工具

“UbuntuHelp:ShellGlobbing”的版本间的差异

来自Ubuntu中文

跳转至: 导航, 搜索
(新页面: {{From|https://help.ubuntu.com/community/ShellGlobbing}} {{Languages|UbuntuHelp:ShellGlobbing}} === Introduction === One of the best features of the shell is globbing. Globbing is the eq...)
 
第2行: 第2行:
 
{{Languages|UbuntuHelp:ShellGlobbing}}
 
{{Languages|UbuntuHelp:ShellGlobbing}}
 
=== Introduction ===
 
=== Introduction ===
 
 
One of the best features of the shell is globbing. Globbing is the
 
One of the best features of the shell is globbing. Globbing is the
 
equivalent of regular expressions of files and directories. As
 
equivalent of regular expressions of files and directories. As
 
everything else, it's probably best explained with an example:
 
everything else, it's probably best explained with an example:
 
 
<pre><nowiki>
 
<pre><nowiki>
 
$ ls
 
$ ls
 
code    devel        downloads  Pictures  src
 
code    devel        downloads  Pictures  src
 
cat.py database.py  gravity.py  insert.py  oldmaid.py
 
cat.py database.py  gravity.py  insert.py  oldmaid.py
 
 
$ ls *.py
 
$ ls *.py
 
cat.py database.py  gravity.py  insert.py  oldmaid.py
 
cat.py database.py  gravity.py  insert.py  oldmaid.py
 
</nowiki></pre>
 
</nowiki></pre>
 
 
 
=== The Basics ===
 
=== The Basics ===
 
 
In the shell, the * character means "match everything". This is the
 
In the shell, the * character means "match everything". This is the
 
most common globbing character. So, globbing can be defined has a set of character
 
most common globbing character. So, globbing can be defined has a set of character
 
that are expanded by the shell. Note, that the globbing characters are
 
that are expanded by the shell. Note, that the globbing characters are
 
expanded before executing a command. For example:
 
expanded before executing a command. For example:
 
 
<pre><nowiki>
 
<pre><nowiki>
 
$ ls -F
 
$ ls -F
第31行: 第24行:
 
-rf
 
-rf
 
</nowiki></pre>
 
</nowiki></pre>
 
 
Here, you see that the directory contains two sub-directories, 'foo'
 
Here, you see that the directory contains two sub-directories, 'foo'
 
and 'bar'. The directory also contains a file named '-rf'. The '*' in
 
and 'bar'. The directory also contains a file named '-rf'. The '*' in
 
the second command, 'rm', is expanded to:
 
the second command, 'rm', is expanded to:
 
 
<pre><nowiki>
 
<pre><nowiki>
 
$ rm bar baz foo -rf tata tintin
 
$ rm bar baz foo -rf tata tintin
 
</nowiki></pre>
 
</nowiki></pre>
 
 
Now, you probably know about the '-rf' options of 'rm'. They make
 
Now, you probably know about the '-rf' options of 'rm'. They make
 
'rm' delete all the directories and their contents. So, why the file
 
'rm' delete all the directories and their contents. So, why the file
第47行: 第37行:
 
which make most commands stop processing options, the result would had
 
which make most commands stop processing options, the result would had
 
been different:
 
been different:
 
 
<pre><nowiki>
 
<pre><nowiki>
 
$ ls -F
 
$ ls -F
第57行: 第46行:
 
bar  baz
 
bar  baz
 
</nowiki></pre>
 
</nowiki></pre>
 
 
=== Globbing characters ===
 
=== Globbing characters ===
 
 
There is other characters other than *. Here a list of the
 
There is other characters other than *. Here a list of the
 
globbing characters, that are available in most shells:
 
globbing characters, that are available in most shells:
 
+
* <code><nowiki>?</nowiki></code>: matches any single character, regardless what the character is.
** <code><nowiki>?</nowiki></code>: matches any single character, regardless what the character is.
+
 
<pre><nowiki>
 
<pre><nowiki>
 
$ ls
 
$ ls
第88行: 第74行:
 
$ ls {bar,foo[0-9]}
 
$ ls {bar,foo[0-9]}
 
bar foo1 foo2 foo3</nowiki></pre>
 
bar foo1 foo2 foo3</nowiki></pre>
 
 
=== Conclusion ===
 
=== Conclusion ===
 
 
There's many other globbing characters, but they are often specific to
 
There's many other globbing characters, but they are often specific to
 
certain shells. I encourage you to read the section in the manual of
 
certain shells. I encourage you to read the section in the manual of

2007年11月30日 (五) 21:29的版本

Introduction

One of the best features of the shell is globbing. Globbing is the equivalent of regular expressions of files and directories. As everything else, it's probably best explained with an example:

$ ls
code    devel        downloads   Pictures   src
cat.py	database.py  gravity.py  insert.py  oldmaid.py
$ ls *.py
cat.py	database.py  gravity.py  insert.py  oldmaid.py

The Basics

In the shell, the * character means "match everything". This is the most common globbing character. So, globbing can be defined has a set of character that are expanded by the shell. Note, that the globbing characters are expanded before executing a command. For example:

$ ls -F
bar/  baz/  foo  -rf  tata  tintin
$ rm *
$ ls
-rf

Here, you see that the directory contains two sub-directories, 'foo' and 'bar'. The directory also contains a file named '-rf'. The '*' in the second command, 'rm', is expanded to:

$ rm bar baz foo -rf tata tintin

Now, you probably know about the '-rf' options of 'rm'. They make 'rm' delete all the directories and their contents. So, why the file '-rf' is still present after the command as been executed? If you look carefully the '-rf' file was never given to 'rm', because 'rm' treated '-rf' as an option. Alternatively, if you had given the '--' option, which make most commands stop processing options, the result would had been different:

$ ls -F
bar/  baz/  foo  -rf  tata  tintin
$ rm -- *
rm: cannot remove `bar': Is a directory
rm: cannot remove `baz': Is a directory
$ ls
bar   baz

Globbing characters

There is other characters other than *. Here a list of the globbing characters, that are available in most shells:

  • ?: matches any single character, regardless what the character is.
$ ls
bar baz bzr
$ ls b?r
bar bzr
  • [...]: matches any character inside the brackets. The characters can be either be a range of character or the

character itself. For example, [a-z] would matches all the lowercase letters through 'a' to 'z'.

$ ls
bar foo FOO
$ ls [A-Z]
FOO
  • [!...]: matches all the characters that are not in the backrets. This is analogous to the caret (^) character used in regular expressions.
$ ls
bar foo FOO
$ ls [!A-Z]
bar foo
  • {p1,p2,...} : matches all the patterns described in the brackets. They can be any of above globbing patterns or specific characters.
$ ls
bar bar2 bar3 baz foo1 foo2 foo3
$ ls {bar,foo[0-9]}
bar foo1 foo2 foo3

Conclusion

There's many other globbing characters, but they are often specific to certain shells. I encourage you to read the section in the manual of your shell about globbing for more information.