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 > usersWrite user list to file named users %cat file1 file2 > file3Combine (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 > junkSort data into junk %cal 1995 > junkOverwrite 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 > junkWrite date into junk %du >> junkAdd 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 > usersWrite user list to file named users %more usersLook at user list %wc -l usersCount number of lines (i.e., number of users) %sort users > sorted-usersSort 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 javamanHow about joining us for lunch tomorrow?(If you're not hungry, you can just drink coffee!)
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access