Most hardware devices offer a file-like API. This is done because it makes both the design of the operating system and the design of applications simpler. The OS only has to have a file API and not a separate terminal API and a separate disk API and a separate sound API and so on. Applications that are not using features specific to a particular kind of hardware can use the file API without caring whether they are accessing a regular file or a hardware device.
A lot of hardware has capabilities that are specific to a type of device. Applications can invoke these capabilities via ioctl. Some hardware doesn't appear as files because you can't simply read or write a stream of bytes to it. For example Linux doesn't expose network interfaces as device files, because network interfaces work on packets, not on individual bytes.
Historically, terminals were hardware devices. Nowadays most terminals are provided by emulators, either in a graphical environment or over the network. Nonetheless even pseudo-terminals appear like hardware devices, because the kernel contains special handling to track which processes are running on which terminal.
On every unix variant, /dev/tty
means “the current terminal for this process”. In other words, whenever a process opens that file, it designates the process's controlling terminal). This allows a process to interact via its terminal even when its standard input and output descriptors have been redirected.
Each terminal has an associated device file, which is either a hardware terminal (tty, for example /dev/tty1
, /dev/tty2
, … for the text mode virtual consoles on Linux, or /dev/ttyS0
, … for serial ports on Linux) or an emulated terminal (pty, short for pseudo-terminal; /dev/pts/NUMBER
on Linux). This is the file through which processes exchange data with the terminal driver or emulator.
It's because terminals are files that you can run applications and display their output to the terminal. When you run a program at the command line, by default, its output goes to the terminal, but you can redirect it to a file.