Linux command line

Sefism.

Chapter 01

Filesystem and navigation

Linux has one directory tree starting at /, with a conventional place for everything. Knowing the layout tells you where to look.

Filesystem layout

Also called FHS, /etc, /var, /usr

What
Everything hangs off /. Key directories: /etc configuration, /home users' files, /var changing data such as logs and databases, /usr installed programs and libraries, /tmp temporary files, /opt add-on software, /proc and /sys live kernel information.
Why
When you need a config file, a log or a binary on an unfamiliar machine, the layout tells you where it almost certainly is.
Example
Nginx config lives in /etc/nginx, its logs in /var/log/nginx, and the binary in /usr/sbin/nginx.

Absolute and relative paths

Also called pwd, ~, ..

What
An absolute path starts at /, such as /var/log/syslog. A relative path starts from the current directory. . is the current directory, .. its parent and ~ your home directory.
Why
Scripts and cron jobs run from unpredictable directories, so knowing which kind of path you wrote avoids "file not found" surprises.

Watch out

Relative paths in scripts run from cron or systemd, which start in a different working directory than your shell.

Moving around and listing

Also called cd, ls, tree

What
pwd prints where you are, cd changes directory (cd - goes back, cd alone goes home), and ls lists contents with options for details, hidden files and sorting.
Why
These are the commands you type most; the right ls flags answer "what is here, how big, and how recent?" at a glance.
Shell
pwd
cd /var/log
ls -lah
ls -lt | head
cd -
tree -L 2 ~/projects

Creating, copying, moving and deleting

Also called cp, mv, rm, mkdir, touch

What
mkdir -p creates directories including parents, touch creates an empty file, cp -r copies, mv moves or renames, and rm deletes (-r for directories). There is no recycle bin.
Why
Daily file work. Knowing the flags avoids partial copies and accidental overwrites.
Shell
mkdir -p app/config
cp -r app app.bak
mv notes.txt docs/notes.md
cp -i draft.txt final.txt
rm -r build/

Watch out

rm -rf with a variable or glob you have not checked. Run ls with the same pattern first, and never run it as root out of habit.

Globs and hidden files

Also called wildcards, dotfiles, *

What
The shell expands globs before running a command: * matches any characters, ? one character, [abc] a set, and {a,b} expands to both words. Files starting with . are hidden from ls and from *.
Why
Globs let one command act on many files, and dotfiles such as .env and .bashrc hold much of your configuration.

Watch out

A glob that matches nothing is passed literally, so rm *.log in an empty directory complains about a file named *.log.

Getting help

Also called man, --help, tldr

What
man cmd opens the manual page, cmd --help prints a short usage summary, and type cmd tells you whether a name is a program, a shell builtin or an alias. Search manuals with man -k keyword.
Why
The manual on the machine matches the installed version exactly, unlike a random web answer.
01 · Filesystem and navigation1 / 10