Wildcards are special characters used to represent one or more characters in file and directory names. They are commonly used in commands like ls, cp, mv, and rm to perform operations on multiple files at once. Mastering wildcards can greatly enhance your efficiency when working with the Linux command line.
*: Matches zero or more characters.?: Matches exactly one character.[]: Matches any one character within the brackets.{}: Used to generate multiple strings or patterns (brace expansion).-: Specifies a range within square brackets.!: Used to negate a pattern within square brackets.**: Matches files and directories recursively (withglobstarenabled).~: Represents the home directory.
- Matches any number of characters (including zero characters).
- Used to match files or directories with any name or extension.
Examples:
*.txt→ Matches all files with a.txtextension (e.g.,file1.txt,document.txt).file*→ Matches all files starting withfile(e.g.,file1,file_abc,file123.txt).
- Matches exactly one character.
- Useful when you want to match files or directories where one character is variable.
Examples:
file?.txt→ Matchesfile1.txt,fileA.txt, but notfile.txt(expects one character betweenfileand.txt).?abc→ Matches1abc,aabc, but notabc.
- Matches any one of the characters inside the brackets.
- Can specify a range of characters.
Examples:
file[123].txt→ Matchesfile1.txt,file2.txt,file3.txt, but notfile4.txt.file[a-d].txt→ Matchesfilea.txt,fileb.txt,filec.txt,filed.txt.
Negation inside square brackets:
file[!a-d].txt→ Matches files likefilee.txt,filez.txt, but notfilea.txt,fileb.txt, etc.
- Used for brace expansion to generate multiple strings or patterns.
- Creates combinations of strings, simplifying commands.
Examples:
file{1,2,3}.txt→ Matchesfile1.txt,file2.txt,file3.txt.a{a,b,c}d→ Matchesaad,abd,acd.
Note: Brace expansion is performed before wildcard matching.
- Specifies a range within square brackets
[].
Examples:
file[a-z].txt→ Matchesfilea.txttofilez.txt(all lowercase letters).file[0-9].txt→ Matchesfile0.txttofile9.txt(digits).
- Negates a pattern within square brackets
[]. - Matches any character not in the brackets.
Examples:
file[!a-c].txt→ Matches files not ending witha,b, orc(e.g.,filed.txt).file[!1-5].txt→ Matchesfile6.txt,file7.txt, but notfile1.txttofile5.txt.
- Matches directories and files recursively.
- Requires enabling the
globstarshell option (Bash 4.0+).
Examples:
**/*.txt→ Matches all.txtfiles in the current and all subdirectories.**/file→ Matches all files or directories namedfileat any directory level.
To enable globstar:
shopt -s globstar
- Represents the home directory of the current user.
Examples:
cd ~→ Navigates to the home directory.ls ~/Documents→ Lists files in theDocumentsdirectory in your home folder.
*.txt→ Matches all.txtfiles.file*→ Files starting withfile.*file→ Files ending withfile.*→ All files in the current directory.*.*→ Files with an extension.file*log*→ Files starting withfilecontaininglog.file?→ Files likefile1,fileA.file[123]→ Matchesfile1,file2, orfile3.file[a-z]→ Filesfileatofilez.file[0-9]→ Filesfile0tofile9.file[a-d].txt→filea.txttofiled.txt.file[!a-d].txt→ Files not ending withatod.file{1,2,3}.txt→ Specific numbered files.file{a,b,c}.txt→ Filesfilea.txt,fileb.txt,filec.txt.*file*.txt→ Files containingfileending with.txt.file[abc]*→ Files starting withfilea,fileb, orfilec.*file[123].txt→ Any files ending withfile1.txt,file2.txt, orfile3.txt.**/*.txt→ All.txtfiles recursively.~→ Home directory reference.file[-abc]→ Files namedfile-,filea,fileb, orfilec.file\!→ Files starting withfile!.
Note: Escape special characters with \ to match them literally.
*~→ Files ending with~(backup files).*.bak→ Files with.bakextension.file{1..5}.txt→file1.txttofile5.txt(sequence expansion).*.log*→ Files containing.log.*log→ Files ending withlog.*test*→ Files containingtest.file*txt→ Files starting withfileending withtxt.file?.txt→ Files likefile1.txt,fileA.txt.[a-c]*.txt→ Files starting witha,b, orc, ending with.txt.[!a-c]*.txt→ Files not starting witha,b, orc, ending with.txt.*[!.txt]→ Files not ending with.txt.*[^a-c]*.txt→ Files not containingatoc, ending with.txt.*.*.*→ Files with two dots in the name.file*.txt→ Files starting withfileending with.txt.
-
Escaping Wildcards: To match wildcard characters (
*,?, etc.) literally, precede them with a backslash (\):ls file\* -
Character Classes: Use character classes for advanced matching:
[[:digit:]]→ Any digit (same as[0-9]).[[:alpha:]]→ Any alphabetic character (same as[a-zA-Z]).[[:alnum:]]→ Any alphanumeric character.[[:space:]]→ Any whitespace character.[[:upper:]]→ Uppercase letters.[[:lower:]]→ Lowercase letters.
Examples:
file[[:digit:]].txt→ Matchesfile0.txttofile9.txt.*[[:space:]]*→ Files with spaces in their names.
To use the ** wildcard for recursive searches:
-
Enable
globstar:shopt -s globstar -
Use
**in your commands:ls **/*.txtThis lists all
.txtfiles in the current directory and all subdirectories.
-
Using Wildcards with
rm:Be extremely careful when using wildcards with the
rmcommand to avoid deleting unintended files.Safe Practice:
-
Preview files before deleting:
ls *.txt -
Use interactive mode:
rm -i *.txt
-