74

I often hear people say "Unix's unique philosophy is that it treats everything as a file" or "In Unix, everything is a file". But I've never heard anyone explain why it is unique to Unix.

So, why is this unique to Unix? Does other operating systems such as Windows and Macs not operate on files?

And, is it unique compared to other operating systems?

12
  • 26
    sidenote: macosx runs ontop of a unix.
    – akira
    Dec 3, 2011 at 7:56
  • 2
    fwiw, while higher-level stuff doesn't use files (just like dbus or X in *nix) low-level Windows is built around handles. You can do a lot of the same with them as you can files on *nix (open, close, control, and usually send/receive), but you need to be a programmer to see that interface. Whether that's a good thing is a matter of preference.
    – Mark
    Dec 4, 2011 at 9:38
  • 1
    everything is a file, except network interfaces.
    – alvin
    Dec 4, 2011 at 13:23
  • 9
    @akira - OSX is certified Unix and doesn't run "on top of" it.
    – Rob
    Dec 4, 2011 at 16:16
  • 1
    @akira Not on top of a Unix. OSX is one of the few certified UNIX OS. Dec 8, 2011 at 17:07

9 Answers 9

56

So, why is this unique to Unix?

Typical operating systems, prior to Unix, treated files one way and treated each peripheral device according to the characteristics of that device. That is, if the output of a program was written to a file on disk, that was the only place the output could go; you could not send it to the printer or the tape drive. Each program had to be aware of each device used for input and output, and have command options to deal with alternate I/O devices.

Unix treats all devices as files, but with special attributes. To simplify programs, standard input and standard output are the default input and output devices of a program. So program output normally intended for the console screen could go anywhere, to a disk file or a printer or a serial port. This is called I/O redirection.

Does other operating systems such as Windows and Macs not operate on files?

Of course all modern OSes support various filesystems and can "operate on files", but the distinction is how are devices handled? Don't know about Mac, but Windows does offer some I/O redirection.

And, compared to what other operating systems is it unique?

Not really any more. Linux has the same feature. Of course, if an OS adopts I/O redirection, then it tends to use other Unix features and ends up Unix-like in the end.

7
  • 29
    OS X basically is Unix, so there's not much of a difference.
    – slhck
    Dec 3, 2011 at 7:06
  • 7
    there is a bit more to the everything a file model.
    – wnrph
    Dec 3, 2011 at 8:55
  • 3
    Plan 9 has the same concept Everything is a file concept and has even been called "more unix then unix"
    – William
    Dec 4, 2011 at 1:09
  • 3
    Windows (since NT) goes a bit futher. There are lots of devices that behave like files, such as serial ports and named pipes. You can write to all using WriteFile. But in addition, there's an even bigger category of "synchronization objects" that includes not just files but also mutexes.
    – MSalters
    Dec 5, 2011 at 10:46
  • 2
    @slhck Unlike Linux, OS X is a certified UNIX OS. Dec 8, 2011 at 17:08
34

The idea that "everything is a file" came from Multics. The designers of Unix based a lot of their work on predecessors, especially Multics. Indeed, a lot of things in computing are based on predecessors.

You can read up on the late Dennis Ritchie's work in the design of Unix for more. He referenced things that they "copied" from Multics, such as the tree-like file system, the command shell, and non-structuring of files. I'm not implying that the Unix folks stole from the Multics folks. For all intents and purposes, it was the same folks.

6
  • 6
    this shouldn't be the highest voted answer, it does not answer the question, it just begs people to ask another question: "Why is 'Everything is a File' in Multics?"
    – Lie Ryan
    Dec 3, 2011 at 11:33
  • 16
    @LieRyan: Actually it does answer the question why is it unique: it isn't. Dec 3, 2011 at 12:50
  • 1
    The Unix folks just took their ideas with them when the Multics project was discontinued and renamed it. Unix just being a play on words - one VS many. Dec 3, 2011 at 20:41
  • 3
    @Lie Ryan - The question was not why is everything a file, it's why is it unique. As Goran and I pointed out, it's not. Dec 3, 2011 at 21:11
  • 1
    The question really refers to the "Unix operating systems", i.e. the family. That family reasonably includes Multics and Linux.
    – MSalters
    Dec 5, 2011 at 10:48
