Introduction

One of the neat ideas in Linux (and other similar systems) is that it treats all peripheral devices (such as disk drives, keyboard, console, memory, mouse etc) as a file! These files are contained in the /dev directory. For example, a program (with appropriate permissions) can simply read the file /dev/sda and it would access the first hard drive on the system. The operating system converts read to the /dev/sda file into appropriate hardware commands to access the physical hard drive. Thus the program does not need to know about the specific commands needed to talk to the device in question. The system does this behind the scenes using device driver software (also part of the operating system).

For example, here is the listing of all drives on a system that has three physical drives. The drive /dev/sda is divided into two partitions, while drives /dev/sdb and /dev/sdc have one partition each. Partitions allow sections of the storage device to be isolated from each other. This allows storage space to be dedicated for specific areas so that running out of space in one area does not affect the functionality of another area.

[alice@localhost ~]$ ls -l /dev/sd*
brw-rw---- 1 root disk 8,  0 Nov 23 05:56 /dev/sda
brw-rw---- 1 root disk 8,  1 Nov 23 05:56 /dev/sda1
brw-rw---- 1 root disk 8,  2 Nov 23 05:56 /dev/sda2
brw-rw---- 1 root disk 8, 16 Nov 23 05:56 /dev/sdb
brw-rw---- 1 root disk 8, 17 Nov 23 05:56 /dev/sdb1
brw-rw---- 1 root disk 8, 32 Nov 23 05:56 /dev/sdc
brw-rw---- 1 root disk 8, 33 Nov 23 05:56 /dev/sdc1

Note the “b" in front of the listing: that denotes that this device operates in blocks of data instead of a stream of characters. A terminal device acts as a stream of characters.

When we login, we get a terminal device to which the characters we type and receive are sent. The tty command tells which terminal we are using. For example:

[alice@localhost ~]$ whoami
alice
[alice@localhost ~]$ tty
/dev/pts/1
[alice@localhost ~]$ ls -l /dev/pts/1
crw------- 1 alice tty 136, 1 Nov 27 22:25 /dev/pts/1
[alice@localhost ~]$ date > /dev/pts/1
Mon Nov 27 22:25:22 MST 2017

Note that only we can read from or write to our terminal. It is often convenient to refer to our terminal in a generic way. The device /dev/tty is a synonym for our login terminal, whatever terminal you are actually using.

[alice@localhost ~]$ date > /dev/tty
Mon Nov 27 22:26:24 MST 2017

Sometimes we want to run a command but don't care what output is produced. We can redirect the output to a special device /dev/null, which causes the output to be thrown away. One common use is to throw away regular output so the error messages are more easily visible. For example, thetime command reports the CPU usage on the standard error, so we can time commands that generate lots of output by redirecting their standard output to /dev/null. See below.

[alice@localhost sandbox(master)]$ time sort /usr/share/dict/linux.words  > /dev/null

real    0m0.147s
user    0m0.439s
sys     0m0.021s