ShareYourGroupShareYourGroupCommands

Guides

Wildcards, quotation marks and spaces in file names

Two small topics cause a large share of command-line mistakes: patterns that match more or fewer files than expected, and file names with spaces in them. Both are simple once the rules are clear.

Wildcards

A wildcard is a character that stands for others in a file name. With one, a single command can act on many files.

WildcardStands forExampleMatches
*any run of characters, including none*.txtevery name ending in .txt
?exactly one characterphoto?.jpgphoto1.jpg, photoA.jpg, not photo10.jpg
[abc]one of the listed characters (Linux, Mac)file[12].txtfile1.txt, file2.txt
[a-z]one character in the range (Linux, Mac)[a-m]*names starting with a to m

On Linux and the Mac:

Linux or Mac terminal
asha@pc:~/demo$ ls *.txt
log.txt
notes.txt
report.txt
asha@pc:~/demo$ ls *o*
log.txt
notes.txt
report.txt

old:
draft.txt

photos:
beach.jpg
hills.jpg
asha@pc:~/demo$ ls ?????.txt
notes.txt
asha@pc:~/demo$ ls [lm]*
log.txt
marks.csv

The third pattern asks for exactly five characters before .txt. The fourth, for names starting with l or m.

On Windows:

Windows Command Prompt
C:\Users\asha\demo>dir /b *.txt
log.txt
notes.txt
report.txt
C:\Users\asha\demo>dir /b *o*
log.txt
notes.txt
old
photos
report.txt
C:\Users\asha\demo>dir /b ?????.txt
log.txt
notes.txt

Look before you act

A pattern is a guess about which files exist. Before using one with a command that changes anything, test the same pattern with a listing command.

Linux or Mac terminal
asha@pc:~/demo$ ls *.csv
marks.csv

What the listing shows is exactly what rm *.csv or mv *.csv folder would act on. On Linux, echo shows the command the shell would really run:

Linux or Mac terminal
asha@pc:~/demo$ echo rm *.txt
rm log.txt notes.txt report.txt

Nothing was removed. echo simply printed the line with the pattern expanded.

Who expands the pattern?

On Linux and the Mac, the shell replaces the pattern with the list of matching names before the command starts. The command never sees the star. Two things follow.

On Windows, each command does its own matching, and commands differ slightly in how they treat patterns.

A stray space is a disaster

rm *.txt and rm * .txt differ by one space. The second has two arguments: *, meaning every file, and .txt. It deletes everything in the folder. The same applies to del * .txt on Windows. Read a deleting command once more before pressing Enter, and list first.

Hidden files

On Linux and the Mac, * does not match names that begin with a dot. rm * leaves hidden files behind, and cp * backup/ does not copy them. To include them, name them: .[!.]*, or copy the folder itself with cp -r.

Windows short names and .

On Windows, *.* matches every file, including those with no dot in the name. Wildcards also match against the old short form of long names, which occasionally makes a pattern match a file whose visible name does not seem to fit.

Spaces in names

The command line splits what you type at spaces. A name such as annual report.txt is read as two names, annual and report.txt.

Linux or Mac terminal
asha@pc:~/demo$ echo sample > "annual report.txt"
asha@pc:~/demo$ cat annual report.txt
Sales report for October
North region: 8750
South region: 5750
East region: 4200
Total: 18700
cat: annual: No such file or directory
asha@pc:~/demo$ cat "annual report.txt"
sample

The first attempt went wrong in an instructive way. cat looked for two files. It could not find one called annual, and it printed a different file, report.txt, which happened to exist. With a deleting command, that kind of slip removes the wrong file. Quotation marks made the name one argument.

On Windows it is the same, with double quotation marks:

Windows Command Prompt
C:\Users\asha\demo>echo sample > "annual report.txt"
C:\Users\asha\demo>type "annual report.txt"
sample 

On Linux and the Mac there is a second way, a backslash before each space: cat annual\ report.txt. Pressing Tab to complete the name adds whatever is needed.

Single and double quotation marks

On Linux and the Mac, the two kinds differ.

FormInside it
"double"spaces are kept; variables such as $HOME are replaced
'single'everything is literal; nothing is replaced
Linux or Mac terminal
asha@pc:~/demo$ echo "Home is $HOME" | cut -c1-12
Home is /c/U
asha@pc:~/demo$ echo 'Home is $HOME'
Home is $HOME

In the Windows Command Prompt, only double quotation marks group words. Single ones are ordinary characters.

Wildcards inside quotation marks do nothing

A pattern in quotation marks is not expanded.

Linux or Mac terminal
asha@pc:~/demo$ ls "*.txt"
ls: cannot access '*.txt': No such file or directory

That looks for a file literally named star-dot-txt. To use a pattern with a path that has spaces, quote the part with spaces and leave the wildcard outside: ls "my files"/*.txt.

Special characters

Some characters mean something to the shell and need quoting or a backslash when they appear in a file name.

CharacterMeans, to the shell
spaceseparates arguments
* ? [ ]wildcards
$variable
&run in the background (Linux), join commands (Windows)
|pipe
> <redirection
;end of a command (Linux)
( )grouping
!history (Bash, Zsh)
#comment (Linux)
%variable (Windows)
^escape character (Windows)

A file called Tom & Jerry.txt typed without quotation marks is read as two commands. Put the whole name in quotation marks.

In scripts, always quote variables

A variable holding a file name should be in double quotation marks every time it is used: rm "$file", not rm $file. Without them, a name with a space becomes two arguments, and a name with a star is expanded. Unquoted variables are behind many scripts that work in testing and fail on real data.

Names that begin with a dash

On Linux, a file called -notes.txt is mistaken for an option. Put ./ in front, or -- before it: rm ./-notes.txt or rm -- -notes.txt. The double dash means "no more options follow".

Choosing names that cause no trouble

For files you will handle from the command line:

report-2026-11.txt needs no quotation marks anywhere. Report (final) #2.txt needs them everywhere.

Examples shown with output were run on 12 November 2026 in a practice folder: Windows commands in cmd.exe on Windows 11, Linux commands with the GNU tools of Git Bash. The folder path and user name were replaced with made-up ones. Commands marked "not run here" are shown as you would type them.

This is an independent learning site. It is not connected with Microsoft, Apple or any Linux distribution. Windows, macOS and other names belong to their owners. Try commands first in a practice folder, never on files you cannot afford to lose.