Filename globbing in Linux: wildcards and *
Updated November 1, 2026
Globbing is the shell's expansion of patterns like * into a list of matching filenames.
Common cases
ls *.txt # every name ending in .txt
ls file?.log # ? is exactly one character
ls [ab]*.conf # names starting with a or b
rm *.tmp # check the pattern with ls firstSample output
$ ls
a.txt b.txt notes.md
$ ls *.txt
a.txt b.txt
$ ls *.log
ls: cannot access '*.log': No such file or directory
$ echo *.log
*.logHow it works: the shell expands the pattern, not the command
The shell expands a glob BEFORE the command runs:
rmseesrm *.tmpasrm a.tmp b.tmp. So if nothing matches, most shells pass the pattern through literally.*matches any sequence of characters (but not the leading dot of a hidden file),?matches exactly one character, and[...]matches one character from the set. To let a command do the matching itself (asfind -namedoes), you quote the pattern.
Common mistakes
* does not descend into subdirectories. A directory holds a.txt and sub/b.txt, and ls *.txt prints only a.txt. To reach the nested file you spell the levels out: ls */*.txt prints sub/b.txt. Walking a tree recursively is the job of find, not of *.
for f in $(ls) breaks on names with spaces, for f in * does not. A directory holds the file my report.txt. The first form prints [my] and [report.txt] on two separate passes of the loop, because the shell splits the output of ls on spaces. The second prints [my report.txt] on one line, because a glob expands to a list of names, not to text.
The pattern is substituted into the command line in full, and on a big directory that line stops fitting. In a directory with 300000 files ls -l *.txt answers bash: /usr/bin/ls: Argument list too long: the shell did substitute every name, and the kernel refused to pass such a list on. The way around it is find . -name '*.txt' -exec ... or xargs, where the names go in batches.
Used in
You need this in the lesson Files and directories. The theory is the chapter Files and directories.
See also
mkdir, cp, mv, rm, The ls command, The find command.
Try it
Open the sandbox: a real throwaway Linux terminal in your browser. Run the commands above by hand - the container is deleted when you leave.