You are on page 1of 7

Chapter 4

File Globbing
Course Name: Operating Systems

Course Code: COMP 1240

Notes appended and modified by K. Al-Ansari


to those accompanying
“LPIC - 1 online course, Introduction to Linux,
Netacad, 2019, Ch.4

E-mail: kalansari1@georgebrown.ca
Introduction
● Globs, also called “wildcards”, are special characters used in the shell
designed to match filenames.

● Glob characters are used for manipulating (listing, copying, moving, etc.)
groups of files.

● Glob characters include:


○ * = Match zero or more of any characters
○ ? = Match exactly one character
○ [] = Match a range of characters
Asterisk * Character
● The asterisk * wildcard can be used to match any string, including the empty string.

● To display all files in the current directory:


sysadmin@localhost:~$ ls *
Desktop Documents Downloads Music Pictures Public Templates Videos

● To display all files in the current directory that begin with the letter D:
sysadmin@localhost:~$ ls D*
Desktop Documents Downloads

● To display all files in the current directory that begin with D and have an n:
sysadmin@localhost:~$ ls D*n*
Documents Downloads
Question Mark ? Character
● The question mark ? character in a string will match exactly one character.

● To display all files in the current directory that have exactly one character in file
name:
sysadmin@localhost:~$ echo /usr/bin/?
/usr/bin/[ /usr/bin/w

● To display all files in the current directory that begin with the letter D and have
three or more characters:
Brackets [ ] Characters
● With the square bracket [ ] characters, a set of characters can be enclosed
that will be used to match exactly one character.

● To display all files in the current directory that begin with a, b or c and
followed by any two characters:

● To display all files in the current directory that don't begin with a, b or c
use the exclamation ! or caret ^ characters:

sysadmin@localhost:/usr/bin$ ls [!a-c]*

● The result of the above command is a long list of files, which is not shown.
None of the files starts with a, b or c.
Complex Globbing Examples
● To display all files in the current directory that begin with a, b or c and are
at least 5 characters long:

sysadmin@localhost:/usr/bin$ ls [abc]????*
Complex Globbing Examples
● The square brackets also support negating the set of characters (also called
complementation). If the first character inside of the square brackets is either
an exclamation ! character or a caret ^ character, then that first character has
a special meaning of not the following characters, meaning match a single
character that is not within the set of characters that follows it.

● In other words, the [!a-v]* pattern would match a file name that does not begin with a
letter a through v:

● Note: The result of this command is much longer (only first part is shown above)

You might also like