Mastering Unix Wildcards: Patterns, Commands, and Practical Examples
This guide explains Unix shell wildcards, their syntax, and how to use symbols like *, ?, [] and [!...] for file name matching, includes practical find command examples, redirection, quoting, pipelines, and shortcuts for efficient command‑line operations.
Wildcard Basics
Wildcards are special characters interpreted by the shell to match file names when the exact name is unknown. They operate on pathnames, not on file contents, and are understood by most Unix‑like commands.
Wildcard Syntax
*– matches zero or more characters, including the empty string. ? – matches exactly one character. [abcd] – matches any one character listed inside the brackets; ranges such as [a-z] or [0-9] are also supported. [!abcd] or [^abcd] – matches any character not listed.
POSIX Character Classes
| Symbol | Meaning |
|----------------|----------------------|
| [[:upper:]] | all uppercase letters |
| [[:lower:]] | all lowercase letters |
| [[:alpha:]] | all alphabetic letters |
| [[:digit:]] | all digits |
| [[:alnum:]] | letters and digits |
| [[:space:]] | whitespace characters |
| [[:punct:]] | punctuation symbols |Test Data Setup
# mkdir /test && cd /test
# touch yuchao.txt yuc.txt cc.txt yuchao01.log yuyu.logUsing * to Match Any Characters
# List all files ending with .txt
ls *.txt
# Files starting with "y" and ending with .txt
ls y*.txt
# Files starting with "y" and ending with "t"
ls y*t
# Shell scripts ending with .sh
ls *.sh
# Find all .sh files whose names start with a lowercase letter
find / -name '[a-z]*.sh'
# Find all .sh files whose names start with a digit
find / -name '[0-9]*.sh'
# Find all .sh files whose names start with an uppercase letter or a digit
find / -name '[A-Z0-9]*.sh'Using ? to Match a Single Character
# Files with exactly one character before .sh
ls ?.sh
# Files with exactly two characters before .txt
ls ??.txtBracket Expressions []
# Files named y followed by a single lower‑case letter and .txt
ls yu[abc].txt
# Files named y followed by a single upper‑case letter and .txt
ls yu[ABC].txt
# Files whose names start with a lower‑case letter
ls [[:lower:]]*.log
# Files whose names start with an upper‑case letter
ls [[:upper:]]*.logNegated Bracket Expressions [!...]
# All .log files not starting with "abcd"
ls [!abcd]*.log
# Files containing either "y" or "u"
ls *[yu]*
# Files not starting with "y" or "u"
ls [!yu]*
# .sh files not starting with "y" or "u"
ls *[!yu]*.shFinding Files with find and Wildcards
# Search /etc for any file containing "hosts"
find /etc -name '*hosts*'
# Files in /etc that start with "ifcfg"
find /etc -name 'ifcfg*'
# Files that start with "ifcfg" and end with a digit
find /etc -name 'ifcfg*[0-9]'
# List disk devices /dev/sda‑/dev/sdd
ls /dev/sd[abcd]Quoting Rules
Single quotes preserve the literal string; no variable, command, or escape substitution occurs.
Double quotes allow variable and command substitution while protecting spaces from word‑splitting.
No quotes can lead to unintended word‑splitting and glob expansion.
# Single‑quoted literal
echo 'hello $USER'
# Double‑quoted with variable expansion
echo "hello $USER"
# Command substitution inside double quotes
echo "Current time: $(date '+%F %T')"Redirection and File Descriptors
>– redirect stdout (file descriptor 1) and overwrite the target file. >> – redirect stdout and append to the target file. < – redirect stdin (file descriptor 0) from a file. 2>&1 – redirect stderr (file descriptor 2) to the same destination as stdout.
# Overwrite a file with the list of .txt files
ls *.txt > all_txt.list
# Append to the same file
ls *.txt >> all_txt.list
# Capture both stdout and stderr
ls /nonexistent > error.log 2>&1Command Chaining and Pipelines
command1 && command2– execute command2 only if command1 succeeds. command1 || command2 – execute command2 only if command1 fails. command1 ; command2 – execute command2 regardless of command1 's exit status. cmd1 | cmd2 – pipe the stdout of cmd1 into the stdin of cmd2.
# Compile and install only if compilation succeeds
make && make install
# Try to list a directory; if it fails, create it
ls /opt/mydir || mkdir /opt/mydir
# Run two commands sequentially
cd /var/log ; pwd ; ls -l
# Count the number of running nginx processes
ps -ef | grep nginx | wc -lBrace Expansion
Brace expansion generates arbitrary sequences that can be used to create multiple files or arguments.
# Create a series of log files named nginx_YYYY-MM-DD_HH:MM:SS.log
touch "nginx_$(date '+%F#%T').log"
# Generate numbers 1 through 30 on the command line
echo {1..30}
# Generate alphabetic sequence a‑z
echo {a..z}
# Copy a set of files with a numeric suffix
cp source{1..5}.txt /backup/Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