29

Unique? No. Defining? Absolutely.

Having everything as a file or a device in a known hierarchy means you can use the same set of tools for everything. Plan 9 from Bell Labs takes this further with even hardware devices as files.

More importantly, this allows for two very simple and powerful concepts. Basic utilities that do One Thing Well (tm), which can be strung together with pipes as needed. Want to find something in a text file? Use cat to show it, pass it through grep, and you're cooking with gas. That's the real power of the 'Unix' way - specialised applications working together for massive amounts of flexibility.

Mac OS X also follows the Unix philosophy, but it's better hidden (an 'application' bundle is really a directory full of files), and in fact is a proper, certified Unix, descended from NeXT, which used bits of FreeBSD.

With Windows, there are some binary components such as event viewer and registry since, and there's some speed advantages there, in that particular scenario.

5
  • 1
    Plus 1 for mentioning plan 9. IMHO Fuse brings plan 9 closer to an implementation through ad-hoc and iteration. Dec 3, 2011 at 10:29
  • 2
    Well, plan 9 is plan 9, it sorta works already. Fuse just allows userspace filesystem expansion in.. interesting ways. I like the one where IRC is treated as a FS.
    – Journeyman Geek
    Dec 3, 2011 at 10:57
  • Hmm - you know, I've been meaning to look it up for ages since finding out through an Eric Raymond book years ago... You've actually finally got me over the "get round to it" hump, I've downloaded the ISO and trying it out. Dec 3, 2011 at 13:23
  • 2
    I believe there's a newer version too, called inferno
    – Journeyman Geek
    Dec 3, 2011 at 14:06
  • 1
    +1 for noting that OS X's Darwin is a 'real Unix' too.
    – calum_b
    Dec 4, 2011 at 9:07
19

Because of the special files. When people say "everything is a file in Unix", common files and directories are not what they have in mind. Special files are unique to Unix-like OSes, of which there are many. So it is not unique to the Unix.

Special files serve many purposes. There are e.g. pipes, sockets and, most notably, device files. Pipes and sockets are communication streams between processes. Much of the functionality of the subsystems is made available to the user space through device files.

Pipes and Sockets

Programs use them just as they'd use ordinary files. In fact, most of the time they don't even care what type of file they use. That's why Unix commands can be so manifoldly combined to form powerful new systems. (See I/O redirection in sawdust's answer)

Device Files

As previously mentioned these act like interfaces for the user space. For example, in order to eject the cd tray, a programmer would at first open the corresponding device file. Another example: you want your program switch the virtual terminal. Open /dev/console first.

What happens next is not sending mere characters to those files, but issuing ioctl()'s on them. The individual ioctl's you can issue depend on the device. E.g. the console is documented in console_ioctl(4)

12

I am probably going to get reamed for saying this, but I think that saying that everything is a file in Unix is in fact a fallacy. What it is really is two things.

  1. Files and devices (and a lot of other stuff) are objects that can be modeled by an interface that comprises of open, close, read, write, and control (ioctl) functions.
  2. The namespace for these objects is hierarchical i.e. these objects are organized in a hierarchy.

A filesystem implements this namespace, and implements the framework that allows the dispatch of interface functions to these objects. A filesystem was first conceptualized to house files, but was then co-opted to organize other objects in the namespace hierarchy. An example of polymorphism from before object oriented was a thing.

There is no harm in just calling everything files. But in reality, they are these more generic objects (a file being one such object). From this perspective, this idea is not unique to Unix at all. A lot of other OSes implement such hierarchies of polymorphic objects.

