More on wildcards and special characters
? - (at the command level) matches a single character
? - (in a regular expression) matches zero or one occurrence of the preceding character
* - (at the command level) matches any sequence of characters (including the null sequence)
* - (in a regular expression) matches any number of occurrences of the preceding character
^ - (outside square brackets) matches beginning of line
^ - (inside square brackets) negates -- [^ab] matches neither a nor b
$ - (in a regular expression) -- matches end of line
$ - (elsewhere) refers to a named variable
. - (in a regular expression) matches any character
| command/output | explanation |
$ mkdir wild $ cd wild $ for i in a b c d e > do > for j in 1 2 3 4 > do > touch $i$j > done > done $ ls a1 a2 a3 a4 b1 b2 b3 b4 c1 c2 c3 c4 d1 d2 d3 d4 e1 e2 e3 e4 |
First let's use a loop to make some files in a new directory. |
$ ls a* a1 a2 a3 a4 $ ls >a1 $ ls -l >a2 $ ls a* >a3 $ ls *1 >a4 |
Let's fill the a* files with some content. Can you predict what the file sizes will be? |
$ wc a*
20 20 60 a1
21 182 1168 a2
4 4 12 a3
5 5 15 a4
50 211 1255 total
|
The numbers appearing on the left should all make sense. |
| command/output | explanation |
$ echo a. a. $ echo a? a1 a2 a3 a4 $ echo a* a1 a2 a3 a4 $ echo .1 .1 $ echo ?1 a1 b1 c1 d1 e1 $ echo *1 a1 b1 c1 d1 e1 |
A comparison of '.', '?', and '*' on the command line. Note that '.' does not match a character. |
$ grep -c a. a2 21 $ grep -c a a2 21 $ grep -c a$ a2 0 $ grep -c a.$ a2 4 $ grep -c a?$ a2 0 |
counting various strings with wildcards in the file a2=`ls -l` |
$ grep -c a a* a1:4 a2:21 a3:4 a4:1 $ grep -c a$ a* a1:0 a2:0 a3:0 a4:0 $ grep -c a.$ a* a1:4 a2:4 a3:4 a4:1 |
Counting various things in all files beginning with a |