Department of Applied Mathematics at the University of Colorado at Boulder
University of Colorado at Boulder Search A to Z Campus Map University of Colorado at BoulderCU Search Links
Print this page

Redirecting Output

Redirecting output

Any program or command that produces textual output to the screen (called standard output) can be simply directed to put that text into a file instead of onto the screen. Or, using the character ``|'', it can be passed directly to another command.

 


>
Use the character `>' (``right arrow'', or ``right angle bracket'', or ``greater than'') followed by a filename, after a command to create a new file with the results of the command. Example:

  • spell mydoc > error.dat     -- spellchecks the file `mydoc', putting the list of misspelled words into `error.dat'
  • myprog > data/0112.dat     -- runs the program `myprog', and directs all output into the file `0112.dat' located in the subdirectory data
  • myprog > /dev/null     -- runs the program `myprog', and directs all output into the famous Unix ``black hole'' file. /dev/null is where to send output which you don't need and don't want cluttering up a file or the screen
WARNING: If file bbb already exists, and you command   myprog > bbb,   then the old file bbb will be deleted and replaced by the output of the command `myprog'.

>>
If you want to append text output to an existing file -- without destroying its existing contents! -- then use double arrows, >>. Examples:

  • spell mydoc >> error.dat     -- spellchecks the file `mydoc', adding the list of misspelled words onto the end of pre-existing file `error.dat'
  • myprog >> data/0112.dat     -- runs the program `myprog', and directs all output onto the end of pre-existing file `0112.dat' located in the subdirectory data

|
Using |, the output from a command can be piped directly into another command. Examples:

  • spell mydoc | more     -- displays the misspelled words in `mydoc' -- but only one screenful at a time
  • spell mydoc | wc -l     -- counts the number of misspelled words in `mydoc'
  • grep help mydoc | wc -l     -- counts how many lines in `mydoc' contain the string "help"
  • grep -hi smith *.dat | sort | more     -- extract all the lines in the files *.dat containing the string "smith" (case insensitive); sort them alphabetically, and display the results one screenful at a time