Files, directories and paths
Unit 1 of 5 in Operating systems and the command line.
You will be able to find files with absolute and relative paths and say what the main Linux directories are for.
Lesson
The shell is where you type commands
A terminal is the window. The shell is the program inside it that reads what you type and runs it. The POSIX standard describes the shell as a command language interpreter that reads its input from a file or from the command line, which means the same language works typed by hand and saved in a script.
Almost everything in this class is a command you type at a shell prompt. A defender uses it because it is fast, repeatable and can be written down and rerun.
One tree, starting at the root
Linux has a single tree of directories. The manual describes the root directory as the place where the whole tree starts. Some directories have standard jobs. /etc holds configuration files that are local to the machine. /home is usually where users' own files live, although the manual says the exact layout depends on local administration. /root is usually the home directory of the root user. /tmp holds temporary files that may be deleted with no notice. /var/log is where log files go, and /usr/bin is the main place for programs ordinary users run.
Knowing this map tells a defender where to look: settings in /etc, logs in /var/log, and anything unusual in /tmp.
Absolute and relative paths
A pathname that starts with a slash is absolute: it begins at the root. One that does not start with a slash is relative: it begins at the current working directory. Every process has a current working directory. It is inherited from the process that started it and can be changed, which is what the cd command does.
To follow a path, the system looks up each directory name in turn. If the process lacks search permission on one of the directories along the way, the lookup fails with a permission denied error. So a file can be unreachable because of a directory above it, not because of the file itself.
Key points
- The shell reads commands typed by hand or saved in a file.
- Linux has one directory tree; /etc is configuration, /var/log is logs, /tmp is temporary.
- Absolute paths start at the root; relative paths start at the current directory.
- Permission on every directory along a path matters, not only on the file.
Sources
- Linux manual page: hier(7), description of the file system hierarchy
- Linux manual page: path_resolution(7), how a pathname is resolved
- POSIX.1-2017 (The Open Group): Shell Command Language, 2.1 Shell Introduction
Practice in the labs
Get comfortable moving around and managing files. These first three labs are short and give you a terminal habit.
- Your first Linux terminal: the commands you'll actually use: You build, fill, search and list a folder with the core commands, the base for everything else.
- Managing files from the command line: You practise copying, moving and removing files, so paths stop feeling abstract.
- Editing files from the terminal: nano and vim: You learn to edit a file without leaving the terminal, which you will do constantly on servers.
Knowledge check
- Which of these is an absolute path? Answer: /var/log/syslog. A pathname starting with a slash is absolute and begins at the root of the tree. The others begin at the current directory (path_resolution(7)).
- Where would you expect to find a machine's local configuration files? Answer: /etc. hier(7) describes /etc as holding configuration files local to the machine. /tmp is temporary and /usr/bin holds programs.
- A file you can read still gives 'permission denied' when opened by its full path. What is one possible cause? Answer: You lack search permission on a directory on the way to it. Path lookup needs search permission on each directory along the way; without it the lookup fails with 'Permission denied' even if the file itself is readable.
Back to the syllabus or see all classes.