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.
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.
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.
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.
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.
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.
Reader Comments 0
Sign in with email or Google to join the discussion.