File in Linux
In Linux (and Unix-like systems), **a file is a logical container used to store data*Everything in Linux is considered a file — not just documents or programs, but also directories, hardware devices, sockets, and even processes.
Types of Files in Linux
Regular files: These include text files, images, executables, logs, etc.
- Example:
/home/user/document.txt
Directories: Special files that contain references to other files.
- Example:
/etc
,/home
,/var/log
Character device files: Represent devices that handle data character by character (e.g., keyboards, serial ports).
- Example:
/dev/tty0
Block device files: Represent devices that transfer data in blocks (e.g., hard drives, USBs).
- Example:
/dev/sda
Symbolic (soft) links: Pointers to other files or directories.
- Example:
shortcut -> /real/location/of/file
Pipes (named and unnamed): Used for inter-process communication.
- Example: FIFO files created with
mkfifo
Sockets: Used for network communication between processes.
Key Properties of a File
You can view file properties using commands like ls -l
:
-rw-r--r-- 1 user group 4096 Jul 22 12:30 notes.txt
Field | Meaning |
---|---|
- | File type (- for regular file, d for directory) |
rw-r--r-- | File permissions (owner/group/others) |
1 | Number of hard links |
user | Owner of the file |
group | Group owner |
4096 | File size in bytes |
Jul 22 12:30 | Last modified date and time |
notes.txt | File name |
Character Device File in Linux
A character device file (or character special file) is a type of file in Linux that provides unbuffered, direct access to hardware devices, transferring data one character (byte) at a time.
These are typically used for devices like:
- Keyboards
- Serial ports
- Mice
- Sound cards
- Terminals (TTY)
Key Features
Feature | Description |
---|---|
Access | Reads/writes one byte at a time (no block buffering) |
Used for | Devices needing sequential data transfer like serial interfaces |
Created with | mknod or udev |
Located in | /dev directory |
Major/Minor Number | Identify the device driver (major) and specific device (minor) |
Example of a Character Device File
Run ls -l /dev/tty
:
$ ls -l /dev/tty
crw-rw-rw- 1 root tty 5, 0 Jul 22 10:41 /dev/tty
Field | Meaning |
---|---|
c | Indicates it’s a character device |
rw-rw-rw- | Permissions for owner, group, others |
5, 0 | Major number 5, minor number 0 identifying the device |
Another example:
$ ls -l /dev/console
crw--w---- 1 root tty 5, 1 Jul 22 10:41 /dev/console
Difference: Character vs Block Devices
Feature | Character Device | Block Device |
---|---|---|
Data Transfer | One character at a time | In blocks (e.g., 512 bytes) |
Buffered | No | Yes |
Examples | /dev/tty , /dev/random | /dev/sda , /dev/sdb1 |
Use Case | Terminals, serial ports | Hard drives, USB drives |
Creating a Character Device (Advanced)
sudo mknod /dev/mydevice c 100 0
c
— character device100
— major number0
— minor number
You’d only do this if writing custom drivers or for manual testing. In modern systems, udev
automatically manages device files.
Summary
- Everything is a file in Linux, including directories, devices, and even processes.
A character device file is a Linux interface to hardware devices that transmit data sequentially, byte-by-byte. These files are essential for low-latency communication with peripherals and are represented in
/dev
. - Files have types, permissions, owners, and metadata.
- Linux treats files uniformly, simplifying interaction with data and devices.
Let me know if you want visuals or command outputs related to file inspection or manipulation.