Skip to content

Click on each book below to review & buy on Amazon.

As an Amazon Associate, I earn from qualifying purchases.


LPI Linux Essentials Exam 010-160 - Topic 2.4 - Simple Globbing

Globbing is a powerful feature in Linux that allows you to match files and directories using wildcard characters. This guide will walk you through the commonly used globbing characters.

Asterisk (*)

The asterisk (*) is a versatile wildcard character that matches zero or more characters in a filename.

For example, to list all files ending in .txt in the current directory, you would run:

ls *.txt

Question Mark (?)

The question mark (?) is a wildcard that matches a single character.

For instance, to list files like image1.jpg or imageA.jpg but not image12.jpg, you would run:

ls image?.jpg

Square Brackets []

Square brackets allow you to specify a set of characters to match a single character at a specific position in the filename, so [abc] would match any single character that is either a, b or c.

For example, to list filea.txt, fileb.txt and filec.txt, you would run:

ls file[abc].txt

You can also use character ranges within square brackets, such as [0-9] to match any single digit.

For example, to list file1.txt, file2.txt, file3.txt , file4.txt and file5.txt, you would run:

ls file[1-5].txt

Double Square Brackets [[:classname:]]

The [[:classname:]] construct is used for character class matching. It's particularly useful for matching specific types of characters:

  • [[:alnum:]]: Matches any alphanumeric character (letters and digits).
  • [[:alpha:]]: Matches any alphabetic character (letters).
  • [[:digit:]]: Matches any digit.
  • [[:lower:]]: Matches any lowercase letter.
  • [[:upper:]]: Matches any uppercase letter.

For example, to list all files that start with a digit, you would run:

ls [[:digit:]]*

This would match files like 1file.txt and 2nd_document.pdf.

Exercise

The following exercise will get you using some simple globbing:

Exercise: Use simple globbing to list files:

Search for all files in the /var/log directory that end in .log:

ls -l /var/log/*.log

List all 3 letter commands that start with c and end in t. The commands cat and cut should return, but there may be more on your system:

Using the ? wildcard:

ls -l /usr/bin/c?t

Using a range:

ls -l /usr/bin/c[a-z]t

List just the man page directories 1 to 3:

ls -ld /usr/share/man/man[1-3]

Find all files under /usr/share/doc that contain an uppercase letter:

find /usr/share/doc -type f -name *[[:upper:]]*

Globbing allows you to create flexible patterns for matching files and directories, making it a powerful tool for working in the Linux command line. You can also combine these characters in various ways to create more complex patterns tailored to your specific needs.

Keep in mind that globbing is distinct from regular expressions, which offer even more advanced pattern matching capabilities. Globbing is straightforward and intuitive for basic pattern matching in the shell. Regular expressions is covered in a later lesson.


Support DTV Linux

Click on each book below to review & buy on Amazon. As an Amazon Associate, I earn from qualifying purchases.

NordVPN ®: Elevate your online privacy and security. Grab our Special Offer to safeguard your data on public Wi-Fi and secure your devices. I may earn a commission on purchases made through this link.