Everything in Linux is a File

Listen to this Post

Featured Image
This is a fundamental concept in Linux. Not just files on disk, but virtual files. Everything—directories, devices (like external hard drives or keyboards), and even inter-process communication mechanisms like pipes and sockets—is represented as files. This uniform approach allows for consistent handling and manipulation of diverse system resources using standard file operations (read, write, etc.).

For example, you can use standard command-line tools like `cat` to read data directly from a device file (though doing so for some devices might not be very useful or safe!).

As Linus Torvalds himself said: “Everything is a stream of bytes.”

You Should Know:

1. Device Files in `/dev`

Linux represents hardware devices as files in /dev. For example:
– `/dev/sda` – First SATA/SCSI disk
– `/dev/tty` – Terminal device
– `/dev/null` – Null device (discards all data)

Example Commands:

 List all block devices (disks)
lsblk

Read from a device (use with caution!)
sudo cat /dev/sda | head -c 512  First 512 bytes of disk

Write to a device (DANGEROUS – can corrupt data!)
echo "test" | sudo tee /dev/sda 

2. Pipes and Sockets as Files

  • Pipes (|) allow command output to be passed as input to another command.
  • Sockets (e.g., /run/systemd/journal/socket) enable inter-process communication.

Example:

 Create a named pipe
mkfifo mypipe

Write to the pipe in one terminal
echo "Hello from pipe" > mypipe

Read from the pipe in another terminal
cat < mypipe 
  1. ProcFS (/proc) – Kernel & Process Info
    The `/proc` directory contains runtime system information. Each process has a subdirectory (e.g., `/proc/1` for PID 1).

Useful Commands:

 View CPU info
cat /proc/cpuinfo

Check memory usage
cat /proc/meminfo

List open files by a process
ls -l /proc/$$/fd  $$ = current shell PID 
  1. SysFS (/sys) – Hardware & Kernel Settings

`/sys` exposes kernel parameters and hardware details.

Example:

 Check connected USB devices
ls /sys/bus/usb/devices

Adjust screen brightness (if supported)
echo 500 | sudo tee /sys/class/backlight/intel_backlight/brightness 

5. File Descriptors & Redirections

Everything in Linux uses file descriptors (stdin=0, stdout=1, stderr=2).

Examples:

 Redirect stderr to a file
some_command 2> error.log

Send output to both stdout and a file
echo "Log this" | tee output.log

Read from a file descriptor
exec 3< myfile.txt 
cat <&3 

6. Special Files (`/dev/null`, `/dev/zero`, `/dev/random`)

– `/dev/null` – Discards all input
– `/dev/zero` – Produces null bytes
– `/dev/random` – Generates random data

Usage:

 Discard command output
noisy_command > /dev/null

Generate random data
head -c 1K /dev/random > random.bin 

What Undercode Say:

The “Everything is a file” philosophy simplifies Linux administration and scripting. By treating devices, processes, and system info as files, Linux ensures a consistent interface for automation and debugging. However, misuse (like writing directly to /dev/sda) can lead to data loss. Always verify commands before execution.

Expected Output:

$ ls -l /dev/sda 
brw-rw- 1 root disk 8, 0 May 16 10:00 /dev/sda

$ cat /proc/meminfo | grep MemTotal 
MemTotal: 8023424 kB

$ echo "Linux is powerful" | tee /dev/pts/0  Send to a terminal 

Prediction:

As Linux evolves, this file-based abstraction will extend further into cloud-native systems (e.g., Kubernetes volumes, eBPF filesystems). Expect more `/sys` and `/proc` enhancements for real-time system observability.

(No URLs extracted as the original post was conceptual rather than linking to external resources.)

References:

Reported By: Divine Odazie – Hackers Feeds
Extra Hub: Undercode MoN
Basic Verification: Pass ✅

Join Our Cyber World:

💬 Whatsapp | 💬 Telegram