Wildcards & Globbing

Using wildcards and pattern matching for file selection and batch operations in the shell

Wildcards and globbing are pattern matching features in Linux that allow you to select multiple files and directories using special characters. The shell expands these patterns before passing them to commands, making file operations more efficient and flexible.

Key Concepts

  • Wildcard: Special characters that represent one or more characters in filenames
  • Globbing: The process of expanding wildcard patterns into matching filenames
  • Pattern Matching: Using wildcards to match files based on naming patterns
  • Shell Expansion: How the shell converts patterns into actual filenames before running commands

Wildcard Characters

* - Matches zero or more characters ? - Matches exactly one character [...] - Matches any single character in brackets [!...] or [^...] - Matches any character NOT in brackets {...} - Matches any of the comma-separated patterns

Character Classes

[a-z] - Any lowercase letter [A-Z] - Any uppercase letter [0-9] - Any digit [a-zA-Z] - Any letter [[:alpha:]] - Any alphabetic character [[:digit:]] - Any numeric character [[:alnum:]] - Any alphanumeric character

Practical Examples

Example 1: Basic asterisk wildcard

1
2
3
4
5
ls *.txt
document.txt  notes.txt  readme.txt

ls file*
file1.doc  file2.pdf  file_backup.zip

Matches all files ending in .txt or starting with “file”

Example 2: Question mark wildcard

1
2
3
4
5
ls file?.txt
file1.txt  file2.txt  fileA.txt

ls ???.log
app.log  sys.log  web.log

Matches exactly one character in the specified position

Example 3: Character ranges

1
2
3
4
5
ls file[1-5].txt
file1.txt  file3.txt  file5.txt

ls [A-Z]*.doc
Document.doc  Report.doc  Summary.doc

Matches files with characters in the specified range

Example 4: Character exclusion

1
2
3
4
5
ls file[!0-9].txt
fileA.txt  fileB.txt  fileX.txt

ls *.[!t][!x][!t]
document.pdf  image.jpg  data.csv

Matches files NOT containing specified characters

Example 5: Brace expansion

1
2
3
4
ls {*.txt,*.doc,*.pdf}
notes.txt  report.doc  manual.pdf

mkdir {2023,2024}/{jan,feb,mar}

Creates multiple directories or matches multiple patterns

Example 6: Complex patterns

1
2
3
4
5
ls [Ff]ile[0-9][0-9].{txt,doc}
File01.txt  file23.doc  File99.txt

find . -name "*[Bb]ackup*"
./data_backup.tar  ./Backup_files/

Combines multiple wildcard types for precise matching

Use Cases

  • File listing: Quickly view files matching patterns
  • Batch operations: Apply commands to multiple files
  • File cleanup: Remove files matching specific patterns
  • Backup scripts: Select files based on naming conventions
  • Log analysis: Process log files with similar names

find - Search for files using patterns locate - Find files using database search grep - Search text using patterns (regex) rename - Batch rename files using patterns

Tips & Troubleshooting

Escaping Wildcards

1
2
ls \*.txt        # Literal asterisk
ls 'file*.txt'   # Prevent expansion

Use backslash or quotes to treat wildcards literally

Hidden Files

1
2
ls .*            # Show hidden files
ls .??*          # Avoid . and .. directories

Dot files require explicit wildcard patterns

Case Sensitivity

1
2
shopt -s nocaseglob  # Enable case-insensitive matching
ls [Dd]ocument*      # Manual case handling

Linux filesystems are case-sensitive by default

Empty Matches

1
2
shopt -s nullglob    # Empty string if no matches
shopt -s failglob    # Error if no matches

Control behavior when patterns don’t match files

Performance Notes

  • Wildcards are expanded by the shell, not commands
  • Large directories may slow wildcard expansion
  • Use find for complex searches across directories
  • Quote patterns when passing to scripts to prevent premature expansion