• ARTICLES
SEARCH

How-To Geek

HTG Explains: 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.

image

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.

image

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.

image

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

image

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.

Chris Hoffman is a technology writer and all-around computer geek. He's as at home using the Linux terminal as he is digging into the Windows registry. Connect with him on Google+.

  • Published 07/2/12

Comments (16)

  1. BigT

    Wow! This is one of the most informative Linux articles I’ve read in a while.

  2. Stephen Hauskins

    I think the idea behind “everything is file” is that Linux (UNIX) commands can access mostly everything as though it is a file. As your examples show you can access CPU information as though it were a file via the command line with no special tricks.

    This is a subtle interpretation of “everything is a file”, it depends if you are just looking at the actual disk entries or looking at it from the perspective of commands accessing these disk entries.

  3. dnlsrl

    Love that feature. It might seem pretty weird in principle, but in the end it is very, very useful.

  4. M Henri Day

    Kudos to Chris Hoffman – a great Linux tutor ! And thanks to How-To Geek for pubhisning his blogs !…

    Henri

  5. riyas

    thanks for such a nice info……..

  6. Ashwin Rao

    Nice and informative post! Thanks to HTG for such posts. However, it would be better if author add some spaces in the commands which are really harmful such as ‘dd’ command in the post.

  7. Ibod Catooga

    Oh my lawd this article was so good I blewe a load in my jeans.

    Good gravy this shizzneezie be smokin’ out da crunk crack of your mama’s nightgown.

    Sheet, negero!

  8. a. wright

    Yahooooooooooo

  9. sam weller

    Very good article, thank you.
    I shall begin saying, in this context, “everything LIKE a file” instead of everything IS a file.

  10. Alan

    UNIX is no different from Windows, which in it’s early days was based on UNIX, or UNIX principles. That is why so many commands used to be in common. Essentially, everything in Windows is a file too. It’s just that we don’t see the files because they are buried in Windows, some so deep that they are in assembly language. Every programmer knows that programs are built from libraries (files) that tell the computer what to do when called and any .sys, .exe, .bat or .com file can call a file – which means that it does what the file tells it to do. There are some seeming exceptions to that but normally, if you just look at the files, on the inside, you will find one of those kinds of files. Most of the files translate from Windows to UNIX. A /proc file is essentially a .sys file. In Windows there may not be a .man file (the help manual) but there is an equivalent file buried in Windows somewhere and most of the time you can find it by looking for the readme.txt file.

  11. TheFu

    When I wrote “everything is a file on Linux” here last week (or something like that), I hoped you’d write this article. Thanks.

    However, if you are a C programmer working on UNIX, then know that all those device files, fifo, lifo, named pipes, and an accessing the keyboard or a DVD player that aren’t normal files can be accessed …. using an open() C language function call, to move around you seek() and when you are done, you close() the file. This is exactly how you do this with a regular file on disk. Same-Same.

    Ok, you’d probably want to use buffered calls, fopen(), fseek(), and fclose() instead for normal file access, but everything really is just a file on UNIX-like operating systems.

    To learn more, ‘man -s2 open’, that will open section 2 of the man system – section 2 is where C function calls are stored. Lurkers: Different sections have different information.

    How about a ‘HowTo Read man Pages’ article next? They are not the easiest thing to understand without a little practice, but once you get the hang of it and know which sections contain what data, most people find it explains almost everything.

  12. Radhakrishna

    Dear Colleagues,

    On perusing my home directory, or folder, one always seems to see the names of my files,( eg thingummy.txt ) duplicated with the extension thingummy_files, containing less information, . In effect, the directory contains thingummy.txt and thingummy(underscore)files.
    When do these new files get made, what is their significance, and do I lose anything if I delete them ?

    Thank you for your help.
    Sincerely,
    Radhakrishna

  13. Radhakrishna

    Dear Colleagues,

    On perusing my home directory, or folder, one always seems to see the names of my files,( eg thingummy.txt ) duplicated with the extension thingummy_files, containing less information, . In effect, the directory contains thingummy.txt and thingummy(underscore)files.
    When do these new files get made, what is their significance, and do I lose anything if I delete them ?

    Thank you for your help.
    Sincerely,
    Radhakrishna

  14. Arnau

    Very pretty article talking about linux.

    Great job!

  15. Heistman

    Thanks for such an informative article, kudos and keep up the good works ;-)

  16. Angel

    Chris Hoffman, you’re the bomb, man! Thanks for the great read. We need more fundamental articles like this. How stuff works kind of articles for clarity and a good foundation.

Get Free Articles in Your Inbox!

Join 134,000 newsletter readers

Email:

Go check your email!