Globbing is very present in Linux. If you're dealing with a command that handles files (which, under the everything is a file principle, is most commands), there is a good chance that you can use globbing. To give you an impression of this, consider the following examples:
reader@ubuntu:~/scripts/chapter_10$ cat *
eee
e2e
e e
aaa
a2a
a a
aabb
We can use this regular file for testing grep.
Regular expressions are pretty cool
Did you ever realise that in the UK they say colour,
but in the USA they use color (and realize)!
Also, New Zealand is pretty far away.
The cat command, combined with the wildcard glob pattern, prints the contents of all files in the current working directory. In this case, since all files are ASCII text, this was not really a problem. As you can see, the files are printed right after each other; there's not so much as an empty line in between.
Should you cat a binary file, your screen will look something like this:
reader@ubuntu:~/scripts/chapter_10$ cat /bin/chvt
@H!@8 @@@�888�� �� � H 88 8 �TTTDDP�td\\\llQ�tdR�td�� � /lib64/ld-linux-x86-64.so.2GNUGNU��H������)�!�@`��a*�K��9���X' Q��/9'~���C J
The worst case scenario is that the binary file contains a certain character sequence that makes temporary changes to your Bash shell, which will make it unusable (yes, this has happened to us many times). The lesson here should be simple: watch out when globbing!
Other commands we've seen up until now that can deal with globbing patterns include chmod, chown, mv, tar, grep, and so on. Perhaps the most interesting for now is grep. We've used regular expressions with grep on a single file, but we can also select files using a glob.
Let's look at the most ridiculous example of grep with globbing: finding anything in everything.
reader@ubuntu:~/scripts/chapter_10$ grep .* *
grep: ..: Is a directory
character-class.txt:eee
character-class.txt:e2e
character-class.txt:e e
character-class.txt:aaa
character-class.txt:a2a
character-class.txt:a a
character-class.txt:aabb
grep-file.txt:We can use this regular file for testing grep.
grep-file.txt:Regular expressions are pretty cool
grep-file.txt:Did you ever realise that in the UK they say colour,
grep-file.txt:but in the USA they use color (and realize)!
grep-file.txt:Also, New Zealand is pretty far away.
Here, we used the regular expression .* search pattern (anything, zero or more times) with the glob pattern of * (any file). As you might expect, this should match every line in every file.
When we use grep in this manner, it has pretty much the same functionality as the earlier cat *. However, when grep is used on multiple files, the output includes the filename (so you know where the line was found).