Why is "Everything is a file" unique to the Unix operating systems?

70

19

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?

Some Noob Student

Posted 2011-12-03T05:12:33.750

Reputation: 1 717

26sidenote: macosx runs ontop of a unix. – akira – 2011-12-03T07:56:22.900

Compared to ... VMS, perhaps? – user1686 – 2011-12-03T13:12:18.720

2fwiw, 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 – 2011-12-04T09:38:12.810

1everything is a file, except network interfaces. – alvin – 2011-12-04T13:23:57.233

8@akira - OSX is certified Unix and doesn't run "on top of" it. – Rob – 2011-12-04T16:16:57.240

@akira What is mac? Private joke – Olivier Pons – 2011-12-05T07:47:26.820

@alvin well, sockets themselves can indeed be files. The whole interface is a little more complicated so is often several files behind the scenes – ewanm89 – 2011-12-05T14:49:41.993

@ewanm89, i was thinking of eth0 which does not appear as a file in linux. i am not sure about this behaviour in unix though. if not, my bad. – alvin – 2011-12-05T17:09:39.097

1@akira Not on top of a Unix. OSX is one of the few certified UNIX OS. – Let_Me_Be – 2011-12-08T17:07:18.847

@Let_Me_Be it depends precisely what one counts as Unix, if it's just kernel and coreutils in OSX that darwin or FreeBSD and gnu coreutils. That is the only part that is POSIX compliant and the rest. Technically anything else is not UNIX but running on top, we could throw in sysv init at a stretch. But this is pretty pedantic kind of reasoning. – ewanm89 – 2011-12-08T20:44:55.310

@ewanm89 Technically only operating systems that have UNIX certification are UNIX. – Let_Me_Be – 2011-12-08T21:03:15.320

No, certified POSIX compliant parts only, all the fluff on top (X Windows/Quartz, DE, WM... is not part of UNIX). – ewanm89 – 2011-12-10T11:50:52.353

Answers

54

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.

sawdust

Posted 2011-12-03T05:12:33.750

Reputation: 14 697

29OS X basically is Unix, so there's not much of a difference. – slhck – 2011-12-03T07:06:15.027

6

there is a bit more to the everything a file model.

– artistoex – 2011-12-03T08:55:41.633

1The opening paragraph simply isn't true. There were operating systems before Unix, and they had features later found in Unix, including I/O redirection. – Patrick Seymour – 2011-12-03T21:14:00.183

3Plan 9 has the same concept Everything is a file concept and has even been called "more unix then unix" – William – 2011-12-04T01:09:06.447

2Windows (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 – 2011-12-05T10:46:50.567

1@slhck Unlike Linux, OS X is a certified UNIX OS. – Let_Me_Be – 2011-12-08T17:08:11.250

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.

Patrick Seymour

Posted 2011-12-03T05:12:33.750

Reputation: 7 662

1In Multics everything is just memory (RAM), except I/O (though I/O can also be (usually) transparently mapped onto memory too). In Unix everything is a file descriptor (not necessarily a file!), i.e. everything (except memory) is I/O. In Multics every file is directly addressable with full random access no different from memory, but in Unix every file (and every device and every network connection) is an I/O stream. – Greg A. Woods – 2015-11-03T21:12:09.167

6this 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 – 2011-12-03T11:33:44.950

13@LieRyan: Actually it does answer the question why is it unique: it isn't. – Goran Jovic – 2011-12-03T12:50:18.070

1The 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. – Fiasco Labs – 2011-12-03T20:41:40.033

2@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. – Patrick Seymour – 2011-12-03T21:11:56.117

1The question really refers to the "Unix operating systems", i.e. the family. That family reasonably includes Multics and Linux. – MSalters – 2011-12-05T10:48:29.067

28

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.

Journeyman Geek

Posted 2011-12-03T05:12:33.750

Reputation: 119 122

1Plus 1 for mentioning plan 9. IMHO Fuse brings plan 9 closer to an implementation through ad-hoc and iteration. – Danny Staple – 2011-12-03T10:29:44.430

2Well, 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 – 2011-12-03T10:57:32.107

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. – Danny Staple – 2011-12-03T13:23:22.380

2I believe there's a newer version too, called inferno – Journeyman Geek – 2011-12-03T14:06:04.213

1+1 for noting that OS X's Darwin is a 'real Unix' too. – calum_b – 2011-12-04T09:07:04.963

18

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)

artistoex

Posted 2011-12-03T05:12:33.750

Reputation: 3 353

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.

Ziffusion

Posted 2011-12-03T05:12:33.750

Reputation: 246

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....) – Greg A. Woods – 2015-11-03T21:15:56.090

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.

Abe

Posted 2011-12-03T05:12:33.750

Reputation: 121

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.

Bruce Ediger

Posted 2011-12-03T05:12:33.750

Reputation: 533

1Well, 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. – Greg A. Woods – 2015-11-03T21:20:24.013

1BTW, an emulator now exists that will boot and run Multics. – Greg A. Woods – 2015-11-03T21:20:52.010

3

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.

Niklas

Posted 2011-12-03T05:12:33.750

Reputation: 753

1

Actually @bgvaughan it's the opposite: http://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".

– LAFK says Reinstate Monica – 2015-03-23T19:12:48.860

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 – 2017-09-25T09:59:43.117

1

You didn't provide a link. On some searching, I found http://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 – 2011-12-07T18:17:47.643