Everything is a file


Image by: Opensource.com

When everything is a file difficult things are easy and impossible things are merely hard.

Here’s a trick question for you: Which of the following are files?

  • Directories
  • Shell scripts
  • LibreOffice documents
  • Serial ports
  • Kernel data structures
  • Data streams
  • Kernel tuning parameters
  • Hard drives
  • Partitions
  • Logical Volumes (LVM)
  • Printers
  • Sockets

Perhaps you won’t believe this, but to Unix and Linux they are all files. That’s one of the most amazing concepts—it makes possible some very simple, yet powerful methods for performing many administrative tasks that might otherwise be extremely difficult or impossible.

Back up the Master Boot Record

Consider, for example, the simple task of making a backup of the Master Boot Record (MBR) of your hard drive. I have, on occasion, needed to restore or recreate my MBR, particularly the partition table. Recreating it from scratch is very difficult. Restoring it from a saved file is easy. Linux comes with a powerful GNU utility, dd, that can perform this and many other functions.

The dd stands for “disk dump,” but those of us administrators who have been around for a long time know it as “disk destroyer” because, if you are not very careful, it will do exactly what you tell it to, including destroy all of the data on a hard drive or partition.

The following command will back up your MBR. It must be run as root because non-root users do not have access to the hard drive device files in the /dev directory. BS is Block Size and count is the number of blocks to read from the source file. This command will create a file, myMBR.bak in the /tmp directory. The file will be 512 bytes in size and contain the contents of your MBR including the bootstrap code and partition table.

# dd if=/dev/sda of=/tmp/myMBR.bak bs=512 count=1

If the MBR were damaged, it would be necessary to boot to a rescue disk and use the following command, which performs essentially the reverse operation of the one above. Notice that it is not necessary to specify the block size and block count as in the first command because the dd command will simply copy the backup file to the first sector of the hard drive and stop when it reaches the end of the source file.

# dd if=/tmp/myMBR.bak of=/dev/sda

It’s all part of the filesystem

Everything on a Linux computer is accessible as a file in the filesystem space. The whole point of this is to be able to use common tools on different things.

The dd command can be used to copy an entire partition of hard drive to a file or another hard drive as shown below. Here again the dd command copies data to the end of the input device and stops. Be sure that the size of the output device is larger than the input device.

# dd if=/dev/sdf2 of=/dev/sdg3

# dd if=/dev/sda of=/dev/sdg

Other filesystem tools work, too. The cat command, for example, can be used to send the content of any file to Standard Output. This includes partitions and entire hard drives. Then the output can be redirected to a file.

# cat /dev/sda1 > partition1.backup

The cat command, however, does not have the control that the dd command does. For example, one cannot specify the amount of data to be read from the source device or file.

Here is an interesting experiment that will demonstrate the fact that everything is a file. Most Linux distributions have multiple virtual consoles, 1 through 7, that can be used to login to a local console session with a shell interface. These can be accessed using the key combinations Ctrl-Alt-F1 for console 1, Ctrl-Alt-F2 for console 2, and so on.

Press Ctrl-Alt-F2 to switch to console 2. On some distributions, the login information includes the tty (Teletype) device associated with this console, but many do not. It should be tty2 because you are in console 2.

Login as a non-root user. Then you can use the who am i command—yes, just like that, with spaces—to determine which tty device is connected to this console.

Before we actually perform this experiment, look at a listing of the tty2 and tty3 devices in /dev.

# ls -l /dev tty[23]

There will be a large number of tty devices defined but we do not care about most of them, just the tty2 and tty3 devices. As device files, there is nothing special about them; they are simply character type devices. We will use these devices for this experiment. The tty2 device is attached to virtual console 2 and the tty3 device is attached to virtual console 3.

Press Ctrl-Alt-F3 to switch to console 3. Login again as the same non-root user.

Now enter the following command on console 3.

# echo "Hello world" > /dev/tty2

Press Ctrl-Alt-F2 to return to console 2. The string “Hello world” (without quotes) is displayed in console 2.

This experiment can also be performed with terminal emulators on the GUI desktop. Terminal sessions on the desktop use pseudo terminal devices in the /dev tree, such as /dev/pts/1. Open two terminal sessions using Konsole or Xterm. Determine which pseudo-terminals they are connected to and use one to send a message to the other.

Now continue the experiment by using the cat command to display the /etc/fstab file on a different terminal.

Another interesting experiment is to print a file directly to the printer using the cat command. Assuming that your printer device is /dev/usb/lp0, and that your printer can print PDF files directly, the following command will print a PDF file on your printer.

# cat test.pdf > /dev/usb/lp0

The dd command can also be used to print a printer ready file. I think the cat command is actually better suited to this task, though.

Implications of “everything is a file”

The implications of “everything is a file” are far-reaching and much greater than can be listed in a short article such as this one. You have already seen some examples in the preceding experiments. But here is a short list that encompasses those and more.

  1. Clone hard drives.
  2. Back up partitions.
  3. Back up the master boot record (MBR).
  4. Install ISO images onto USB thumb drives.
  5. Communicate with users on other terminals.
  6. Print files to a printer.
  7. Change the contents of certain files in the /proc pseudo filesystem to modify configuration parameters of the running kernel.
  8. Overwrite files, partitions, or entire hard drives with random data or zeros.
  9. Redirect unwanted output from commands to the /dev/null device where it disappears forever.
  10. etc., etc., etc.

There are so many possibilities here that any list can really only scratch the surface. I am sure that you have—or will—figure out many ways to use this feature of Linux far more creatively than I have mentioned here. I would be interested to see your comments on how you use “everything is a file.”

Additional information

For more information on the /dev/ directory and the devices that you may find there, refer to this article at The Linux Journal. For more specific information on individual devices, this article and this article at the Linux Documentation Project are helpful.