link to content
Essentials at NC State Home
skip header navigation and go to content Help | ResNet | Computing@NC State | For OIT Staff | Publications | Search NC State | Feedback | Site Map
your unity account
antivirus & security
email & messaging
connections & labs
your computer
software@nc state
files
web pages
education & training
publications
other resources
troubleshooting
ITD Sections

Redirecting commands

Normally everything you do on the keyboard is sent to the computer screen so that you can see both what you typed and what happened as a result.

Your typing on the keyboard is the "input," and what you see on the screen is the "output." To Unix, a computer screen and a file are forms of output. The screen is the standard output, normally abbreviated as "stdout." With redirection you can send output to files instead of the screen, or you can use what's in a file as the input for a command. With redirection you can change where the computer gets input and where it puts output.

You use the symbols < and > to redirect input and output.

For example, to redirect output and send a directory listing to a file instead of the screen, you'd enter the command

ls -la > mydir

What this command does is list all the files and directories in long form and send them to the file "mydir". If the file "mydir" does not exist, it will be created. If it does exist, it will be overwritten with the new data.

To append information to the end of a file which already exists, use two greater-than signs. For example, the command

ls >> mydir

appends the data from a short file listing to the more complete listing already existing in the file "mydir". If you type the above commands and then look at the results with a file viewer like more, you'll see a directory listing in short form connected to the end of one in long form.

Redirecting input is just as easy. For example, say you want to mail someone the directory list you just made. Instead of having to import it into a mail program, you can redirect the input for the mail command by typing

mail userID < mydir

This command uses the contents of the "mydir" as the input for the mail command.

To connect several files in order and have them output as one file, use the cat command and redirection. For example, the command

cat section1 section2 section3 > Chapter1

reads all three files (section1, section2, section3) and concatenates each to the file "Chapter1," where all three sections will appear in the order that they are listed in the cat command.

If you wanted to add one more section to the file "Chapter1," you'd append the text using the command

cat section4 >> Chapter1

to make sure the text that already exists in the file isn't disturbed.

NOTE: Even though you send the "standard output" to a file, if an error occurs, the error message will be sent to your screen because it isn't classified as "standard." Errors are special cases. If you need to redirect error messages along with the standard output, place an ampersand (&) after the > or >> command. For example:

cat file1 file2 file3 >& Finished_file

Here are two more examples for you to experiment with.

  • The command zwho > people prints the list of active users to the file "people".
  • The command sort file1 > sorted_list sorts the contents of "file1" and saves the sorted list as "sorted_list".

 


Last modified July 20, 2004 by cawalker

jump back to content/page ends, begin footer
jump to content
jump to content Go to page top Page Top | Site Map | OIT | Policy Disclaimer | Site Survey