Everything is a file

"Everything is a file" is an idea that Unix, and its derivatives, handle input/output to and from resources such as documents, hard-drives, modems, keyboards, printers and even some inter-process and network communications as simple streams of bytes exposed through the filesystem name space. Exceptions include semaphores, processes and threads.

The advantage of this approach is that the same set of tools, utilities and APIs can be used on a wide range of resources and a number of file types. When a file is opened, a file descriptor is created, using the file path as an addressing system. The file descriptor is then a byte stream I/O interface on which file operations are performed. File descriptors are also created for objects such as anonymous pipes and network socketsand therefore a more accurate description of this feature is Everything is a file descriptor.

Additionally, a range of pseudo and virtual filesystems exists which exposes internal kernel data, such as information about processes, to user space in a hierarchical file-like structure. These are mounted into the single file hierarchy.

An example of this purely virtual filesystem is under /proc that exposes many system properties as files. All of these files, in the broader sense of the word, have standard Unix file attributes such as an owner and access permissions, and can be queried by the same classic Unix tools and filters. However, this is not universally considered a fast or portable approach. Some operating systems do not mount /proc by default due to security or speed concerns, relying on system calls instead. It is, though, used heavily by Linux shell utilities, such as procps ps implementation and the widely installed on embedded systems BusyBox. Android Toolbox program depend on it as well.

Another example is sysfs, which is usually mounted to /sys, which exposes kernel data structures. sysfs provides functionality similar to the sysctl mechanism found in BSD operating systems, with the difference that sysfs is implemented as a virtual file system instead of being a purpose-built kernel mechanism. The philosophy behind sysfs is to represent each value with a dedicated file. In addition each file has a maximum size of  bytes.

For a kernel module there are three possibilities to use a file below /sys:

The standard sysfs API uses a dedicated terminology: A file is called an attribute, the function executed upon reading an attribute is called  and the one for writing an attribute.
 * module parameter
 * register new subsystem
 * debugfs: debugfs, mounted in /sys/kernel/debug.

Sysfs was derived from procfs between Linux kernel versions 2.5-2.6, initially as a dedicated filesystem to debug a new driver model. Both sysfs and procfs are memory-based. Sysfs contains directories for block devices, physical bus types, device classes (such as those used for graphics, networking, input or printing), firmware-specific objects and attributes, kernel modules and the power subsystem.

For example, writing  to /sys/power/state will trigger a suspend-to-RAM procedure.

Another example of files with specific behaviors are /dev/null and /dev/zero device files. Writes to them will be discarded. This can, for example, be used to redirect unneeded standard streams.