Linux › Section B · Basic commands and the directory hierarchy › Topic B.3
Navigating directories and wildcards
Every file on a Linux system sits somewhere in one tree of directories, and moving around that tree quickly is a core skill. This topic covers paths, the commands for changing and creating directories, and the wildcard patterns the shell uses to match many file names at once.
One tree from the root
Linux arranges all files in a single hierarchy of directories. Its top is the root directory, written as a single slash, /. Everything else hangs below it, and extra disks or USB drives are attached somewhere inside the tree rather than getting drive letters, as the topic "Mounting filesystems and /etc/fstab" explains. Names along a path are separated by forward slashes, as in /usr/share/doc; the backslash is never a directory separator on Linux. The main directories under /, such as /etc, /home and /usr, each have a set purpose, described in the topic "Linux directory hierarchy essentials".
Absolute and relative paths
A path names a file or directory. An absolute path starts with / and spells out the route from the root, so /usr/share/doc means the same thing wherever you are. A relative path does not start with /; it is read from your current working directory. Two special names help. A single dot, ., means the current directory, and two dots, .., mean its parent. If you are in /usr/share, then .. is /usr, ../lib is /usr/lib, and ./doc is /usr/share/doc, which most commands also accept as plain doc. Relative paths are what you use most, because you are usually already near the files you want.
cd, pwd and the home directory
Every running program keeps its own current working directory. The shell's is shown in most prompts, and pwd prints it in full. cd changes it: cd /etc moves to an absolute path and cd projects to a relative one. With no argument, cd returns to your home directory, the place your login starts, usually /home/username. The shell expands ~ at the start of a word into the path of your home directory, so cd ~/projects works from anywhere. cd - jumps back to the previous directory and prints its name. cd must be a shell built-in: a separate program would change only its own working directory, then exit, leaving the shell where it was.
Worked example: moving around a tree
In bash 5.2 you start in /tmp/practice and run mkdir -p projects/site/css, then cd projects/site. 1. pwd prints /tmp/practice/projects/site. 2. cd .. moves up one level; pwd prints /tmp/practice/projects. 3. cd ../.. climbs two more levels: from projects, the first .. is /tmp/practice and the second is /tmp, so pwd prints /tmp. 4. cd - returns to the previous directory and the shell printed /tmp/practice/projects. Check: each .. removed exactly one name from the end of the path, and cd - reversed only the last move, not the whole journey.
Creating and removing directories
mkdir notes creates a directory called notes. Normally every directory above the new one must already exist, but mkdir -p creates any missing parents too, so mkdir -p projects/site/css builds all three levels at once, and it does not complain if they already exist. rmdir old removes a directory only if it is empty; otherwise it fails with an error such as rmdir: failed to remove 'projects': Directory not empty. That refusal is a safety feature. To delete a directory with everything inside it you need rm -r, described next with a warning.
Warning: rm -r
rm -r dir deletes dir and, recursively, every file and subdirectory inside it, with no recycle bin and usually no confirmation. It is one of the few everyday commands that can destroy a great deal of work in a moment, and run by the superuser it can damage the whole system. Safe practice: first list exactly what would go, with ls -R dir; prefer rm -ri dir, which asks about each item, when unsure; never combine -r with wildcards such as * unless you have just checked what they expand to; and keep backups. Read the whole command once more before pressing Enter.
Wildcards: the shell's globbing
The shell can match patterns against file names, a feature called globbing. The star, *, matches any run of characters, including none. So at* matches names starting with at, *at matches names ending in at, and *at* matches names containing at anywhere. The question mark, ?, matches exactly one character: b?at matches boat and brat but not bat. Square brackets match one character from a set: [bc]at* matches names starting bat or cat. To match every file, use * alone. The Windows habit *.* matches only names containing a dot, since Linux names need no extension.
Expansion happens before the command runs
The shell, not the program, does the matching. It replaces each pattern with the sorted list of matching names, a step called expansion, and only then runs the command with the new argument list. So rm *.tmp hands rm a list of file names; rm never sees the star. echo is the ideal way to preview this: echo *.tmp prints what the pattern would become. By default, if nothing matches, bash leaves the pattern unchanged and passes it on literally. To stop the shell expanding a pattern, put it in single quotes: echo '*' prints a star. This matters for commands such as find, which do their own matching.
Worked example: predicting expansions
A directory holds atlas, bat, boat, brat, cat.txt, notes.md and a subdirectory projects. Each command below was run in bash 5.2. 1. echo b?at printed boat brat: the ? must match exactly one character, so bat is excluded. 2. echo *at printed bat boat brat: the names must end in at. 3. echo *at* printed atlas bat boat brat cat.txt: at may appear anywhere. 4. echo *.* printed cat.txt notes.md: only names with a dot. 5. echo *zzq printed *zzq: nothing matched, so the pattern stayed as it was. Check: projects never matched, because it contains neither at nor a dot.
Common mistakes to avoid
First, using backslashes in paths; Linux uses /. Second, forgetting that a relative path depends on where you are, so the same command can act on different files in different directories. Third, expecting rmdir to remove a directory with contents; it only removes empty ones. Fourth, running rm -r with a wildcard you have not previewed with echo or ls. Fifth, typing *.* to mean all files. Sixth, thinking the program expands wildcards; the shell does it first, and an unmatched pattern is passed on literally.
Key takeaways
Linux has one tree rooted at /, with / as the separator. Absolute paths start at /; relative paths start from the current directory, using . for here and .. for the parent. pwd prints the current directory; cd changes it, returns home with no argument and goes back with cd -; ~ means your home directory. mkdir -p creates parents; rmdir removes only empty directories; rm -r deletes whole trees, so preview first. Globs: * matches any characters, ? exactly one, [ ] one from a set. The shell expands them before the command runs, leaves unmatched patterns as typed, and skips expansion inside single quotes.
Flashcards
Flashcard
The root directory, and the path separator
Test your understanding
Question 1 of 5
You are in /home/ana/projects/site. Which directory does cd ../.. take you to?
Question 2 of 5
A directory contains bat, boat, brat and bait. What does echo b?at print?
Question 3 of 5
No file in the current directory ends in .log. With bash's default settings, what does echo *.log print?
Question 4 of 5
The command rmdir reports fails with Directory not empty. Why?
Question 5 of 5
Why must cd be built into the shell rather than be a separate program?
