Understanding File Paths and Wildcards
Every command that works with files needs to know which files. A path tells Bash where a file is, and a wildcard lets you describe many files with a single pattern instead of typing each name. These two ideas appear in almost every command you will write, so it pays to understand exactly how they work.
Understanding Paths
A file system is a tree. At the top is the root directory, written /, and every file and directory hangs below it. A path is a route through that tree, with / separating the names along the way.
The examples in this lesson use a small web project. The shell is inside the site directory:
/home/user/
├── docs/
└── projects/
└── site/
├── about.html
├── contact.html
├── index.html
├── notes.txt
├── .hidden
├── my file.txt
├── report_a.txt
├── report_ab.txt
├── report_b.txt
├── Report_C.txt
├── css/
│ ├── main.css
│ └── print.css
└── img/
├── logo.png
├── photo1.jpg
├── photo2.jpg
└── photo10.jpg
The pwd command prints the current directory:
pwd
Output:
/home/user/projects/site
Absolute Paths
An absolute path starts with / and describes the route from the root. It means the same thing no matter where you are:
ls /home/user/projects/site/css/main.css
Output:
/home/user/projects/site/css/main.css
Relative Paths
A relative path does not start with /. Bash reads it starting from the current directory. From site, the file main.css is reached through css:
ls css/main.css
Output:
css/main.css
Special Names in Paths
Four short names have a fixed meaning:
| Name | Meaning |
|---|---|
. | The current directory |
.. | The parent directory, one level up |
~ | Your home directory, such as /home/user |
- | The previous directory (used with cd) |
The name . is useful when a command requires a path and you mean "here". The name .. can be repeated to climb several levels. All three of these commands name the same file:
ls css/main.css
ls ./css/main.css
ls ../site/css/main.css
Output of each:
css/main.css
./css/main.css
../site/css/main.css
The last one goes up to projects, then comes back down into site and on to css/main.css. The tilde is expanded by the shell before a command runs:
echo ~
cd ~/docs
pwd
Output:
/home/user
/home/user/docs
The command cd - returns you to the directory you were in before and prints its path. It uses the OLDPWD variable, which Bash updates on every cd.
Symbolic Links and pwd -P
When a directory is reached through a symbolic link, pwd shows the path you used to get there. The option -P prints the physical path, with all links resolved. In this project there are no links, so both print the same text.
How Wildcards Work
A wildcard, also called a glob pattern, is a pattern that Bash expands into a list of matching file names. The important point is who does the work: the shell expands the pattern before the command starts. The command never sees the wildcard; it receives the final list of names.
You can see this with echo, which just prints its arguments:
echo *.html
Output:
about.html contact.html index.html
Bash replaced *.html with the three matching names, in alphabetical order, and echo printed them. Running ls *.html is exactly the same as typing ls about.html contact.html index.html.
The Wildcard Characters
The Asterisk *
The * matches any number of characters, including none. It matches within one path component only, so it never matches the / between directories:
ls *.html
ls css/*.css
Output:
about.html
contact.html
index.html
css/main.css
css/print.css
The Question Mark ?
The ? matches exactly one character. With these files, report_?.txt matches a name with one character after the underscore, and report_??.txt matches names with two:
ls report_?.txt
ls report_??.txt
Output:
report_a.txt
report_b.txt
report_ab.txt
Character Sets with [ ]
Square brackets match one character out of a set or range:
ls report_[ab].txt
ls img/photo[0-9].jpg
Output:
report_a.txt
report_b.txt
img/photo1.jpg
img/photo2.jpg
The second pattern matches names with exactly one digit, so photo10.jpg is left out because it has two. To match anything except the listed characters, start the set with !:
ls report_[!a].txt
Output:
report_b.txt
Sets can also use named classes such as [[:digit:]], [[:lower:]] and [[:upper:]]. The classes behave the same in every language setting, which makes them safer than ranges like [A-Z]. In some locales a range such as [a-z] can also match uppercase letters.
Brace Expansion Is Different
Curly braces look similar, but they are not a wildcard. {a,b} generates text whether or not any file exists:
echo img/photo{1,2}.jpg
Output:
img/photo1.jpg img/photo2.jpg
Brace expansion is covered in its own lesson later. The rule to remember is that a glob only produces names of files that exist, and braces produce names regardless.
Rules You Need to Know
Hidden Files
A name that begins with a dot is hidden. The wildcard * does not match a leading dot, so .hidden is skipped:
ls *
Output:
Report_C.txt
about.html
contact.html
index.html
my file.txt
notes.txt
report_a.txt
report_ab.txt
report_b.txt
css:
main.css
print.css
img:
logo.png
photo1.jpg
photo10.jpg
photo2.jpg
Two things happen here. The hidden file is missing, and because css and img also match *, ls shows what is inside those directories. To include hidden files, use ls -A, or write the dot yourself with a pattern such as .h*. In Bash 5.2 and later, the pattern .* no longer matches the special entries . and .., so echo .* prints only .hidden here. Older versions also printed . and ...
Order of Results
Bash sorts the expanded names alphabetically according to your locale, and it compares text, not numbers:
ls img/photo*.jpg
Output:
img/photo1.jpg
img/photo10.jpg
img/photo2.jpg
photo10.jpg comes before photo2.jpg because the character 1 sorts before 2. The order of uppercase and lowercase names also depends on the locale: in this example Report_C.txt came first in the listing, but another system might place it after report_b.txt.
Quoting Turns Wildcards Off
Inside quotes, or after a backslash, a wildcard is just text:
echo *.html
echo "*.html"
echo '*.html'
echo \*.html
Output:
about.html contact.html index.html
*.html
*.html
*.html
Quote a pattern when a program should receive it unchanged. This is why find . -name "*.py" needs quotes: find does its own matching.
File Names with Spaces
The glob result keeps each name together, spaces included, because Bash treats every match as a separate argument:
ls my*
Output:
my file.txt
When you type a name with a space by hand, you must quote it or escape the space, as in "my file.txt" or my\ file.txt.
When Nothing Matches
If no file matches, Bash leaves the pattern in place as plain text:
ls *.pdf
echo *.pdf
Output:
ls: cannot access '*.pdf': No such file or directory
*.pdf
ls received the literal text *.pdf, looked for a file with that exact name and failed. The echo line shows the same thing: the pattern passed through unchanged. This behavior surprises many people, because a script that loops over *.pdf runs once with the literal name when there are no PDF files.
Changing How Globs Behave with shopt
The shopt command turns Bash options on with -s and off with -u. Several options change how wildcards work:
| Option | Effect |
|---|---|
nullglob | A pattern with no match expands to nothing |
failglob | A pattern with no match is an error |
dotglob | * also matches hidden files |
nocaseglob | Matching ignores case |
globstar | ** matches files in all subdirectories |
All of them are off by default. Here are the first three in action:
shopt -s nullglob
echo *.pdf
shopt -u nullglob
Output (an empty line, because nothing expanded):
shopt -s dotglob
echo *
shopt -u dotglob
Output:
.hidden Report_C.txt about.html contact.html css img index.html my file.txt notes.txt report_a.txt report_ab.txt report_b.txt
With failglob on, an unmatched pattern stops the command before it runs and Bash reports the problem:
bash: no match: *.pdf
The nocaseglob option makes the letter case irrelevant, so report_?.txt also matches Report_C.txt:
shopt -s nocaseglob
ls report_?.txt
shopt -u nocaseglob
Output:
Report_C.txt
report_a.txt
report_b.txt
Finally, globstar lets ** reach through any number of directory levels:
shopt -s globstar
ls **/*.css
shopt -u globstar
Output:
css/main.css
css/print.css
Changes made with shopt last only for the current shell session. In a script, put the shopt -s line near the top.
Practical Applications
Wildcards pair naturally with commands that change many files at once. Because the shell expands the pattern before the command runs, you can preview exactly what will be affected by putting echo in front of the command:
echo rm img/photo*.jpg
Output:
rm img/photo1.jpg img/photo10.jpg img/photo2.jpg
Nothing was deleted: echo only printed the command that Bash would have run, with the pattern already expanded. If that list is what you expected, remove the echo and run the command for real. If the list is wrong, you found the mistake before any file was touched.
Copying all web pages into a backup directory:
cp *.html ~/docs/
Counting the photos:
echo img/*.jpg | wc -w
Output:
3
Searching text only in the stylesheets. The css/*.css pattern expands to both stylesheet names, so grep receives two files and prints the file name in front of every match:
grep -n "color" css/*.css