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.
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.
To know how big a block is on your system, run "blockdev --getbsz device" as root, e.g.:
sudo blockdev --getbsz /dev/sda1
Character special files
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
|Type name||Symbolic name||Bitmask|
|Block special file||S_IFBLK||0060000|
|Character special file||S_IFCHR||0020000|
|FIFO (first in, first out)||S_IFIFO||0010000|
How can I tell if a file is special?
Test for block special
test -b /dev/sda1; echo $? # check for block special, echo exit status of test
test -b /dev/null; echo $? # character special files are not block special
Test for character special
To determine if a file is character special, use "test -c file":
test -c /dev/null; echo $?
You can also check a file's type with stat:
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: -
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: -