Everything Is a File & Linux File Types
In this article
In Linux, "everything is a file" means that nearly all system resources—devices, processes, pipes, sockets, and even kernel data structures—are represented as files in the filesystem, accessible through the same read/write operations. This design simplifies interaction with the system, but it also means that Linux has several distinct file types beyond the regular files and directories you might already know.
Background: The Unix Philosophy
The concept originates from Unix, developed at Bell Labs in the late 1960s and 1970s. The designers wanted a uniform interface: if a program can read from a file, it can read from a terminal, a printer, or a network connection. This abstraction made it possible to build small, composable tools that work together through standard input/output streams.
Linux, created by Linus Torvalds in 1991, was written to be a free Unix-like operating system and adopted this same philosophy. The filesystem is the central organizing structure, and the kernel exposes system information and device access through special files. This is why you can use commands like cat to read the contents of a device or ls to list system information as if it were a directory.
How "Everything Is a File" Works
The Linux kernel provides a virtual filesystem (VFS) that translates file operations into calls to specific drivers. When you open a file, the kernel doesn't care whether it's a regular file on disk, a keyboard, or a network socket—it just hands you a file descriptor. Subsequent read, write, and close operations are routed to the appropriate driver.
This abstraction is visible in the /dev directory, which contains device files. For example:
/dev/sdarepresents the first SCSI/SATA hard disk./dev/tty0is the first virtual terminal./dev/nulldiscards all data written to it.
Similarly, /proc and /sys are virtual filesystems that expose kernel and hardware information as files. Reading /proc/cpuinfo shows processor details, and writing to /sys/class/backlight/intel_backlight/brightness can change screen brightness. These aren't real files on disk; they're generated on the fly by the kernel.
The 7 Linux File Types
When you run ls -l, the first character of the output indicates the file type. There are seven standard types:
- Regular file (-) – ordinary files containing data, programs, or text.
- Directory (d) – a container that maps filenames to inodes.
- Symbolic link (l) – a reference to another file or directory, like a shortcut.
- Character device (c) – a device that transfers data one character at a time, e.g., a keyboard or serial port.
- Block device (b) – a device that transfers data in fixed-size blocks, e.g., a hard disk or SSD.
- Named pipe (p) – also called a FIFO, used for inter-process communication.
- Socket (s) – an endpoint for network or local inter-process communication.
Each type has its own purpose and behavior. For example, reading from a character device may produce data in real time, while reading from a block device returns raw disk content.
Special File Systems: /proc and /sys
These virtual filesystems are where the "everything is a file" philosophy is most apparent. /proc is a pseudo-filesystem that presents process and system information. Each running process has a directory named after its PID, containing files like cmdline (the command line) and status (process state).
/sys exposes kernel objects, including devices, drivers, and kernel parameters. It is used by tools like udev to manage device nodes dynamically. Writing to certain files in /sys can change hardware settings, such as enabling or disabling a network interface.
These filesystems are not stored on disk; they are created in memory when the system boots. That's why they occupy zero disk space and disappear when the system shuts down.
Working with Different File Types
Most users interact with regular files and directories, but knowing how to handle the others is useful for system administration and scripting.
- Symbolic links are created with
ln -s target link_name. They can point to files or directories and may span filesystems. - Named pipes are created with
mkfifo. They allow two processes to communicate without using a temporary file. For example,mkfifo mypipethencat mypipein one terminal andecho "hello" > mypipein another. - Unix sockets are used for local inter-process communication. Tools like
mysqlandnginxoften create socket files under/runor/var/run. - Device files are usually managed by udev, but you can create them manually with
mknodif needed (requires root).
To identify a file's type, use file command, which reads the file's content and reports its type. For example, file /dev/sda returns "block special" and file /proc/cpuinfo returns "ASCII text".
Common Misconceptions and Pitfalls
A frequent misconception is that "everything is a file" means every file is a regular file. In reality, the phrase means everything is represented as a file descriptor, but the underlying behavior differs. For instance, you cannot seek on a socket or a pipe the way you can on a regular file.
Another pitfall is accidentally reading from a device file and getting binary garbage. For example, cat /dev/sda will dump raw disk data to your terminal, which can be dangerous if you're not careful. Always redirect output to a file or use dd with appropriate options.
Also, some files in /proc are read-only, while others are write-only. Trying to write to a read-only file will produce a permission error, even if you are root. The kernel enforces these constraints.
Alternatives and Comparisons
Other operating systems use different models. Windows, for example, treats devices as separate objects in the \\.\ namespace and uses the registry for system configuration. macOS, being Unix-based, follows the same "everything is a file" model as Linux, though it adds some proprietary layers like IOKit.
Plan 9, an operating system developed at Bell Labs, took the idea further by making even network resources appear as files. Linux has adopted some of these ideas, such as /net in early versions, but never fully embraced them.
The main advantage of the Unix model is simplicity and uniformity: any program that can read a file can read from a device or a pipe. The downside is that some operations, like seeking or memory-mapping, don't work on all file types, which can confuse developers coming from other platforms.
Frequently Asked Questions
Does "everything is a file" include directories?
Yes, directories are files, but they contain a special structure that maps filenames to inodes. You can't read a directory as a regular stream of bytes; you must use system calls like readdir to iterate over its entries. The kernel treats directories as a distinct file type.
How can I see the type of a file?
Use ls -l to see the file type in the first character of the output. Alternatively, the file command examines the content and prints a human-readable description, such as "ELF 64-bit LSB executable" or "FIFO pipe".
Can I create my own device file?
Yes, with mknod as root. You need to know the major and minor numbers that identify the device driver. However, it's rarely necessary because udev creates device nodes automatically when hardware is detected.
Why does /proc show files that don't exist on disk?
/proc is a virtual filesystem maintained by the kernel. The files are not stored on any physical medium; they are generated dynamically when accessed. This allows the kernel to expose current system state without writing to disk.
Is a socket file the same as a network port?
No. A socket file is a filesystem entry used for local inter-process communication (Unix domain sockets). A network port is a numerical identifier used with TCP/UDP. Unix sockets don't use IP addresses or ports; they use filesystem paths.
What happens if I delete a socket file?
Deleting a socket file only removes the filesystem entry. The socket itself continues to exist as long as the process that created it is running, but new clients can no longer connect because they can't find the path. The process must be restarted to recreate it.