1
  • Exactly! In Unix everything is a file descriptor (not necessarily a file!), i.e. everything (except memory) is I/O, even files. Files must be accessed as if they are I/O streams (though they are seek-able, so somewhat randomly accessible, but that the extreme cost of extra system calls). (with the exception of what little can be done with mmap() of course....) Nov 3, 2015 at 21:15
9

When people say "In Unix, everything is a file" what they mean is that also things which are not files are treated as files.

Of course most operating systems work with files. Text files, image files, sound files. But not all operating systems treat devices as files. That's an important difference. If I list the content of my /dev/ folder in my Ubuntu operating system (which is Unix based), I get a list of more than 200 devices. Some of those devices are hardware, but are shown inside a folder. For example hard drives, USB ports, mouse and keyboard, audio devices and printers among others. Some of the devices are virtual, for example /dev/urandom, which behaves as an infinite file full of random numbers. It's not a real file on my hard drive.

All of those devices are treated as files. I can read data from and/or write data into those devices. Here there are examples of copying data from different devices into the audio device. This is possible because they are treated as files. The (geeky) result is the ability to listen to the content of the hard drive, the mouse motion, the computer memory or the pixels of an image. This would be much harder to achieve if devices were not treated as files, because each device would require different methods for reading and writing data.

That being said, what "everything" means varies from system to system. For example, OS X is based on Unix, but does not have a /dev/audio device. It uses a proprietary audio system called CoreAudio. So in this case one might say "almost everything is a file". Then, in systems like Windows, where "everything is not a file", you could still do things like copy the content of a file to printer port (typing something like copy mydocument.txt >lpt1:), which is similar to copying a document to the printer device in Unix systems.

Do other operating systems such as Windows and OS X not operate on files? Yes they do. Windows and OS X operate on files, but Windows does not treat devices as files, which is part of what "everything is a file" means.

8

I see Multics cited as the source of "everything is a file", but I think you have to look a bit deeper than just device files, named pipes, regular files, etc etc. You have to look at file naming syntax. That makes a big difference even when AUX, CON and LP "magic file names" exist.

See "The Hideous Name" by Rob Pike. It compares Unix file-naming syntax with a couple of other syntaxes, most notably VMS. Note that the mainframe OSes of the late 60s/early 70s, when Unix originated, had what the modern mind would consider extraordinarily rococo file naming syntax. It's been years since I've used VM/CMS, or NOS or NOS/VE, and I've only looked over the shoulder of someone using a Univac OS, but suffice it to say the quirks outnumbered the regularities.

I've never even seen Multics run, but you can see a paper on its file system. It appears that the original Unix took the "everything is a file" a bit further than Multics did, but your mileage may vary.

2
  • 1
    Well, in Multics everything is memory, except I/O (though that can be mapped onto memory too). In Unix everything (except memory) is a file descriptor, including files. I.e. in Unix everything is I/O. Nov 3, 2015 at 21:20
  • 1
    BTW, an emulator now exists that will boot and run Multics. Nov 3, 2015 at 21:20
4

Please note that even Linus Torvalds disagrees with that "everything is a file." If you search for "

everything is a bytestream

" you will find interesting philosophies how we mean that everything is not a file for instance an abstract data structure in the memory or a pointer is surely not a file, OK surely anything can be represented as a file but representing and being are 2 different actions.

