Wildcard characters in Unix/Linux allow the matching of files in an abstracted way. Rather than having to perfectly match a file or directory’s exact name and casing, you can insert wildcards to allow variations and match multiple files. Using wildcards to match multiple files is known as filename expansion, or "globbing" in some shells (like Bash).
Wildcards can be used in almost any Linux/Unix command or utility that accepts multiple file parameters. From ls to pandoc, you can use wildcards to operate on files in batches without having to create extensive file lists.
Asterisk (*) and question mark (?) are the two wildcard characters
Both of these will match any character (including spaces, punctuation, and non-UTF symbols). The asterisk matches any number of characters (including zero), and the question mark matches exactly one character.
If you have a directory with files named myfile-1.txt, myfile-2.txt, and myfile-3.txt, then you can match all three of these files with the wildcard expression myfile-?.txt. This would exclude myfile-10.txt, though, as there is more than one character in 10, so the single question mark wildcard would fail to match. If you wanted both myfile-1.txt and myfile-10.txt to be matched, you would want to use myfile-*.txt.
Examples of commands with wildcards
An asterisk (*) matches any number of characters: ls b*
An asterisk also matches zero characters (a.txt): ls a*.txt
Refine results by including an extension, matching only files with a matching extension: ls b*.txt
A question mark matches a single character: b-1?.txt
A question mark requires a character (a.txt is not matched): ls a?.txt
Square brackets match multiple characters or a range in most shells (demo in Bash): ls b-[12]?.*
Use wildcards to find files with matching text in the middle of the filename
You can find files with matching text in the middle of the filename by beginning and ending with an asterisk, e.g. *keyword*. This will match files that start with any number of characters, followed by keyword, and ending with any number of characters. Unless you specify a file extension, all file extensions will be included. For example:
The command find ~/Dropbox -name '*conflicted copy*' will return all of your conflicted Dropbox copies by matching anything containing "conflicted copy," regardless of what machine name generated the conflict. You can combine this with an -exec rm to clear out all of those conflicts.
Matching subdirectories with **
You can match files in multiple subdirectories by including ** in the wildcard match. For example, if you wanted to find all .txt files in all subdirectories of the current directory, you could run ls **/*.txt. In some shells, this will only search subdirectories, so you would have to combine it with *.txt if you also wanted to include .txt files in the current directory. For the most part, though, using **/*.txt will include all .txt files in the current directory as well as in subdirectories.
Use square brackets ([]) to match more specific characters
There are also square bracket operators in most shells (notably not Fish). These allow you to define a specific character set to match, e.g. myfile-[0-2].txt. That would match myfile-1.txt and myfile-2.txt, but not myfile-3.txt or myfile-10.txt. Square bracket operators can give you more granular matching than * or ?. Note that a square bracket expression can only match a single character; unlike regular expressions, there's no repeat operator. Square brackets can contain a list of individual characters that are matched, e.g. [adg] to match a, d, or g, or a range of numbers or letters to allow, specified with a hyphen between two characters. Matching is case-sensitive, so if you want to match any letter between a and z regardless of case, your expression would be [a-zA-Z]. To replicate the * operator but exclude any non-alphanumeric characters, use [a-zA-Z0-9].
Written by
Brett Terpstra
Principal Developer, Oracle
Filed Under
Related Articles
List Open Ports in Linux
Learn how to output the list of open TCP and UDP ports in Linux, as well as their IP addresses and ports using the netstat command.
Count Files in Linux
Learn how to count files and folders contained in directories and subdirectories in Linux using the ls, find, and wc commands.
How to Check the Size of Folders in Linux
Learn how to output the size of directories and subdirectories in a human-readable format in Linux and macOS using the du command.
Linux Chmod Command
Understand how to use chmod to change the permissions of files and directories. See examples with various chmod options.
POST JSON Data With Curl
How to send valid HTTP POST requests with JSON data payloads using the curl command and how to avoid common syntax pitfalls. Also, how to solve the HTTP 405 error code.
Format Command Output In Linux
Learn how to filter and format the content of files and the output of commands in Linux using the awk command.
Create Groups In Linux
Learn how to manually and automatically create and list groups in Linux.
Switch Users In Linux
Learn how to switch between users, log in as another user, and execute commands as another user in Linux.
Remover Users in Linux
Learn how to remove local and remote user accounts and associated groups and files in Linux using the userdel and deluser commands.
Delete Files In Linux
Learn how to selectively delete files in Linux based on patterns and properties using the rm command.
Find Files In Linux
Learn how to find and filter files in Linux by owner, size, date, type and content using the find command.
Copy Files In Linux
Learn how to safely and recursively copy one or more files locally and remotely in Linux using the cp and scp command.