A Quick Introduction to Unix/Files and Processes

From Wikibooks, open books for an open world
Jump to: navigation, search

Everything in Unix is a file or a process. In Unix a file is just a destination for or a source of a stream of data. Thus a printer, for example, is a file and so is the screen.

A process is a program that is currently running. So a process may be associated with a file. The file stores the instructions that are executed for that process to run.

Another way to look at it is that file is a collection of data that can be referred to by name. Files are created by users either directly (using text editors, running compilers etc.) or indirectly (by running some program - like processing a text input file to produce a formatted file for printing).

Examples of files include:

  • a text document;
  • a program written in a programming language such as C++ or Java;
  • a jpeg;
  • a directory: directories can be thought of as the analogue of Windows’ folders. Directories are files that contain links to other files.

The standard input and output and the standard error stream[edit]

There are two files that have somewhat opaque names, stdin and stdout. These names refer to default sources of and destinations for data. Consider the process initiated by the command ls. The default output of this process is a list of files in the current working directory and we have seen that the output is displayed on screen. This illustrates the default output stdout which is nothing but the screen. The standard input by contrast is the keyboard - thus also known as stdin.

In shell programming it is often useful to prevent error messages from Unix commands from being displayed on screen but to either suppress them or send them to a file. This is done by redirecting the error messages to a filename or to /dev/null - the null device or destination. To use these streams (stdin, stdout, stderr) in the shell we don't refer to them by name but by the numerical descriptors

Stream name Descriptor
stdin 0
stdout 1
stderr 2

For the beginning user, the important thing to grasp is that commands effectively take their input from and direct their output to files, and by default the output file is the screen and the input file is the keyboard.