≡ Menu

hardware device

Linux / UNIX: Device files

Q. Can you explain me what is device files and how do I access or see device files? Why UNIX / Linux has device files?

A. Under Linux and UNIX each and every hardware device treated as a file. A device file allows to accesses hardware devices so that end users do not need to get technical details about hardware.

In short, a device file (also called as a special file) is an interface for a device driver that appears in a file system as if it were an ordinary file. This allows software to interact with the device driver using standard input/output system calls, which simplifies many tasks.

Device file two types

There are two types of device files based upon how data written to them and read from them is processed by the operating system and hardware:

  • Character special files or Character devices
  • Block special files or Block devices

Understanding Character special files or Character devices

  • Talks to devices in a character by character (1 byte at a time)
  • Examples: Virtual terminals, terminals and serial modems etc

Understanding Block special files or Block devices

  • Talks to devices 1 block at a time ( 1 block = 512 bytes to 32KB)
  • Examples: Hard disk, DVD/CD ROM, and memory regions etc

Why use device files?

Device file allows transparent communication between user space applications and computer hardware.

Device file location

All device files are stored in /dev directory. Use cd and ls command to browse the directory:
cd /dev/
ls -l

How do I find out the device file type?

Simply use ls -l command:
ls -l /dev
Look for file's type in the first column output.

A character device is marked with a c as the first letter of the permissions strings.
$ ls -l /dev/console
A block device is marked with a b as the first letter of the permissions strings:
$ ls -l /dev/sdb1

Q. I have a compact flash card reader hardware device connected to USB port of computer system. Now how do I mount and access a compact flash card connected to this Linux system (Fedora Core)?

A. Your compact flash card will automatically detected by Linux. First create a mount point (type the following command as root user):

# mkdir -p /mnt/cflashcard
If you don't have any SCSI device, your first partition on a compact flash card should be /dev/sda1 (second will be /dev/sda2 and so on). If you have 1 SCSI device attached, your first partition on a compact flash card should be /dev/sdb1.

Mount partition using mount command:
# mount /dev/sda1 /mnt/cflashcard
To see or access files:
# cd /mnt/cflashcard
# ls -l

Open /etc/fstab file:
# vi /etc/fstab
Add an entry (append) to /etc/fstab file:
/dev/sda1 /mnt/cflashcard auto user,rw,exec 0 0
Save and close the file.

How do I unmount or unplug card?

First unmount card with umount command and then unplug card:
# umount /mnt/flash