Clear, practical technology insights BSOD Code Lookup · Windows Error Code Lookup · Wi-Fi Troubleshooting · PC Troubleshooting Checklist

Linux File Wildcards: How to Match Names With *, ?, and Brackets

Match Linux filenames with shell wildcards, preview the results, and learn when to quote patterns for find.

Table of Contents

In a Linux shell, * matches zero or more characters, ? matches exactly one, and [abc] matches one character from a set. The shell usually expands an unquoted pattern before the command runs. Preview the match before copying, moving, or deleting files.

Match a prefix, suffix, or substring with *

ls -d -- *.txt
ls -d -- example*
ls -d -- *_1
ls -d -- *ample*

These patterns match names ending in .txt, beginning with example, ending in _1, or containing ample, respectively. -d keeps ls from opening matching directories. -- prevents a filename beginning with a dash from being treated as an option.

How to use wildcards to match file names in Linux Picture 1

How to use wildcards to match file names in Linux Picture 2

How to use wildcards to match file names in Linux Picture 3

How to use wildcards to match file names in Linux Picture 4

Names beginning with a dot are usually excluded by default. Use ls -A to inspect hidden entries or ls -d -- .config* for a specific hidden prefix.

How to use wildcards to match file names in Linux Picture 5

Match one character with ?

file?.txt matches file1.txt and fileA.txt, but skips file10.txt. Use two question marks for exactly two characters. Combine ? with * when only one position needs an exact width.

How to use wildcards to match file names in Linux Picture 6

How to use wildcards to match file names in Linux Picture 7

How to use wildcards to match file names in Linux Picture 8

Match or exclude a character set

[1ab]file.txt matches names beginning with 1, a, or b. file[!a-zA-Z] matches a single suffix character outside the specified letter ranges. For explicit case variants use [fF]ile.txt. Range behavior can depend on the locale.

How to use wildcards to match file names in Linux Picture 9

How to use wildcards to match file names in Linux Picture 10

How to use wildcards to match file names in Linux Picture 13

Generate alternatives with braces

In Bash, file{1,2,3}.txt produces three names even if the files do not exist. This is brace expansion.

How to use wildcards to match file names in Linux Picture 11

Quote a pattern for find

find Documents -type f -name '*.txt'
find Documents -type f -iname '*.txt'

The quotes pass the pattern to find so it can search inside subdirectories. -iname matches case-insensitively in GNU find. If a shell glob finds nothing, Bash and Zsh can behave differently. See the Bash filename expansion reference.

How to use wildcards to match file names in Linux Picture 12

Discussion

Reader Comments 0

Sign in with email or Google to join the discussion.