Skip to Content
This project is a work in progress. If you have any questions or suggestions, feel free to contact me.
LinuxFile OperationsFiles in Linux

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
FieldMeaning
-File type (- for regular file, d for directory)
rw-r--r--File permissions (owner/group/others)
1Number of hard links
userOwner of the file
groupGroup owner
4096File size in bytes
Jul 22 12:30Last modified date and time
notes.txtFile 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

FeatureDescription
AccessReads/writes one byte at a time (no block buffering)
Used forDevices needing sequential data transfer like serial interfaces
Created withmknod or udev
Located in/dev directory
Major/Minor NumberIdentify 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
FieldMeaning
cIndicates it’s a character device
rw-rw-rw-Permissions for owner, group, others
5, 0Major 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

FeatureCharacter DeviceBlock Device
Data TransferOne character at a timeIn blocks (e.g., 512 bytes)
BufferedNoYes
Examples/dev/tty, /dev/random/dev/sda, /dev/sdb1
Use CaseTerminals, serial portsHard drives, USB drives

Creating a Character Device (Advanced)

sudo mknod /dev/mydevice c 100 0
  • c — character device
  • 100 — major number
  • 0 — 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.

Last updated on