Monday, July 2, 2012

What “Everything Is a File” Means on Linux


image

One of the defining features of Linux and other UNIX-like operating systems is that “everything is a file.” This is an oversimplification, but understanding what it means will help you understand how Linux works.
Many things on Linux appear in your file system, but they aren’t actually files. They’re special files that represent hardware devices, system information, and other things — including a random number generator.
These special files may be located in pseudo or virtual file systems such as /dev, which contains special files that represent devices, and /proc, which contains special files that represent system and process information.

/proc

For example, let’s say you want to find information about your CPU. The /proc directory contains a special file – /proc/cpuinfo – that contains this information.
You don’t need a special command that tells you your CPU info – you can just read the contents of this file using any standard command that works with plain-text files. For example, you could use the command cat /proc/cpuinfo to print this file’s contents to the terminal – printing your CPU information to the terminal. You could even open /proc/cpuinfo in a text editor to view its contents.
Remember, /proc/cpuinfo isn’t actually a text file containing this information – the Linux kernel and the proc file system are exposing this information to us as a file. This allows us to use familiar tools to view and work with the information.
The /proc directory also contains other similar files, for example:
  • /proc/uptime – Exposes the uptime of your Linux kernel – in other words, how long your system has been on without shutting down.
  • /proc/version – Exposes the version of your Linux kernel.

/dev

In the /dev directory, you’ll find files that represent devices – as well as files that represent other special things. For example, /dev/cdrom is your CD-ROM drive. /dev/sda represents your first hard drive, while /dev/sda1 represents the first partition on your first hard drive.
Want to mount your CD-ROM? Run the mount command and specify /dev/cdrom as the device you want to mount. Want to partition your first hard drive? Run a disk-partitioning utility and specify /dev/sda as the hard disk you want to edit. Want to format the first partition on your first hard drive? Run a formatting command and tell it to format /dev/sda1.
As you can see, exposing these devices as part of the file system has its advantages. The file system provides a consistent “name space” that all applications can use to address and access the devices.

/dev/null, /dev/random, and /dev/zero

The /dev file system doesn’t just contain files that represent physical devices. Here are three of the most notable special devices it contains:
  • /dev/null – Discards all data written to it – think of it as a trash can or black hole. If you ever see a comment telling you to send complains to /dev/null – that’s a geeky way of saying “throw them in the trash.”
  • /dev/random – Produces randomness using environmental noise. It’s a random number generator you can tap into.
  • /dev/zero – Produces zeros – a constant stream of zeros.
If you think of these three as files, you won’t see a use for them. Instead, think of them as tools.
For example, by default, Linux commands produce error messages and other output that they print to the standard output, normally the terminal. If you want to run a command and don’t care about its output, you can redirect that output to /dev/null. Redirecting a command’s output to /dev/null immediately discards it. Instead of having every command implement its own “quiet mode,” you can use this method with any command.
command > /dev/null
If you wanted a source of randomness – say, for generating an encryption key, you wouldn’t need to write your own random number generator – you could use /dev/random.
To erase a hard drive’s contents by writing 0’s to it, you don’t need a special utility dedicated to zero’ing a drive – you could use standard utilities and /dev/zero. For example, the dd command reads from a location and writes to another location. The following command would read zeros from /dev/zero and write them directly to the first hard disk partition on your system, completely erasing its contents.
(Warning: This command will erase all data on your first partition if you run it. Only run this command if you want to destroy data.)
dd if=/dev/zero of=/dev/sda1
Here we’re using dd with special files (/dev/zero and /dev/sda1), but we could also use dd to read from and write to actual files. The same command works both for manipulating devices directly and working with files.

Clarification

In practice, it’s more accurate to say that “everything is a stream of bytes” than “everything is a file.” /dev/random isn’t a file, but it certainly is a stream of bytes. And, although these things technically aren’t files, they are accessible in the file system – the file system is a universal “name space” where everything is accessible. Want to access a random number generator or read directly from a device? You’ll find both in the file system; no other form of addressing needed.
Of course, some things aren’t actually files – processes running on your system aren’t a part of the file system. “Everything is a file” is inaccurate, but lots of things do behave as files.

No comments:

Post a Comment