CONCEPT: Every program you run from the shell opens three files: Standard input, standard output, and standard error. The files provide the primary means of communications between the programs, and exist for as long as the process runs.
The standard input file provides a way to send data to a process. As a default, the standard input is read from the terminal keyboard.
The standard output provides a means for the program to output data. As a default, the standard output goes to the terminal display screen.
The standard error is where the program reports any errors encountered during execution. By default, the standard error goes to the terminal display.
CONCEPT: A program can be told where to look for input and where to send output, using input/output redirection. Unix uses the "less than" and "greater than" special characters [< and >] to signify input and output redirection, respectively.
Redirecting input
Using the "less-than" sign with a file name like this:< file1in a shell command instructs the shell to read input from a file called "file1" instead of from the keyboard.EXAMPLE:Use standard input redirection to send the contents of the file /etc/passwd to the more command:
more < /etc/passwdMany Unix commands that will accept a file name as a command line argument, will also accept input from standard input if no file is given on the command line.
EXAMPLE: To see the first ten lines of the /etc/passwd file, the command:
head /etc/passwdwill work just the same as the command:head < /etc/passwdRedirecting output
Using the "greater-than" sign with a file name like this:> file2causes the shell to place the output from the command in a file called "file2" instead of on the screen. If the file "file2" already exists, the old version will be overwritten.EXAMPLE: Type the command
ls /tmp > ~/ls.outto redirect the output of the ls command into a file called "ls.out" in your home directory. Remember that the tilde [~] is Unix shorthand for your home directory. In this command, the ls command will list the contents of the /tmp directory.Use two "greater-than" signs to append to an existing file. For example:
>> file2causes the shell to append the output from a command to the end of a file called "file2". If the file "file2" does not already exist, it will be created.EXAMPLE: In this example, I list the contents of the /tmp directory, and put it in a file called myls. Then, I list the contents of the /etc directory, and append it to the file myls:
ls /tmp > mylsls /etc >> myls
Redirecting error
Redirecting standard error is a bit trickier, depending on the kind of shell you're using [there's more than one flavor of shell program!]. In the POSIX shell and ksh, redirect the standard error with the symbol "2>".EXAMPLE: Sort the /etc/passwd file, place the results in a file called foo, and trap any errors in a file called err with the command:
Most Linux commands read input, such as a file or another attribute for the command, and write output. By default, input is being given with the keyboard, and output is displayed on your screen. Your keyboard is your standard input [stdin] device, and the screen or a particular terminal window is the standard output [stdout] device.
However, since Linux is a flexible system, these default settings don't necessarily have to be applied. The standard output, for example, on a heavily monitored server in a large environment may be a printer.
5.1.2. The redirection operators
5.1.2.1. Output redirection with > and |
Sometimes you will want to put output of a command in a file, or you may want to issue another command on the output of one command. This is known as redirecting output. Redirection is done using either the ">" [greater-than symbol], or using the "|" [pipe] operator which sends the standard output of one command to another command as standard input.
As we saw before, the cat command concatenates files and puts them all together to the standard output. By redirecting this output to a file, this file name will be created - or overwritten if it already exists, so take care.
nancy:~> cat test1 some words nancy:~> cat test2 some other words nancy:~> cat test1 test2 > test3 nancy:~> cat test3 some words some other words
Redirecting "nothing" to an existing file is equal to emptying the file:
nancy:~> ls -l list -rw-rw-r-- 1 nancy nancy 117 Apr 2 18:09 list nancy:~> > list nancy:~> ls -l list -rw-rw-r-- 1 nancy nancy 0 Apr 4 12:01 list
This process is called truncating.
The same redirection to an nonexistent file will create a new empty file with the given name:
nancy:~> ls -l newlist ls: newlist: No such file or directory nancy:~> > newlist nancy:~> ls -l newlist -rw-rw-r-- 1 nancy nancy 0 Apr 4 12:05 newlist
Chapter 7 gives some more examples on the use of this sort of redirection.
Some examples using piping of commands:
To find a word within some text, display all lines matching "pattern1", and exclude lines also matching "pattern2" from being displayed:
grep pattern1 file | grep -v pattern2
To display output of a directory listing one page at a time:
ls -la | less
To find a file in a directory:
ls -l | grep part_of_file_name
5.1.2.2. Input redirection
In another case, you may want a file to be the input for a command that normally wouldn't accept a file as an option. This redirecting of input is done using the "" followed by the name of the file. Now you will be prompted to insert data into this newly created file. Type a line and then press "Ctrl+D" to save the file. $ cat > secondFile.