← All lessons
Command line basics · Lesson 6 of 7

Wildcards and quoting

Wildcards let one word stand for many file names. * matches any run of characters (even none), ? matches exactly one character, and [abc] matches one character from the list.

$ ls
duck1.jpg  duck2.jpg  duck10.jpg  notes.txt  todo.txt
$ ls *.txt
notes.txt  todo.txt
$ ls duck?.jpg
duck1.jpg  duck2.jpg

The shell expands the wildcard before the command runs: ls *.txt really runs ls notes.txt todo.txt. That means wildcards work with every command. cp *.jpg photos/ copies every JPEG, and rm *.tmp deletes every file ending in .tmp.

That last one is why wildcards deserve care. Run ls with the same pattern first to see what it matches, then swap ls for rm. A stray space in rm * .tmp deletes everything in the folder and then complains that .tmp doesn't exist.

Quotes switch the special meanings off. In single quotes nothing is special: echo '*' prints a star. Double quotes still let $variables through (more on those in the Processes and scripts course) but stop wildcards and keep spaces together.

A backslash escapes one character: my\ notes.txt is the same as "my notes.txt". Tab completion adds these backslashes for you.

Practice

In a folder with duck1.jpg, duck2.jpg and duck10.jpg, what does duck?.jpg match?

More info

Quick reference

*Any characters, or none.
?Exactly one character.
[abc], [0-9]One character from the set.
'text'Nothing inside is special.
"text"Keeps spaces together; `$variables` still work.
\xEscape one character.

The other Linux courses