Linux file globbing
Introduction
The shell is also responsible for file globbing (or dynamic filename generation). In this session, we have covered the Linux file globbing.
asterisk(*)
The asterisk * is interpreted by the shell as a sign to generate filenames, matching the asterisk to any combination of characters (even none). When no path is given, the shell will use filenames in the current directory. See the man page of glob(7) for more information. (This is part of LPI topic 1.103.3.)
question mark(?)
Similar to the asterisk, the question mark ? is interpreted by the shell as a sign to generate filenames, matching the question mark with exactly one character.
square brackets ( [] )
The square bracket [ is interpreted by the shell as a sign to generate filenames, matching any of the characters between [ and the first subsequent ]. The order in this list between the brackets is not important. Each pair of brackets is replaced by exactly one character.
You can also exclude characters from a list between square brackets with the exclamation mark !. And you are allowed to make combinations of these wild cards.
a-z and 0-9 ranges
The bash shell will also understand ranges of characters between brackets.
$LANG and square brackets
But, don't forget the influence of the LANG variable. Some languages include lower case letters in an upper case range (and vice versa).
If $LC_ALL is set, then this will also need to be reset to prevent file globbing.
preventing file globbing
The screenshot below should be no surprise. The echo * will echo a * when in an empty directory. And it will echo the names of all files when the directory is not empty.
Globbing can be prevented using quotes or by escaping the special characters, as shown in this screenshot.
Exercise, Practice and Solution:
1. Create a test directory and enter it.
Code:
2. Create files file1 file10 file11 file2 File2 File3 file33 fileAB filea fileA fileAAA file( file
2 (the last one has 6 characters including a space)
Code:
3. List (with ls) all files starting with file
Code:
4. List (with ls) all files starting with File
Code:
5. List (with ls) all files starting with file and ending in a number.
Code:
6. List (with ls) all files starting with file and ending with a letter
Code:
7. List (with ls) all files starting with File and having a digit as the fifth character.
Code:
8. List (with ls) all files starting with File and having a digit as a fifth character and nothing else.
Code:
9. List (with ls) all files starting with a letter and ending in a number.
Code:
10. List (with ls) all files that have exactly five characters.
Code:
11. List (with ls) all files that start with f or F and end with 3 or A.
Code:
12. List (with ls) all files that start with f have i or R as second character and end in a number.
Code:
13.List all files that do not start with the letter F.
Code:
14. Copy the value of $LANG to $MyLANG.
Code:
15. Show the influence of $LANG in listing A-Z or a-z ranges.
Code:
16. You receive information that one of your servers was cracked, the cracker probably
replaced the ls command. You know that the echo command is safe to use. Can echo replace
ls ? How can you list the files in the current directory with echo ?
Code:
17. Is there another command besides cd to change directories ?
Code:
Previous:
Linux Shell history
Next:
Linux I/O redirection