Learning note
Files That Don’t Live on Disk: How Pseudo Filesystems Work
I have been refining my knowledge on Linux filesystems and the filesystem hierarchy. It was interesting to learn about virtual (pseudo) filesystems, which do not actually store data on disk but generate it on the fly. /𝘱𝘳𝘰𝘤 and /𝘴𝘺𝘴 are two good examples.
I used to assume that anything living under / had to be backed by actual blocks on disk somewhere, but that is not true with procfs, sysfs and other virtual file systems.
When you run 𝘤𝘢𝘵 /𝘱𝘳𝘰𝘤/𝘮𝘦𝘮𝘪𝘯𝘧𝘰, the read() call does not get routed to a disk driver, rather, the VFS (virtual filesystem layer) routes it to a 𝘧𝘶𝘯𝘤𝘵𝘪𝘰𝘯 inside the kernel that formats the live state of the memory subsystem into text, right at that moment. There is no file sitting on a disk waiting to be read. The "file" is just a hook that triggers a kernel function.
This explains why /proc takes up 0 bytes on disk, because nothing there was ever written to disk in the first place. Everything under /proc disappears on reboot, because it was never persisted, only generated on demand.
Directories like /proc/1234 appear and vanish as processes start and stop, because they are not directories in the traditional sense, they are a live reflection of the process table.
/sys works on the same principle, except it exposes the kernel's device and driver model.
So apparently, "everything is a file" in Linux does not mean everything is stored on disk. It means everything exposed through the filesystem responds to the same open/read/write interface. What happens underneath that interface is entirely up to whichever filesystem driver is handling it - ext4 hits a disk, procfs runs a function. Same interface but a completely different backing behavior.