Advanced Bash Scripting

Globs

Star

The asterisk (*) is a powerful wildcard used in Unix-like shells and many programming languages to match any sequence of characters (including none). In file path patterns (globs), it lets you flexibly select files based on prefixes, extensions, or arbitrary substrings.

By mastering * you can streamline file operations, automate tasks in scripts, and improve your command-line efficiency.


How * Works

  • * matches zero or more characters in a filename or path segment.
  • It does not match the path separator (/) unless the shell supports recursive globs (e.g., ** in Bash).
  • Always quote globs when you need to pass the literal pattern to a command rather than having the shell expand it first.

Quoting Patterns

Prevent premature expansion by the shell. For example:

grep "TODO" "*.txt"

Here, grep receives the pattern *.txt instead of the shell expanding it.


1. Matching Files with a Common Prefix

Imagine a directory containing:

$ ls
report.txt  report.docx  report.pdf  image1.jpg  backup2.tar.gz  1.log2.log

To list all files beginning with report. and any extension:

$ ls report.*
report.txt  report.docx  report.pdf
  • report. is matched literally.
  • * covers any extension (including an empty extension if it existed).

2. Matching a Specific Extension

Given files:

$ ls
report.py   report.pdf   notes.docx   notes.pdf   image1.jpg   config.pdf   log2.log

List only PDF documents:

$ ls *.pdf
report.pdf  notes.pdf  config.pdf
  • * matches any filename prefix.
  • .pdf filters for that exact extension.

3. Matching a Prefix with Varying Suffixes

In a directory like:

$ ls
notes.pdf   image1.jpg   image2.jpeg   image3.png   script.sh   backup1.tar.gz

Select all files whose names start with image regardless of extension:

$ ls image*
image1.jpg  image2.jpeg  image3.png
  • image is the fixed prefix.
  • * grabs everything that follows, covering .jpg, .jpeg, .png, etc.

Common * Patterns at a Glance

PatternDescriptionExample Result
report.*Files starting with report.report.txt, report.pdf
*.pdfFiles ending in .pdfnotes.pdf, config.pdf
image*Files starting with imageimage1.jpg, image2.jpeg
*All files in the current directoryEvery visible file

Watch Video

Watch video content

Previous
Globs Question Mark
Next
Escape