Kodokon kodokon.com

Relative paths, absolute paths and wildcards

Point to any file with an absolute or a relative path, and act on several files at once.

8 min · 3 questions

Open this lesson in Kodokon

There are two ways to say where a file is, exactly like giving an address. The absolute path is the full postal address: it starts from the root and works from anywhere. On macOS and Linux it always starts with a forward slash /, for example /home/yoann/my-project/index.html. On Windows it starts with a drive letter, for example C:\Users\yoann\my-project.

The relative path, on the other hand, is more like "second door on the left": it starts from the folder you are currently in. Three symbols are enough to express anything. The dot . means the current folder. The two dots .. mean the parent folder. The tilde ~ means your home folder. You chain them together with slashes to build a route.

BASH
cd /home/yoann/my-project
cd ~/my-project
cd ../other-project
cat ./src/index.html
cp ../photo.jpg .
The same folder reached absolutely then relatively, and a copy into the current folder.

When you need to act on ten files, you are not going to type ten commands. The shell recognizes wildcards, also called patterns: special characters that stand in for one or more unknown characters. The star * stands for any sequence of characters, including none at all. The question mark ? stands for exactly one character. Square brackets [ ] accept any one of the characters listed inside them.

BASH
ls *.html
ls src/*.css
ls photo-?.jpg
ls report-202[45].pdf
cp *.txt archive/
Every .html file, the .css files in a subfolder, photo-1 to photo-9, the 2024 and 2025 reports.

One last detail that prevents a lot of trouble: on macOS and Linux, file names are case-sensitive. Photo.jpg and photo.jpg are two different files, and ls photo* will not find the first one. Windows makes no such distinction. So a project that works on your Windows machine can fail on the Linux server that hosts it, purely because of one capital letter.

Knowledge check

Make sure you remember the key points of this lesson.

  1. On macOS or Linux, what does an absolute path always start with?
    • A dot .
    • A forward slash /
    • A tilde ~
    • The name of the current folder
  2. Fill in the command to list only the files whose name has a single character between photo- and .jpg.
    $ ls
    photo-1.jpg  photo-2.jpg  photo-10.jpg
    $ ls ___
    photo-1.jpg  photo-2.jpg
    • photo-*.jpg
    • photo-?.jpg
    • photo-??.jpg
    • photo-[0-9]*.jpg
  3. You want to delete every .tmp file safely. What precaution do you take?
    • Run rm *.tmp then check with ls
    • Run ls *.tmp first to see what will be deleted
    • None, the shell always asks for confirmation