Many Linux users encounter the /dev and /sys directories but may not fully understand their roles in the system. However, when working with disks, partitions, terminals (tty, pty), and other devices, it is useful to grasp their essence.
What is /dev?
The /dev directory contains special files that allow the operating system to interact with both physical devices (such as hard drives, cameras, printers) and virtual ones (such as /null, /random, /tty).
These files are not drivers themselves but serve as an interface between the kernel and hardware. When the system accesses them, the kernel uses appropriate drivers to process requests.
Types of Devices in /dev
When listing the contents of the /dev directory, you will see files with different prefixes:
$ ls -l /dev
brw-rw---- 1 root disk 8, 1 Feb 10 10:15 sdb1
crw-rw-rw- 1 root root 1, 3 Feb 10 10:15 random
prw-r--r-- 1 root root 0 Feb 10 10:15 pipe_data
srw-rw-rw- 1 root root 0 Feb 10 10:15 sock_comm
Notice the first character in each line:
b— block devicec— character devicep— named pipes— socket
Let's take a closer look at them.
1. Block Devices
Block devices transfer data in fixed-size blocks. An example is a hard drive or a USB stick.
Examples:
- Hard drives (HDD, SSD)
- Flash drives
- SD cards
2. Character Devices
Character devices transfer data character-by-character or in small chunks. Unlike block devices, they do not use file systems but operate with data streams.
Examples:
- Keyboard (/dev/input/eventX)
- Mouse (/dev/input/mice)
- Serial ports (/dev/ttyS0)
- Null device (/dev/null)
When data is written to /dev/null, it simply disappears. This is useful for discarding unwanted program output.
3. Named Pipes
Named pipes (FIFOs) are similar to the | operator but allow processes to communicate via a file.
Example: /dev/fifo_pipe allows processes to exchange data through a named pipe.
4. Sockets
Sockets are used for interprocess communication both within the system and over a network. They allow programs to exchange data without using files.
Examples:
/dev/log— used for logging messages- Unix sockets such as
/dev/socket/my_appallow process communication without network involvement
Major and Minor Device Numbers
Each device in /dev has two numbers:
8, 1 Feb 10 10:15 sdb1
- Major (8) — identifies the device driver
- Minor (1) — unique identifier within the driver
-
For example, a disk partition will have different minor numbers but the same major number since it uses the same driver.
How Are /dev Files Created?
Files in
/devare not permanent — they are created at boot time or when new devices are connected. They are managed byudevdormdevd, which:- Detect new devices
- Assign appropriate drivers
- Create corresponding files in
/dev
What is Hidden in /sys?
/sysis a virtualsysfsfile system that provides information about system configuration and hardware. Unlike/dev,/sysrepresents device structures and properties.Examples of useful files in
/sys:/sys/class/net/eth0/address— MAC address of a network interface/sys/block/sda/size— disk size ofsdain 512-byte sectors/sys/devices/system/cpu/cpu0/cpufreq/scaling_governor— current CPU frequency scaling mode
Practical Uses of Device Files
- Writing to the null device
echo "Hello" > /dev/nullThe data will disappear without a trace.
- Reading random data
head -c 10 /dev/randomOutputs 10 random bytes.
- Reading random data
3. Reading a network interface MAC address
cat /sys/class/net/eth0/address
4. Creating a named pipe and transmitting data
mkfifo /tmp/mypipe
echo "test" > /tmp/mypipe & cat /tmp/mypipe
The data from echo is passed through the named pipe to cat.
Conclusion
The /dev and /sys directories play a crucial role in Linux, bridging the kernel and hardware. Understanding their structure enables more effective system management, working with disks, terminals, and other hardware components.