The shell and the filesystem
What the shell is, how paths resolve, and how to look around before touching anything.
The shell is a program that starts programs
When you type at a prompt, three things cooperate. The terminal draws characters and passes keystrokes. The shell, usually Bash, reads a whole line, expands it, and asks the kernel to start the named program with the resulting arguments. The program does the work and exits with a status code. Keeping the three apart explains most early surprises. A wildcard that "deleted too much" was expanded by the shell, not by rm. A command that "works in my terminal" may depend on something only your shell set up.
The shell gives every command an environment and a working directory. Relative paths are resolved against that directory, which is why the same command behaves differently depending on where you stand. Before acting on a host, the reading's first habit is worth keeping: run pwd, then ls -l, and only then decide.
Paths: absolute, relative, and shorthand
An absolute path starts at / and means the same thing everywhere. A relative path is resolved from the working directory. Three shorthands appear constantly:
.is the current directory../deployruns thedeployin this directory rather than one found onPATH...is the parent directory.~is expanded by the shell to your home directory. Other programs, such as a service manager reading a unit file, will not expand it.
Runbooks usually give absolute paths because the reader's working directory is unknown. Scripts should do the same, or deliberately cd first.
Links: names that point at other names
A symbolic link is a tiny file whose content is a path. When a program opens it, the kernel follows that path. Release layouts lean on this: /srv/current points at releases/2.4, and a release is "switched" by repointing one link. ls -l shows the arrow and the stored path. readlink -f resolves the whole chain to a real location. A link can dangle: if its target is removed, the link remains and opening it fails with "No such file or directory", even though ls shows the link.
A hard link is different. It is a second directory entry for the same file data, so neither name is the "original". The reading introduces both. Operations work mostly uses symbolic links.
Looking before touching
The filesystem hierarchy is a map of intent. /etc holds configuration, /var/log holds logs, /var/lib holds service state, and /tmp holds scratch data that may vanish at reboot. When something goes wrong, the hierarchy tells you where to look first.
ls answers most first questions if you choose the right flags. -l shows owner, group, mode, size, and time. -t sorts newest first. -S sorts largest first. -h prints readable sizes. -a shows dotfiles. Combining them, as in ls -lth, turns "which handoff is newest?" into a fact rather than a guess. For a whole tree, du -sh * totals each entry and points to the space consumer.
Changing things on purpose
Copy, move, and remove are where the shell can hurt you. Three habits from the reading prevent most accidents:
- Preview a wildcard with
lsbefore you hand the same pattern tormormv. - Use the interactive flags (
-i) or name files exactly when a directory mixes scratch and live data. - Prefer moving to deleting when unsure. A file moved to a holding directory can come back.
In the labs, a correct answer is an exact path or file name, not a command. The command is how you found the evidence; the finding is what you report.
Key terms
- Shell
- The program that reads your command line, expands it, and starts other programs. Bash is the common default.
- Working directory
- The directory a process resolves relative paths against. Each process has its own.
- Absolute path
- A path starting with
/, resolved from the root no matter where you are. - Symbolic link
- A small file whose content is another path. Opening it follows that path; deleting it leaves the target alone.
Read further
- The Linux Command Line, Chapters "What Is the Shell?", "Navigation", and "Exploring the System" (Free PDF (CC BY-NC-ND))
The difference between the shell and the terminal emulator, howcdandpwdrelate to the current working directory, and thels -lcolumns. Note which directories the tour of the system says hold configuration, logs, and programs. - The Linux Command Line, Chapter "Manipulating Files and Directories" (Free PDF (CC BY-NC-ND))
Wildcards, and the options that makecp,mv, andrmask before they overwrite. Pay attention to how hard and symbolic links are created and what each one points at. - How Linux Works, 3rd edition, Ch. 2, "Basic Commands and Directory Hierarchy" (Purchase)
The directory hierarchy overview. Compare it with the tour in the other book and note the roles of/etc,/var,/usr, and/tmp.