• Modern UX

    Edit and navigate faster in the terminal with Warp's IDE-like input editor.

  • Warp AI

    AI suggests what commands to run and learns from your documentation.

  • Agent Mode

    Delegate tasks to AI and use natural language on the command line.

  • Warp Drive

    Save and share interactive notebooks, workflows, and environment variables.

  • All Features

How To Use Unix Wildcards

Thumbnail for Brett TerpstraBrett Terpstra

Brett Terpstra

Principal Developer, Oracle

Published: 2/1/2024

About Terminus

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*

Thumbnail for

An asterisk also matches zero characters (a.txt): ls a*.txt

Thumbnail for

Refine results by including an extension, matching only files with a matching extension: ls b*.txt

Thumbnail for

A question mark matches a single character: b-1?.txt

Thumbnail for

A question mark requires a character (a.txt is not matched): ls a?.txt

Thumbnail for

Square brackets match multiple characters or a range in most shells (demo in Bash): ls b-[12]?.*

Thumbnail for

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:

Thumbnail for

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

Thumbnail for Brett TerpstraBrett Terpstra

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.

UnixLinux
Thumbnail for Razvan LudosanuRazvan Ludosanu

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.

LinuxUnix
Thumbnail for Razvan LudosanuRazvan Ludosanu

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.

LinuxUnix
Thumbnail for Razvan LudosanuRazvan Ludosanu

Linux Chmod Command

Understand how to use chmod to change the permissions of files and directories. See examples with various chmod options.

Linux
Thumbnail for Razvan LudosanuRazvan Ludosanu

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.

BashUnixLinux
Thumbnail for Neeran GulNeeran Gul

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.

Linux

Create Groups In Linux

Learn how to manually and automatically create and list groups in Linux.

Linux

Switch Users In Linux

Learn how to switch between users, log in as another user, and execute commands as another user in Linux.

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.

Linux

Delete Files In Linux

Learn how to selectively delete files in Linux based on patterns and properties using the rm command.

Linux

Find Files In Linux

Learn how to find and filter files in Linux by owner, size, date, type and content using the find command.

Linux

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.

Linux

Trusted by hundreds of thousands of professional developers

Download Warp to get started

Download for Mac
Request demo
Thumbnail for null