Why is it possible to look at the contents of a block device's file like /dev/sda
using cat
or a hex editor?
Why can I not do the same for a character device like /dev/pts/3
?

- 4,790

- 123
3 Answers
This isn't (necessarily) a distinction between character and block devices. For example, /dev/urandom
is a character device and you can certainly use cat
on it.
In your case, /dev/sda
refers to storage that already exists, so reading from it reads the existing storage. However, /dev/pts/3
is a pseudo-terminal and reading from it blocks (waits) until the next character of input is ready.
An additional difference is that you you can seek to a particular location on a device which provides access to persistent storage, but you can't seek on a terminal.

- 7,053
- 2
- 32
- 34
Devices on Unix act like files, but are not the same. For some devices it made more sense to the implementer of the device software for both reading and writing, for others just implementing just one of those made more sense.
It is not because /dev/pts/3
is a character device that you cannot read from it, other such devices exist where you can. For the pts
device I could see that reading back could mean getting the characters back from the screen as a linear array of screenwidth*screenheight
length, but that is not extremely useful nor efficient.
Although it is getting a bit old, Linux Device Drivers, chapter 3 is an interesting read on the subject.

- 79,293
This explains what /dev/pts/3
is for and what you can do with it. /dev/sda
points to your HDD/SSD which you can read from and write to. When you edit data on /dev/sda
, you are writing directly to the drive and not necessarily in a partition. If you do this you can corrupt a partition so it's generally not used to save data. In some cases, however, it is convenient to write directly to the disk (e.g., dd
for securely erasing a disk). In Unix everything is a file, but you can't read and/or write to every file.