Command Input and Output

By default, most commands that produce output write to your terminal. If you use the > character, the shell lets you send the output to a file instead:

% who > users                    Write user list to file named users
% cat file1 file2 > file3        Combine (concatenate) file1 and file2 into file3

This is known as output redirection. If the output file does not exist, it is created. If the file already exists, its contents are overwritten:

% sort data > junk                 Sort data into junk
% cal 1995 > junk                  Overwrite junk with 1995 calendar

Another form of output redirection uses the >> characters to append output to a file. You can record your disk usage as of a given date, like this:

% date > junk                   Write date into junk
% du >> junk                    Add disk space usage report to junk

When you write a command's output to a file, you can use it later in different ways without having to run the command over and over:

% who > users                    Write user list to file named users
% more users                     Look at user list
% wc -l users                    Count number of lines (i.e., number of users)
% sort users > sorted-users      Sort user list into another file

Many commands read from the terminal by default. That is, they read whatever you type until you enter a line consisting only of CTRL-D, which signifies end of file. For instance, to send a mail message, you can start up the mailer, type in your message, and then terminate the message with CTRL-D:

% mail javaman
How about joining us for lunch tomorrow?
(If you're not hungry, you can just drink coffee!)

Get Using csh & tcsh now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.