A filesystem that generates its contents
Most files live on a disk, and their bytes were written when someone saved them. The files under /proc work differently. The kernel creates them in memory when they are read and forgets them afterward. When you run cat /proc/loadavg, the kernel computes the three load averages and returns them as text. The file has no size on disk, and running ls -l on it often reports a size of zero.
The filesystem is mounted as type proc. It holds two kinds of entries. The numbered directories, one per process ID, describe a single process. The other files, such as meminfo, cpuinfo, and mounts, describe the whole system. A third group, such as sys, holds tunable kernel settings that you can read and sometimes write.
The per-process directory
Each process has a directory named by its PID. Inside it, several files and links expose the state of that process. status is a readable list of fields such as the name, state, parent PID, user IDs, and memory use. cmdline holds the arguments the process was started with. cwd is a link to the directory the process is working in, and exe links to the program file. fd is a directory of open files, and maps describes the memory layout. The man page proc(5) lists every entry.
grep -E "^(Name|PPid|Uid):" /proc/$$/statusThe dollar-dollar parameter expands to the PID of the current shell, so this command prints the name, parent, and user ID of bash. The three fields are written as a key, a colon, and a value, which makes them easy to filter with grep.
The self link
Writing the PID of a process into a path is awkward when the process is the one doing the reading. The link /proc/self points at the directory of the process that opens it. A program that reads /proc/self/status sees its own status, not the status of the shell that started it. This is why the examples in this module use self rather than a fixed number.
readlink /proc/self/cwdThe readlink command prints the target of a link. Run this way, it prints the directory where the command runs, because the link cwd under readlink points at that working directory.
Reading the command line
The cmdline file stores the arguments separated by NUL bytes, not spaces. A terminal shows NUL as nothing, so the output runs together. Translating the NUL bytes with tr makes the file readable. It is a good example of a file whose format is designed for programs and not for people.
cat /proc/self/cmdline | tr "\0" " "; echoThe pattern is worth remembering. Many /proc files use simple text, but some use NUL separators, and environ works the same way. Knowing which format a file uses saves a lot of confusion.
System-wide files
/proc/meminfo reports memory totals, including the amount available for new programs. /proc/loadavg holds the three load averages, followed by the running and total process counts and the most recent PID. /proc/mounts and /proc/self/mountinfo list the mounted filesystems, which the chroot lesson uses. /proc/sys holds kernel settings, and most of them need extra privileges to change.
When a tool such as ps or top looks wrong, read the same numbers directly from /proc. The tools are only readers, so the file is the primary source.
Permissions and privacy
A process can read its own directory fully. Reading another user's process is restricted: some files, such as environ, are readable only by the process owner, and the hidepid mount option can hide other users' processes entirely. Because the lab runs as an unprivileged user, you see your own processes and the public system files, which is enough for the exercises.
Reading /proc is also cheap. The files are generated on demand, so a script that polls a status file does not write to a disk. This is one reason monitoring tools read /proc directly.
Practice habit
Whenever a command reports a number you do not trust, look for the file that holds it. Most of the numbers that ps, free, and top print come from /proc. Learning the paths makes those tools feel like thin wrappers around the kernel.
Parsing /proc in scripts
Because /proc files are text with a stable layout, scripts read them with the same tools used on any text. The status file separates each key from its value with a colon, so cut with the colon delimiter picks out one value, and awk can compare numbers. A monitoring script might read the VmRSS line of a process and print it in megabytes. The fields are documented in proc(5), and the names stay the same across kernel versions even when new fields are added.
Two cautions apply. First, a process can exit between the moment you list its PID and the moment you read its file, so a read can fail with a no such file error. A careful script treats that as normal and moves on. Second, /proc numbers are a snapshot. Two readings taken a second apart differ, and a rate needs the difference between readings divided by the interval, not a single number.
Why /proc is not a backup target
Copying /proc with cp does not give you a copy of the system. The files are regenerated on every read, and many of them report a size of zero. A tool that needs to capture a state should read each file in one go and save the text, which is what the exercises in this module do with redirection.