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.)
datasoft @ datasoft-linux ~$ ls
ABC.png part1
ajax-php-mysql-user-interface.html part2
count part3
Desktop Pictures
Documents png
Downloads Public
examples.desktop sqlite3
file2 sqlite-amalgamation-3080500 (2)
FileA sqlite-amalgamation-3080500.zip
FileB sqlite-shell-linux-x86-3080500.zip
linux-command-past-date.png summer.png
mno.txt Summer.png
Music Templates
MyDir test1
MyDir1 test2
MyDirA text2
Myfile1.doc typescript
MYFILE1.doc Untitled 1.odt
MYFILE2.doc Videos
MyTest xyz.txt
datasoft @ datasoft-linux ~$
datasoft @ datasoft-linux ~$ ls File*
FileA FileB
datasoft @ datasoft-linux ~$ ls file*
file2
datasoft @ datasoft-linux ~$ ls *ext2
text2
datasoft @ datasoft-linux ~$ ls M*
Myfile1.doc MYFILE1.doc MYFILE2.doc
Music:
MyDir:
otherstuff stuff test test2
MyDir1:
file1 file2 FileA
MyDirA:
FileA
MyTest:
file1 file1~ file2 FileA
datasoft @ datasoft-linux ~$
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.
datasoft @ datasoft-linux ~$ ls
ABC.png part1
ajax-php-mysql-user-interface.html part2
count part3
Desktop Pictures
Documents png
Downloads Public
examples.desktop sqlite3
file2 sqlite-amalgamation-3080500 (2)
FileA sqlite-amalgamation-3080500.zip
FileB sqlite-shell-linux-x86-3080500.zip
linux-command-past-date.png summer.png
mno.txt Summer.png
Music Templates
MyDir test1
MyDir1 test2
MyDirA text2
Myfile1.doc typescript
MYFILE1.doc Untitled 1.odt
MYFILE2.doc Videos
MyTest xyz.txt
datasoft @ datasoft-linux ~$ ls File?
FileA FileB
datasoft @ datasoft-linux ~$ ls Fil??
FileA FileB
datasoft @ datasoft-linux ~$ ls Fi???
FileA FileB
datasoft @ datasoft-linux ~$
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.
datasoft @ datasoft-linux ~$ ls
ABC.png part1
ajax-php-mysql-user-interface.html part2
count part3
Desktop Pictures
Documents png
Downloads Public
examples.desktop sqlite3
file2 sqlite-amalgamation-3080500 (2)
FileA sqlite-amalgamation-3080500.zip
FileB sqlite-shell-linux-x86-3080500.zip
linux-command-past-date.png summer.png
mno.txt Summer.png
Music Templates
MyDir test1
MyDir1 test2
MyDirA text2
Myfile1.doc typescript
MYFILE1.doc Untitled 1.odt
MYFILE2.doc Videos
MyTest xyz.txt
datasoft @ datasoft-linux ~$ ls File[5A]
FileA
datasoft @ datasoft-linux ~$ ls file [a5][Z!]
bash: !]: event not found
datasoft @ datasoft-linux ~$ ls file[!3]*
file2
datasoft @ datasoft-linux ~$ ls test[!3]*
test1
test2:
apti test2 test2.txt
a-z and 0-9 ranges
The bash shell will also understand ranges of characters between brackets.
datasoft @ datasoft-linux ~$ ls
ABC.png ajax-php-mysql-user-interface.html
Desktop count
Documents examples.desktop
Downloads file2
FileA linux-command-past-date.png
FileB mno.txt
MYFILE1.doc part1
MYFILE2.doc part2
Music part3
MyDir png
MyDir1 sqlite-amalgamation-3080500 (2)
MyDirA sqlite-amalgamation-3080500.zip
MyTest sqlite-shell-linux-x86-3080500.zip
Myfile1.doc sqlite3
Pictures summer.png
Public test1
Summer.png test2
Templates text2
Untitled 1.odt typescript
Videos xyz.txt
datasoft @ datasoft-linux ~$ ls file [a-z]*
ls: cannot access file: No such file or directory
ajax-php-mysql-user-interface.html png
count sqlite-amalgamation-3080500.zip
examples.desktop sqlite-shell-linux-x86-3080500.zip
file2 summer.png
linux-command-past-date.png test1
mno.txt text2
part1 typescript
part2 xyz.txt
part3
sqlite-amalgamation-3080500 (2):
shell.c sqlite3.c sqlite3.h sqlite3ext.h
sqlite3:
sqlite-autoconf-3080500 sqlite-autoconf-3080500.tar.gz
test2:
apti test2 test2.txt
datasoft @ datasoft-linux ~$ ls file[0-9]
file2
datasoft @ datasoft-linux ~$
$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).
datasoft @ datasoft-linux ~$ ls [A-Z]ile?
file2 FileA FileB
datasoft @ datasoft-linux ~$ ls [a-z]ile?
file2 FileA FileB
datasoft @ datasoft-linux ~$ echo $LANG
en_IN
datasoft @ datasoft-linux ~$ LANG=C
datasoft @ datasoft-linux ~$ echo $LANG
C
datasoft @ datasoft-linux ~$ ls [a-z]ile?
file2
datasoft @ datasoft-linux ~$ ls [A-Z]ile?
FileA FileB
datasoft @ datasoft-linux ~$
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.
datasoft @ datasoft-linux ~$ mkdir test10
datasoft @ datasoft-linux ~$ cd test10
datasoft @ datasoft-linux ~/test10$ echo *
*
datasoft @ datasoft-linux ~/test10$ touch file10 file25
datasoft @ datasoft-linux ~/test10$ echo *
file10 file25
datasoft @ datasoft-linux ~/test10$
Globbing can be prevented using quotes or by escaping the special characters, as shown in this screenshot.
datasoft @ datasoft-linux ~/test10$ echo *
file10 file25
datasoft @ datasoft-linux ~/test10$ echo \*
*
datasoft @ datasoft-linux ~/test10$ echo '*'
*
datasoft @ datasoft-linux ~/test10$ echo "*"
*
datasoft @ datasoft-linux ~/test10$
Exercise, Practice and Solution:
1. Create a test directory and enter it.
Code:
mkdir testdir; cd testdir
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:
touch file1 file10 file11 file2 File2 File3
touch file33 fileAB filea fileA fileAAA
touch "file("
touch "file 2"
3. List (with ls) all files starting with file
Code:
ls file*
4. List (with ls) all files starting with File
Code:
ls File*
5. List (with ls) all files starting with file and ending in a number.
Code:
ls file*[0-9]
6. List (with ls) all files starting with file and ending with a letter
Code:
ls file*[a-z]
7. List (with ls) all files starting with File and having a digit as the fifth character.
Code:
ls File[0-9]*
8. List (with ls) all files starting with File and having a digit as a fifth character and nothing else.
Code:
ls File[0-9]
9. List (with ls) all files starting with a letter and ending in a number.
Code:
ls [a-z]*[0-9]
10. List (with ls) all files that have exactly five characters.
Code:
ls ?????
11. List (with ls) all files that start with f or F and end with 3 or A.
Code:
ls [fF]*[3A]
12. List (with ls) all files that start with f have i or R as second character and end in a number.
Code:
ls f[iR]*[0-9]
13.List all files that do not start with the letter F.
Code:
ls [!F]*
14. Copy the value of $LANG to $MyLANG.
Code:
MyLANG=$LANG
15. Show the influence of $LANG in listing A-Z or a-z ranges.
Code:
see example in book
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:
echo *
17. Is there another command besides cd to change directories ?
Code:
pushd popd
Previous:
Linux Shell history
Next:
Linux I/O redirection
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/linux-system-administration/file-globbing.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics