Innovate | Connect | Transform
ContactService statusUSD $

GICT knowledge base

How to Use Wildcards (*, ?) for File Management in Linux

Published

LinuxLinux and Commands

Managing files and directories from the command line is a core skill for any Linux user, whether you are an administrator, a developer, or a hobbyist. The Linux shell provides powerful tools for handling files in bulk, and wildcards play a crucial role in making those tools flexible and efficient. Wildcards, also known as globbing patterns, enable you to refer to multiple files and directories at once using concise patterns, saving you time and reducing the risk of errors.

This article explains the concept of wildcards, the different types available in Linux, and how to use them for everyday file management tasks. You will learn practical commands, see real-world examples, and gain best practices for safer and more effective use.

Prerequisites

  • Access to a Linux machine or terminal emulator
  • Familiarity with basic shell commands (ls, cp, mv, rm, etc.)
  • Appropriate permissions to manage files in your working directory

What Are Wildcards in Linux?

Wildcards are special characters used by the shell to represent one or more other characters in file and directory names. When you include wildcards in a command, the shell automatically expands the pattern to match all files and directories that fit, making it possible to handle collections of files with a single command.

This process is called globbing. It is built into the shell itself, not the file management commands. Wildcards are supported in Bash, Zsh, and most other common Linux shells.

The Main Wildcards: * and ?

The Asterisk (*) Wildcard

The * wildcard matches zero or more characters in a filename or directory name.

Key Points:

  • Can be used anywhere in the pattern (start, middle, or end)
  • Matches any number of characters (including zero)
  • Often used to perform operations on all files of a certain type

Examples:

  • *.txt matches all files ending with .txt (report.txt, notes.txt, summary.txt, etc.)
  • file* matches any file starting with file (file, file1, file123.txt, file_backup.doc)
  • *data* matches any file or directory containing data anywhere in its name (data.csv, rawdata.txt, metadata.json)

The Question Mark (?) Wildcard

The ? wildcard matches exactly one character in a filename or directory name.

Key Points:

  • Each ? matches a single character
  • Useful for targeting files with names of the same length or similar patterns

Examples:

  • file?.txt matches file1.txt, fileA.txt, but not file10.txt or file.txt
  • data_202?.csv matches data_2021.csv, data_2022.csv, etc., but not data_20210.csv or data_202.csv
  • log_??.log matches log_ab.log, log_12.log, but not log_a.log or log_123.log

Using Wildcards with Common File Management Commands

List Files with Wildcards

$ ls *.sh
Lists all files ending in .sh in the current directory.

$ ls data_20??.csv
Lists all CSV files from the years 2000–2099 if named in that pattern.

Copy Multiple Files

$ cp image*.jpg /mnt/backup/
Copies all JPEG files starting with image to the /mnt/backup/ directory.

Move Files by Pattern

$ mv report_2023* ../archive/
Moves all files starting with report_2023 to the archive directory one level up.

Remove Files Using Wildcards

$ rm *.bak
Deletes all files ending with .bak in the current directory.

WARNING: Be careful when using wildcards with rm or any destructive command. Always test your pattern with ls first.

Edit Multiple Files

$ vim *.conf
Opens all files ending with .conf in Vim.

Advanced Wildcard Patterns

Character Sets

[abc] matches any single character listed between the brackets.
$ ls file[123].txt
Matches file1.txt, file2.txt, and file3.txt.

Character Ranges

[a-z] matches any single character in the range from a to z.
$ ls notes[1-5].txt
Matches notes1.txt through notes5.txt.

Negated Sets

[!abc] matches any single character except a, b, or c.
$ ls file[!0-9].txt
Matches fileA.txt, fileX.txt, but not file1.txt.

Matching Multiple Extensions

$ ls *.{jpg,png,gif}
Matches all files ending with .jpg, .png, or .gif. This is called brace expansion and is supported by Bash.

Matching Hidden Files

$ ls .*
This will also match . (current directory) and .. (parent directory), so use with care.

Recursive Globbing (globstar)

$ shopt -s globstar
$ ls **/*.txt
Lists all .txt files in the current directory and all subdirectories.

Real-World File Management Scenarios

Clean Up Old Logs

$ rm /var/log/*2022*.log

Archive Monthly Reports

$ mv report_2024-03-*.pdf archive/

Copy Multiple File Types

$ cp *.{jpg,png,gif} images/

Find and Edit Specific Files

$ vim *nginx*.conf

Rename Files in Bulk

$ for file in *.jpeg; do mv "$file" "${file%.jpeg}.jpg"; done

Escaping Wildcards

$ ls file\*.txt
$ ls "file*.txt"
This matches a file literally named file*.txt, not all files matching the pattern.

Combining Wildcards with find for Recursive Search

$ find . -name "*.log"
Lists all .log files in the current directory and all subdirectories.

$ find /home/alex/docs -type f -name "report_2023*.pdf"
Finds all PDF reports from 2023 under /home/alex/docs.

Wildcards and Quoting

$ ls *.txt      # expands to all .txt files
$ ls "*.txt"    # looks for a file literally named *.txt

Troubleshooting Wildcard Use

  • Wildcard Pattern Matches Too Many Files – Always try your pattern with ls before using destructive commands
  • Command Fails to Match Hidden Files – Use .* if you want to include hidden files
  • Pattern Doesn’t Match Files in Subdirectories – Use find or enable globstar
  • Accidental Match of Current/Parent Directory – Patterns like .* match . and .., avoid rm .* unless sure
  • Shell Doesn’t Support Advanced Patterns – Some minimal shells do not support ** for recursive globbing

Best Practices for Using Wildcards

  • Preview with ls: Always test your pattern before using with mv, cp, or rm
  • Be specific: Narrow your patterns to avoid accidental matches
  • Use quotes carefully: Avoid quoting patterns unless you want to prevent expansion
  • Escape wildcards for literal matches: Use backslash or quotes for files with actual * or ?
  • Combine with -i for safety: Use interactive flag (-i) with rm or mv
  • Document scripts: Comment your logic for others when using wildcards

Security Considerations

Wildcards can be powerful—and dangerous if misused. A mistyped pattern with rm can delete many unintended files. Always double-check your matches, especially when operating with elevated privileges (sudo).

WARNING: Avoid running rm -rf * or similar commands in important directories. One mistake can wipe out your files or system.

Combining Wildcards with Other Linux Tools

Search for a Pattern in Multiple Files

$ grep "ERROR" *.log
Searches for the word "ERROR" in all .log files.

Archive All Source Files

$ tar -czvf src_backup.tar.gz *.c *.h
Creates a compressed archive of all .c and .h files.

Summary Table: Wildcard Patterns

Pattern Matches Example
* Any number of characters (zero or more) file* → file, file1, fileX
? Exactly one character file?.txt → file1.txt, fileA.txt
[abc] a, b, or c file[ab].txt → filea.txt, fileb.txt
[a-z] Any character a through z file[a-c].txt → filea.txt, fileb.txt, filec.txt
[!0-9] Any character not 0–9 file[!0-9].txt → filea.txt
*.[pj][np]g Multiple possible extensions *.png, *.jpg
*.* All files with an extension file.txt, image.jpg, doc.pdf
.* All hidden files .bashrc, .gitignore

Conclusion

Wildcards are a foundational tool for working efficiently on the Linux command line. By mastering patterns with *, ?, and character sets, you can perform complex file management tasks with a few keystrokes—listing, copying, moving, deleting, or editing groups of files in bulk. Use wildcards thoughtfully, always preview your patterns, and you will save time and reduce mistakes in your Linux workflow.

Related guides

Account services are unavailable until the production backend is configured.