A Quick Introduction to Unix/Files and Processes
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 image;
- 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
editThere 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 which is then 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. Instead, they are either suppressed or sent 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 refer to them by the numerical descriptors rather than by name.
Stream name | Descriptor |
---|---|
stdin | 0 |
stdout | 1 |
stderr | 2 |
To review: Commands effectively take their input from files and direct their output to files. By default, the output file is the screen, and the input file is the keyboard.