Many 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.
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:
mail bigboss@corp < to_do%
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
> (greater-than symbol) operator. The pipe operator
sends the standard output of one
command to the standard input of another command. Input/output
redirection is one of the nicest features of UNIX because of its
tremendous power and flexibility.
Instead 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.
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:
cat /etc/passwd > password%
cat passwordroot::0:0:Root:/:/bin/sh daemon:NONE:1:1:Admin:/: . . . john::128:50:John Doe:/usr/john:/bin/sh %
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:
who > users%
date > today%
lspassword today users ...
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:
cat userstim tty1 Aug 12 07:30 john tty4 Aug 12 08:26 %
cat todayTue Aug 12 08:36:09 EDT 1997 %
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:
cat > filename
takes input from the keyboard and redirects it to a file. Try the following example:
cat > to_do Finish report by noon Lunch with Xannie Swim at 5:30 ^D%
You can also create a bigger file out of many smaller files using the cat command and the > operator. The form:
cat file1 file2 > newfile
cat today to_do > diary%
cat diaryTue Aug 12 08:36:09 EDT 1997 Finish report by noon Lunch with Xannie Swim at 5:30 %
CAUTION: If you are using the > (output redirection) operator, you should be careful not to overwrite the contents of a file accidentally. Your system may let you redirect output to an existing file. If so, the old file will be deleted (or, in UNIX lingo, "clobbered"). Be careful not to overwrite a much-needed file! Many shells can protect you from this risk. In the C shell, use the command set noclobber. The Korn shell and bash command is set -o noclobber. Enter the command at a shell prompt or put it in your shell's startup file After that, the shell will not allow you to redirect onto an existing file and overwrite its contents.
This doesn't protect against overwriting by UNIX commands like cp; it works only with the > redirection operator. For more protection, you can set UNIX file access permissions.
cat file2 >> file1
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:
cat users >> diary%
date >> diary%
cat diaryTue Aug 12 08:36:09 EDT 1997 Finish report by noon Lunch with Xannie Swim at 5:30 tim tty1 Aug 12 07:30 john tty4 Aug 12 08:26 Tue Aug 12 09:07:24 EDT 1997 %