6

So I recently asked this question, which lead me to run this command:

$ du -sk /dev/*

Which outputs about 353 different entries that look like this:

0   /dev/sdt
0   /dev/stderr
0   /dev/stdin
0   /dev/stdout
0   /dev/systrace
0   /dev/tty
0   /dev/tty.Bluetooth-Incoming-Port
0   /dev/tty.Bluetooth-Modem
0   /dev/ttyp0
0   /dev/ttyp1

Can someone please elaborate on what these directories are? And why are they all zero bytes?

Ichigo Kurosaki
  • 103
  • 1
  • 8
  • 1
    I don't agree that this is a duplicate question. The duplicate question simply explains the general nature of the device nodes at /dev/* but the accepted answer on the post you claim is a duplicate doesn't seem to mention the zero-byte file size, and my question is specifically asking about file size. – Ichigo Kurosaki Jan 02 '15 at 11:41

1 Answers1

16

They're device nodes:

In Unix-like operating systems, a device file or special file is an interface for a device driver that appears in a file system as if it were an ordinary file. [...] They allow software to interact with a device driver using standard input/output system calls, which simplifies many tasks and unifies user-space I/O mechanisms.

In other words, they're not normal files, although they may be organized into regular directories. Historically, these were actual nodes created on disk that contained a smidge of information for the kernel. On contemporary linux systems, this has been replaced with a special in-memory filesystem, devtmpfs, much like /proc is an in-memory procfs and /sys an in-memory sysfs. That's why they are listed as having 0 bytes -- they are direct interfaces with kernel.

As to what they all are, there is a section about "Naming Conventions" in that wikipedia article, although this is not binding or universal (they may be renamed from userspace, which e.g., RedHat/Fedora derived udev installations will do). You can get another couple of clues from the major/minor number, shown in two columns left of the date with ls -l (where "size" would be). Those numbers correspond to entries in /sys/dev/block or /sys/dev/char.

Keep in mind most of these devices are virtual and may have little or nothing to do with any particular part of the hardware. For example, tty devices are the virtual terminals you can navigate with ctrl-alt-F[N]; pt devices (there's a directory for them) are pseudo-terminals used in GUI emulators. There are various other more esoteric things such as shared memory segments (under shm -- these have actual sizes). Again, the purpose is just to allow for a standardized interface to the kernel; the concept of "device" is very loose.

goldilocks
  • 87,661
  • 30
  • 204
  • 262
  • Most contemporary Linux distributions don't actually use devfs, but an ordinary ramdisk (tmpfs) filesystem whose content is managed by the udev system running outside the kernel. – hmakholm left over Monica Dec 28 '14 at 21:56
  • @HenningMakholm I hadn't noticed the distinction between devfs and devtmpfs (corrected). udev (which is a userspace entity) may manage aspects of this but AFAICT the preliminary set-up is still by the kernel, and using a dev node is still a kernel interface (i.e., udev is not a gatekeeper that way). – goldilocks Dec 28 '14 at 22:05
  • x @goldilocks: Hm, turns out I had a too simplistic model of how udev works -- mount on my box says that /dev is an ordinary tmpfs with no magical properties, but /proc/mounts knows it as devtmpfs. – hmakholm left over Monica Dec 28 '14 at 22:16
  • @HenningMakholm It's all magical ;) – goldilocks Dec 28 '14 at 22:24