If you use linux then at some point or the other you must have heard that in Linux “Everything is a file”. If you haven’t heard it already then don’t worry, you’ll get to know it very soon. I have been exploring Linux Filesystem lately and this blog is a result of that exploration.
I came across the best description of this phrase in an answer on stackoverflow which can be seen below:
The “Everything is a file” phrase defines the architecture of the operating system. It means that everything in the system from processes, files, directories, sockets, pipes, … is represented by a file descriptor abstracted over the virtual filesystem layer in the kernel. The virtual filesytem is an interface provided by the kernel. Hence the phrase was corrected to say “Everything is a file descriptor“. Linus Torvalds himself corrected it again a bit more precisely: “Everything is a stream of bytes“. – (Source – StackOverflow)
If you have a linux machine handy right now do the following:
➜ ~ ls -l /proc/cpuinfo -r--r--r-- 1 root root 0 Aug 6 01:13 /proc/cpuinfo # Look at the file permissions carefully. No one has write access to this file. # Not even the root user. You can change the file permission using chmod and then # try modifying it as well using vim or gedit but your changes will be gone because # the next time you try opening that file OS will give you the latest cpuinfo.
/proc/cpuinfo is not a real text file. The Linux kernel is exposing this information to us as a file. Because the cpu information is made available to us as a file we can use tools which we use with any other file (e.g. cat, tail, watch, etc). Please note that /proc/cpuinfo is not an actual file, it just appears to be one.
So basically you can say that the /proc folder is a file system and files in that folder are stream of bytes made available to you by the kernel. So when you try doing an “ls” inside that folder or you try to open a file in that folder the OS simply gives you access to the data in kernel data structures in form of file. For more information on proc filesystem (procfs) I strongly recommend you to do “man proc”. Output of that command which is more relevant to this article is shown below.
NAME proc - process information pseudo-filesystem DESCRIPTION The proc filesystem is a pseudo-filesystem which provides an interface to kernel data structures. It is commonly mounted at /proc. Most of it is read-only, but some files allow kernel variables to be changed.
/dev folder in linux is also an interesting folder which exposes data in form of files. It’s an interesting folder and I will write more about it later. Though if you are interested to explore it further you can check out : http://www.tldp.org/LDP/Linux-Filesystem-Hierarchy/html/dev.html.
The philosophy behind “Everything is a file” in linux is really great. Do read this article on stackoverflow to know more about it.
In the next post of this series I will cover different file types in Linux and their importance.