Character Special File

Character Special File


Special file

  • In a computer operating system, a special file is one type of file that may be 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 be used to 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.

  • 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.

    Type name Symbolic name Bitmask

  • Socket S_IFSOCK 0140000

  • Symbolic link S_IFLNK 0120000

  • Regular file S_IFREG 0100000

  • Block special file S_IFBLK 0060000

  • Directory S_IFDIR 0040000

  • Character special file S_IFCHR 0020000

  • FIFO (named pipe) S_IFIFO 0010000

    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 of 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 $?
    0
    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: -


    What is an Operating System? » Computer Shortcut Keys and their Functions » Keyboard Function Keys » Computer Basics - Hardware - Software - Parts

    Short Stories for Kids - Moral Stories – English Short Stories for Children - Moral Stories for Kids - Stories for Kids - Funny Story for Kids - Scary Stories for Kids - Really Funny Short Stories - Bedtime Stories
    Proverb Stories
    Powerful Motivational Quotes for Students » Success Quotes » English Short Stories for Kids

    Cabin Crew Jobs & Career Advice » Secretary Job Description » Receptionist Job Description » Top 100 Interview Questions and Answers » How to Prepare for an Interview » How to Write a CV » How to Choose a Career » Computer Shortcut Keys and their Functions

    Scholarships in Australia » Scholarships in Austria » Scholarships in Belgium » Scholarships in Canada » Scholarships in Germany » Scholarships in Ireland » Scholarships in Italy » Scholarships in Japan » Scholarships in Korea » Scholarships in Netherlands » Scholarships in Switzerland » Scholarships in UK » Scholarships in USA

    Scholarships for Study in Africa » Scholarships for African Students » Undergraduate Scholarships » African Women Scholarships & Grants Scholarships for Study in Africa » Scholarships for African Students » Undergraduate Scholarships »

  • Click here to post comments

    Join in and write your own page! It's easy to do. How? Simply click here to return to Computer Basics FAQ.