4
  • 1
    You didn't provide a link. On some searching, I found yarchive.net/comp/linux/everything_is_file.html, quoting Torvald's responses in an email thread, in which he implicitly affirms that the model should be "everything is a file."
    – bgvaughan
    Dec 7, 2011 at 18:17
  • 3
    Actually @bgvaughan it's the opposite: yarchive.net/comp/linux/everything_is_file.html gives the quote The UNIX philosophy is often quoted as "everything is a file", but that really means "everything is a stream of bytes". Mar 23, 2015 at 19:12
  • 1
    @LIttleAncientForestKami: and Unix traditionally treats files as simply streams of bytes, as opposed to systems that treated files as streams of records (or had both streams of bytes and streams of records), as collections of streams (or resource forks), or had more complex models, where there were several different ways to access several different kinds of objects.
    – ninjalj
    Sep 25, 2017 at 9:59
  • @LAFKsaysReinstateMonica Yes!! And even something like `Everything could be referenced in a standard filesystem hierarchy``... Have a look at my answer Jan 9 at 9:30
0

My two cents

A lot of people don't understand the full meaning of this concept.

Everything is a file (@howtogeek.com)

  • The root of everything is /.
  • From there you find directories called
    • bin for binaries,
    • dev for devices,
    • etc for configuration file,
    • lib for libraries,
    • tmp for temporaries (the content of this will be destroyed each reboot),
    • proc under linux, hold access point to kernel pointers/variables,
    • sys under recent (I don't remember wich version) linux, for entry point to system drivers,
    • var for variables files (DB, mails, apt, rpm)...

... You may look, when using ls -l:

ls -ld /etc /etc/issue
drwxr-xr-x 240 root root 20480 Oct  7 23:12 /etc
-rw-r--r--   1 root root    27 Apr 10  2021 /etc/issue

The 1st character mean:

  • d for directories
  • - for standard file

But if you

ls -dl /bin/sh /dev/tty1 /dev/ttyS0 /dev/?da /dev/sr0
lrwxrwxrwx  1 root root         4 Dec 10  2020 /bin/sh -> dash
brw-rw----  1 root disk     8,  0 Aug 30 18:06 /dev/sda
brw-rw----+ 1 root cdrom   11,  0 Aug 30 18:10 /dev/sr0
crw--w----  1 root tty      4,  1 Aug 30 18:08 /dev/tty1
crw-rw----+ 1 root dialout  4, 64 Aug 30 18:06 /dev/ttyS0

The 1st character mean:

  • l for symbolic link (pointing to /bin/dash)
  • b for block devices, like hard disk or cd rom
  • c for character devices, like serial (COM1 under window), many USB devices, etc...

Then everything is a file mean, you could address a disk or a partition, a printer, a camera, or audio device, or... in same way you address a file!

For sample:

Simple command like:

cat /dev/sr0 >/path/to/diskImage.iso

is enough to create a disk image from a CDROM.

cat /path/to/diskImage.iso >/dev/sdX

will create an USB stick from ISO image... But will overwrite potential content of sdX with content of image file... You'd better ensure to point correct device!!!

More?

mkfifo /tmp/fifotest 
ls -l /tmp/fifotest 
prw-r--r-- 1 user  user  0 Oct  7 22:09 /tmp/fifotest

I won't explain here how to use a fifo nor a socket... If you host mysqld:

ls -l /run/mysqld/mysqld.sock 
srwxrwxrwx 1 mysql mysql 0 Aug 27 18:33 /run/mysqld/mysqld.sock
  • p for fifo...
  • s for socket...

Pseudo entries, feature

Under recent , you could access TCP connection via pseudo /dev/tcp/ip_address/tcp_port devices. For sample this open a connection on port 80 at `www.google.com:

exec {wget}<>/dev/tcp/www.google.com/80

Then a minimal request could look like:

printf '%s\r\n' 'GET / HTTP/1.0' Host: www.google.com '' >&$wget &&
while read -ru $wget line;do
    [[ $line =~ Set-Cookie* ]] && echo $line ;done; exec {wget}>&-

( This will print an useless cookie )

Then

Everything you want to do, you just have to take some(s) input, apply some(s) filter and transmit to some(s) output.

That's all.

Other OS

Mac

As Mac OS is based on Un*x, from Mac Os X, they use near same approach.

Windows

I remember, more than 30 years ago: I was impressed how many money could represent command like cat when dealed under Windows to permit some users to make CD images or save his hard drive!!!

Everything you want to do under Window depend on different libraries, with different meaning, syntax etc.

Note

In this answer, I don't tell about hard links, mount point, socket, like a lot of other basis concept making POSIX the must!

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .