In Linux/Unix systems, device files (also called special files) are interfaces to hardware devices and are found in the /dev directory. These files do not store data, but act as access points to device drivers in the kernel.
✅ Types of Device Files in Linux
There are two main types of device files:
🔹 1. Character Devices (c)
Represented with a c in file permissions.
Data is transferred byte-by-byte (like a stream).
Used for devices where direct sequential access is expected.
📌 Examples:
/dev/tty # Terminal
/dev/console # System console
/dev/null # Discards input
/dev/random # Random number generator
📌 List them:
ls -l /dev | grep '^c'
🔹 2. Block Devices (b)
Represented with a b in file permissions.
Data is transferred in blocks (e.g., 512 bytes, 4 KB).
Support random access, suitable for filesystems.
📌 Examples:
/dev/sda # First SATA hard disk
/dev/sdb # Second SATA disk
/dev/loop0 # Loop device (mounting ISO)
📌 List them:
ls -l /dev | grep '^b'
🔹 Major and Minor Numbers
Every device file has:
Major number: identifies the driver associated with the device.
Minor number: identifies a specific device (instance) the driver controls.
📌 View using:
ls -l /dev/sda
# brw-rw---- 1 root disk 8, 0 Jun 23 10:00 /dev/sda
# ↳ 8 = major, 0 = minor
🔹 3. Other Special Device Types
📌 List all:
ls -l /dev | grep -E '^[cbpsld-]'
🔸 FAANG-Level 20 Questions and Answers
📌 Conceptual & System-Level
Q: What’s the difference between block and character devices?
A: Block devices allow random access in fixed-size blocks (like disks). Character devices are sequential and unbuffered (like serial ports).Q: What do major and minor numbers represent?
A: Major = driver; Minor = device instance.Q: How can you list all block devices in the system?
A: Use lsblk, ls -l /dev | grep '^b', or cat /proc/partitions.Q: What happens if you delete a device file (e.g., /dev/null)?
A: The system won't work correctly. You can recreate using mknod.Q: How to recreate /dev/null?
A: sudo mknod -m 666 /dev/null c 1 3Q: Which type of device file is /dev/sda?
A: Block device.Q: Can block devices be used without mounting?
A: Yes, using tools like dd, but mounting is needed for filesystem access.Q: What command shows all device drivers and their major numbers?
A: cat /proc/devicesQ: How do loop devices work?
A: They map a file as a block device, used to mount disk images.Q: What is the difference between /dev/random and /dev/urandom?
A: /dev/random is blocking (more secure); /dev/urandom is non-blocking (faster, uses PRNG).
📌 Advanced and Use-Case Based
Q: How can a program interact with a device file?
A: Via system calls like open(), read(), write() just like regular files.Q: Can a device have both character and block interfaces?
A: Yes, some devices expose both (e.g., loop devices).Q: What is /dev/pts?
A: Pseudo terminal slaves used in terminal emulators (like SSH).Q: How does udev relate to device files?
A: udev dynamically manages /dev entries based on hardware events.Q: What’s /dev/shm?
A: Temporary shared memory filesystem, often used for IPC.Q: How do sockets in /dev differ from regular files?
A: They facilitate communication (e.g., /dev/log for system logs).Q: What are /dev/fb0, /dev/input/mice?
A: Framebuffer and input devices for graphical and peripheral input.Q: Why does ls -l /dev/sda show b at the beginning?
A: It is a block device.Q: How do I find the device file for a USB stick?
A: Use dmesg | tail, lsblk, or udevadm info.Q: How does Linux know what driver to use for a device file?
A: Based on the major number, which maps to a kernel driver.