Difference between revisions of "Everything is a File"
| Line 11: | Line 11: | ||
It can be even said that Unix even treats the user as file. At the command line user interaction consists of reading and inputting a stream of text. More specifically Unix is centered around three standard input/output streams: ''standard input'' (<code>stdin</code>), ''standard output'' (<code>stdout</code>) and ''standard error'' (<code>stderr</code>). | It can be even said that Unix even treats the user as file. At the command line user interaction consists of reading and inputting a stream of text. More specifically Unix is centered around three standard input/output streams: ''standard input'' (<code>stdin</code>), ''standard output'' (<code>stdout</code>) and ''standard error'' (<code>stderr</code>). | ||
| − | Most files in Unix are ''regular files'': they contain regular data | + | Most files in Unix are ''regular files'': they contain regular data e.g. text files, images, executables or program input/output. There are exceptions however. These are things that can be said to be "more than files", that is things that have special properties although they can also be treated as regular files e.g. viewed with <code>ls</code>. They are: |
| + | |||
| + | * Directories: lists of other files. | ||
| + | * Special files: most of these are in <tt>/dev/</tt>. They are often used for input and output for example <code>/dev/input/mice</code> and there are also special files that have a particular purpose e.g. <code>/dev/random</code> exists as a source of randomness and <code>/dev/full</code> exists so programmers can testo see what happens if a disk is full. (try <code>echo "I ate too many donuts" > /dev/full). | ||
| + | * Links: files that are a link to another file (or directory). These allow them to be seen elsewhere or under a different name. Most often we talk about and use symlinks, these have the advantage that if you delete a symlink the original doesn't also get deleted. | ||
| + | |||
| + | * | ||
Revision as of 13:07, 17 February 2011
"On a UNIX system, everything is a file; if something is not a file, it is a process."
"Everything is a file" is a core design principle of Unix (and hence Linux) architecture. It can be taken to mean two things
Everything is a stream of bytes
Everything is a stream of bytes you can read and/or write to. Not just documents but also storage devices like a CD-ROM in fact almost any device. This is in keeping with Unix philosophy which favors simplicity, universality and plain text. This means that you can treat devices and other parts of the system just like you would any other text file. To demonstrate this plug in an external mouse and type
sudo cat /dev/input/mice
in the terminal and move your mouse around. It might become clearer if you pipe the output through hexdump
sudo cat /dev/input/mice | hexdump
It can be even said that Unix even treats the user as file. At the command line user interaction consists of reading and inputting a stream of text. More specifically Unix is centered around three standard input/output streams: standard input (stdin), standard output (stdout) and standard error (stderr).
Most files in Unix are regular files: they contain regular data e.g. text files, images, executables or program input/output. There are exceptions however. These are things that can be said to be "more than files", that is things that have special properties although they can also be treated as regular files e.g. viewed with ls. They are:
- Directories: lists of other files.
- Special files: most of these are in /dev/. They are often used for input and output for example
/dev/input/miceand there are also special files that have a particular purpose e.g./dev/randomexists as a source of randomness and/dev/fullexists so programmers can testo see what happens if a disk is full. (tryecho "I ate too many donuts" > /dev/full). - Links: files that are a link to another file (or directory). These allow them to be seen elsewhere or under a different name. Most often we talk about and use symlinks, these have the advantage that if you delete a symlink the original doesn't also get deleted.