Skip to content

Both.org

News, Opinion, Tutorials, and Community for Linux Users and SysAdmins

Primary Menu
  • About Us
  • Computers 101
    • Hardware 101
    • Operating Systems 101
  • End of 10 Events
    • Wake Forest, NC, — 2025-09-20
  • Linux
    • Why I use Linux
    • The real reason we use Linux
  • My Linux Books
    • systemd for Linux SysAdmins
    • Using and Administering Linux – Zero to SysAdmin: 2nd Edition
    • The Linux Philosophy for SysAdmins
    • Linux for Small Business Owners
    • Errata
      • Errata for The Linux Philosophy for SysAdmins
      • Errata for Using and Administering Linux — 1st Edition
      • Errata for Using and Administering Linux — 2nd Edition
  • Open Source Resources
    • What is Open Source?
    • What is Linux?
    • What is Open Source Software?
    • The Open Source Way
  • Write for us
    • Submission and Style guide
    • Advertising statement
  • Downloads
  • Home
  • Everything is a file
  • Filesystems
  • Linux

Everything is a file

When everything is a file Difficult things are easy and impossible things are merely hard.
David Both January 4, 2024 7 minutes read
files_documents_paper_folder
1

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.

Tags: Everything is a file Filesystems

Post navigation

Previous: Creating a universal live Linux USB drive
Next: Linux Boot and Startup

Related Stories

Puzzle pieces coming together to form a computer screen
  • Fortran 77
  • History
  • Linux

A look back: f2c

Jim Hall May 13, 2026
Ubuntu-Resolute-Raccoon
  • Getting Started
  • Linux
  • Ubuntu

Getting started with Linux: Ubuntu 26.04

Don Watkins May 12, 2026
Rows of old fashioned alarm clocks.
  • Linux
  • SysAdmin

Changing the timezone in Fedora

Jim Hall May 11, 2026

Random Quote

Unix is user-friendly. It just isn’t promiscuous about which users it’s friendly with.

— Steven King

Why I’ve Never Used Windows

On February 12 I gave a presentation at the Triangle Linux Users Group (TriLUG) about why I use Linux and why I’ve never used Windows.

Here’s the link to the video: https://www.youtube.com/live/uCK_haOXPFM 

Why there’s no such thing as AI

Last October at All Things Open (ATO) I was interviewed by Jason Hibbits of We Love Open Source. It’s posted in the article “Why today’s AI isn’t intelligent (yet)“.

Technically We Write — Our Partner Site

Our partner site, Technically We Write, has published a number of articles from several contributors to Both.org. Check them out.

Technically We Write is a community of technical writers, technical editors, copyeditors, web content writers, and all other roles in technical communication.

Subscribe to Both.org

To comment on articles, you must have an account.

Send your desired user ID, first and last name, and an email address for login (this must be the same email address used to register) to subscribe@both.org with “Subscribe” as the subject line.

You’ll receive a confirmation of your subscription with your initial password as soon as we are able to process it.

Administration

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org

License and AI Statements

Both.org aims to publish everything under a Creative Commons Attribution ShareAlike license. Some items may be published under a different license. You are responsible to verify permissions before reusing content from this website.

The opinions expressed are those of the individual authors, not Both.org.

You may not use this content to train AI.

 

Advertising Statement

Both.org does not sell advertising on this website.


Advertising may keep most websites running—but at Both.org, we’re committed to keeping our corner of the web ad-free. Both.org does not sell advertising on the website. Nor do we offer sponsored articles at this time. We’ll update this page if our position on sponsorships changes.

We want to be open about how the website is funded. Both.org is supported entirely by David Both and a few other dedicated individuals.

 

 

Copyright © All rights reserved. | MoreNews by AF themes.