Everything is a file

No readers like this yet.
Arrows moving a process forward

Opensource.com

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

  • Directories
  • Shell scripts
  • LibreOffice documents
  • Serial ports
  • Kernel data structures
  • 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 had, 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.

David Both
David Both is an Open Source Software and GNU/Linux advocate, trainer, writer, and speaker. He has been working with Linux and Open Source Software since 1996 and with computers since 1969. He is a strong proponent of and evangelist for the "Linux Philosophy for System Administrators."

7 Comments

Actually, "dd" name is "convert and copy", but "cc" was already taken by "clang".
Apart from that, the second command example has two "if" arguments and none "of" argument.

Thanks for informing me about the incorrect argument. I have fixed it so it is correct now and has both if= and of= arguments.

Thanks!

In reply to by Arthur (not verified)

Some readers not sufficiently steeped in linux / unix are undoubtedly thinking, "Well that's neat - you can treat anything as if it were a file." I was stuck in that mindset for the longest time.

Then one day I delved into the magic involved in creating a "virtual disk". It turns out that there is no special ingredient. You just make a file of the specified size, partition it and format it.

Yeah there's some little adjustment (that I can never seem to remember the command for) that you can make to it to keep fdisk and mke2fs from complaining so much, but it works even without that.

To the OS, everything really -is- a file and a file really can -be- anything.

About virtual disks: on unix-like systems, if you have a file in which you think there is a filesystem, like cd/dvd/iso images, floppy images and virtual-disks, you can always mount them if you use the 'loop' option. This is only because the kernel expects a device where it finds a file.

mount /full/path/to/file.iso /path/to/mountpoint/ -o loop -t filesystem.

In reply to by Liam (not verified)

Thanks for the interesting article.
How would I list all the pseudo terminal devices?

Thanks! I am glad you found it interesting.

The more recent versions of Linux maintain the pseudo terminal devices in the /dev/pts directory. These devices are only created as they are needed when a new terminal session is created. So there is no permanent list of the pts devices.

To determine the pts device for a specific open terminal session, use the "who am i" command just as you would for one of the virtual consoles.

# who am i
root pts/3 2015-09-28 07:50 (:0:S.1)

In this case the terminal session is /dev/pts/3.

I hope this helps.

In reply to by JJ

When I learned unix, the statement was: Everything is a file, if it is not a file, it can still be handled as a file. And a file is a sequence of bytes.

From the programming point of view: a file can be opened, closed, read, written and a pointer can be set.

Using this on a device has some implications. Writing to a keyboard (/dev/kbd is a name that comes to mind) only can set the lichts for caps-lock, num-lock and scroll-lock. Setting the pointer is not possible at all (except for resetting the lights). Reading will just read the key presses or the character sequence, depending on the 'raw' or 'dedicated' device.

For the character screens used at the time, writing was always possible, setting the pointer has 2 coordinates but reading was not always possible.

A disk was just a file. Most likely /dev/sd0 is your disk (this was /dev/hd0 at the time). If the disk is partitioned, /dev/sd0a is the first partition. If you have a smaller disk and copy it to a file on a larger disk

This all went wrong once linux (and a lot of other unix-likes) start using cdrom and specially the writable versions. There are only few unix implementations that can burn a cd with the command `dd if=image.iso of=/dev/sr0`, where the reverse (`dd if=/dev/sr0 of=image.iso`) is the best way to create an iso-file from any cd.

Creative Commons LicenseThis work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License.