5. Redirecting I/O5.1 Standard Input and Standard OutputMany UNIX commands read input (such as a file) and write output. In general, if no filename is specified in a command, the shell takes whatever you type on your keyboard as input to the command (after you press the first [RETURN] to start the command running, that is). Your terminal keyboard is the command's standard input . As a command runs, the results are usually displayed on your terminal screen. The terminal screen is the command's standard output . So, by default, each command takes its input from the standard input and sends the results to the standard output. These two default cases of input/output can be varied. This is called input/output redirection . You can use a given file as input to a command that doesn't normally accept filenames by using the "<" (less-than symbol) operator. For example, the following command mails the contents of the file to_do to bigboss@corp :
%
You can also write the results of a command to a named file or some
other device instead of displaying output on the screen using the
5.1.1 Putting Text in a FileInstead of always letting the output of a command come to the screen, you can redirect output into a file. This is useful when you have a lot of output that would be hard to read on the screen or when you put files together to create a bigger file. As we've seen, the cat command can display a short file. It can also be used to put text into a file, or to create a bigger file out of smaller files. 5.1.1.1 The > operatorWhen you add "> filename " to the end of a command line, the results of the command are diverted from the standard output to the named file. The > symbol is called the output redirection operator . For example, let's use cat with this operator. The contents of the file that you'd normally see on the screen (from the standard output) are diverted into another file:
% An example in Chapter 3, Your UNIX Account , showed how cat /etc/passwd displays the file /etc/passwd on the screen. The example above adds the > operator; so the output of cat goes to a file called password in the working directory. Displaying the file password shows that its contents are the same as the file /etc/passwd (the effect is the same as the copy command cp /etc/passwd password ). You can use the > redirection operator with any command that sends text to its standard output - not just with cat . For example:
% We've sent the output of who to a file called users and the output of date to the file named today . Listing the directory shows the two new files. Let's look at the output from the who and date commands, regarding these two files:
% You can also use the cat command and the > operator to make a small text file. We told you earlier to type [CTRL-D] if you accidentally enter cat without a filename. This is because the cat command alone takes whatever you type on the keyboard as input. Thus, the command:
takes input from the keyboard and redirects it to a file. Try the following example:
% cat takes the text that you typed as input, and the > operator redirects it to a file called to_do . Type [CTRL-D] on a new line by itself to signal the end of the text. You should get a shell prompt. You can also create a bigger file out of many smaller files using the cat command and the > operator. The form:
creates a file newfile , consisting of file1 followed by file2 .
%
5.1.1.2 The >> operatorYou can add more text to the end of an existing file, instead of replacing its contents, by using the >> (append redirection) operator. Use it like the > (output redirection) operator. So,
appends the contents of file2 to the end of file1 . For an example, let's append the contents of the file users , and also the current date and time, to the file diary . Then we display the file:
% |
|