$_ CLI In Depth
Learning progress 0%
lesson syscalls namespaces cgroups

The /proc Filesystem

5 min read Module 17: Syscalls, Namespaces & cgroups Updated Dec 5, 2026
Cheat Sheet
0:00 / 3:27

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.

BASH1 lines
grep -E "^(Name|PPid|Uid):" /proc/$$/status

The 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.

BASH1 lines
readlink /proc/self/cwd

The 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.

BASH1 lines
cat /proc/self/cmdline | tr "\0" " "; echo

The 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.

Tip

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.

Inside a process: its file table and its memory

Visualizer

Every process keeps a table of open files and a map of its memory. Both are visible as plain files under /proc, so you can read what the kernel knows about a running shell.

1 / 3
fd table of this shell
0/dev/pts/0 (stdin)
1/dev/pts/0 (stdout)
2/dev/pts/0 (stderr)

Each process has an fd table: a small array of numbers, each pointing at an open file. In a terminal session, the first three are inherited: 0 for input, 1 for output, 2 for errors.

exec 3< /etc/hostname; ls -l /proc/$$/fd; exec 3<&-
Terminal lab · 3 steps · real Linux in your browser

Read the process view in /proc

Read the status, working directory, and command line of the process that is reading them.

01
Read the status file
/proc/self is a link to the directory of the process that opens it. Filter its status file for the name and user lines and save them: ``bash grep -E "^(Name|Uid):" /proc/self/status > proc-status.txt `` Open proc-status.txt. The Name line is the program that read the file, which is grep.
Show hint for step 1
The fields in /proc/PID/status are written as Key: value. Use a regular expression that matches both key names at the start of a line.
02
Read the working directory link
The cwd entry in /proc/PID is a link to the directory the process is working in. Read it with readlink: ``bash readlink /proc/self/cwd > cwd.txt `` Open cwd.txt. It should contain the directory you started in.
Show hint for step 2
readlink prints the target of a symbolic link. The process that runs readlink is the one whose cwd it reads.
03
Read the command line
The cmdline file stores the arguments of a process separated by NUL bytes. Translate the NUL bytes to spaces so the file is readable: ``bash cat /proc/self/cmdline | tr "\0" " " > cmdline.txt `` Open cmdline.txt. It shows the cat command that wrote it.
Show hint for step 3
The NUL bytes are the separators. tr can map them to spaces. The file is read by cat, so its command line starts with cat.
Check yourself Question 1 of 4
Concept Check The arguments file recall

Which file in /proc/PID holds the arguments of a process, separated by NUL bytes?

Recap & Key Takeaways

/proc is a generated view of kernel state, and its files are created when they are read rather than stored on disk.
Each numbered directory describes one process, with status, cmdline, cwd, exe, fd, and maps files inside.
/proc/self is a link to the process that opens it, which lets a program inspect itself without knowing its PID.
Some /proc files, such as cmdline and environ, separate fields with NUL bytes, so translate them before reading.
Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue