In a computer operating system, a special file is a type of file stored in a file system. A special file is sometimes also called a device file.

The purpose of a special file is to expose the device as a file in the file system. A special file provides a universal interface for hardware devices (and virtual devices created and used by the kernel), because tools for file I/O can access the device.

When data is red from or written to a special file, the operation happens immediately, and is not subject to conventional filesystem rules.

In Linux, there are two types of special files: block special file and character special file.

Block special files

A block special file acts as a direct interface to a block device. A block device is any device which performs data I/O in units of blocks.

  • Block special files

  • Character special files

  • Linux file types

  • How can I tell if a file is special?

  • Test for block special

  • Test for character special

  • Using stat

  • Test for block special

  • Test for character special

  • Using stat

Examples of block special files:

  • /dev/sdxn — mounted partitions of physical storage devices. The letter x refers to a physical device, and the number n refers to a partition on that device. For instance, /dev/sda1 is the first partition on the first physical storage device.
  • /dev/loopn — loop devices. These are special devices which allow a file in the filesystem to be used as a block device. The file may contain an entire filesystem of its own, and be accessed as if it were a mounted partition on a physical storage device. For example, an ISO disk image file may be mounted as a loop device.

If you want to know how big a block is on your system, run “blockdev –getbsz device” as root, e.g.:

sudo blockdev –getbsz /dev/sda1

4096

In this example, the block size is 4096 bytes (4 kibibytes).

Character special files

A character special file is similar to a block device, but data is written one character (eight bits, or one byte) at a time.

Examples of character special files:

  • /dev/stdin (Standard input.)
  • /dev/stdout (Standard output.)
  • /dev/stderr (Standard error.)
  • /dev/random (PRNG which may delay returning a value to acquire additional entropy.)
  • /dev/urandom (PRNG which always returns a value immediately, regardless of required entropy.)
  • /dev/null (The null device. Reading from this file always gets a null byte; writing to this file successfully does nothing.)

Linux file types

In the Linux kernel, file types are declared in the header file sys/stat.h. The type name, symbolic name, and bitmask for each Linux file type is listed below.

How can I tell if a file is special?

Test for block special

In bash, the command “test -b file” returns an exit status of 0 if file is block special, or 1 if file is another type or does not exist.

test -b /dev/sda1; echo $? # check for block special, echo exit status of test

0

test -b /dev/null; echo $? # character special files are not block special

1

Test for character special

To determine if a file is character special, use “test -c file”:

test -c /dev/null; echo $?

Using stat

You can also check a file’s type with stat:

stat /dev/sda1

File: /dev/sda1 Size: 0 Blocks: 0 IO Block: 4096 block special file Device: 6h/6d Inode: 7998 Links: 1 Device type: 8,1 Access: (0660/brw-rw—-) Uid: ( 0/ root) Gid: ( 6/ disk) Access: 2018-07-08 06:41:25.540000000 -0400 Modify: 2018-07-08 06:41:25.540000000 -0400 Change: 2018-07-08 06:41:25.540000000 -0400 Birth: -

stat /dev/random

File: /dev/random Size: 0 Blocks: 0 IO Block: 4096 character special file Device: 6h/6d Inode: 6518 Links: 1 Device type: 1,8 Access: (0666/crw-rw-rw-) Uid: ( 0/ root) Gid: ( 0/ root) Access: 2018-07-08 06:41:19.676000000 -0400 Modify: 2018-07-08 06:41:19.676000000 -0400 Change: 2018-07-08 06:41:19.676000000 -0400 Birth: -

Inode, Operating system terms, Standard input (stdin), Standard output (stdout)