In Detail: UNIX File Systems - The standard files and directions
(Page 4 of 5 )
They are three standard files: stdin, stdout and stderr. These files are for input, output and the error echo, respectively. Every application will maintain these files. Furthermore, it is a generally accepted fact that new programs should be written such that, by default, if we do not give to it an input they will read from the stdin, print on the stdout and signal errors on the stderr.
We call all the programs that follow this idea "filters." An integer number also codes to this tree inputs that are automatically opened by the operating system. The standard input is zero, the standard output is one and the standard error is two.
The shell allows you to change where files put their input and output. It can direct some files to a program as input or direct its output to a file. For input, use the > followed by the file name. For output we direct in the other direction, and use the > character followed by the file name.
These three standard files do not have names yet; so we can reach them, they exist via the /dev/stdin, /dev/stdout and /dev/stderr devices. Redirecting via these will also create a new file and add it to the output. To avoid this, and just append the new data to the end of a file, use the >>.
I will use this moment to also introduce the cat command. This will echo back the content of a file you point to it. This is a filter. If you use it with no arguments, it will use the stdin (the terminal itself) until you signal the end of file (Ctrl + D). After this, it will print back whatever you entered.
#print back the content of alfa
cat alfa
#use as input the alfa and as output the beta
cat <alfa >beta
# now we will append the content of alfa to beta
cat <alfa >>beta
#input alfa, output remains default, error to null device
cat 0<alfa 2>/dev/null
The last one is a little more interesting. This shows that we can also point something to direct to somewhere. We use the integer codification of the standard files as I described above. Also note that by directing the error output to the null device, we virtually ignore them, as the null device will eat up everything you throw at it.
Next: File names on the shell command line >>
More Miscellaneous Articles
More By Gabor Bernat