Command Line Basics: Redirecting Output

August 23, 2009 by
Filed under: bash, command line, HowTo, linux 

In today's command line basics, I'm going to show you how to redirect output from the bash cli (command line interface). The output of most GNU command line tools is normally sent to the terminal (ie - standard output). There are times when you would like to have the output saved as a file. Now, you could copy the text from the terminal and paste it into a text editor and save it, but there's a better way.

The GNU tools in your typical Linux distro provide a simple way to save the command output. This is done by using the greater than symbol ( > ).

Say you would like to save a listing of all your files in your home directory and call it files.txt. You would simply enter the following at the command line prompt:

ls > files.txt

Redirecting output with > will create a new file. What if you want to append the output to the end of an existing file? Well you can do that with >>.

Affiliate Link

For example, you can append the date and time to the end of your files.txt file. The date command will print the current time and date. Try it out in the terminal.

date

Now use the same command, but redirect the output to append it to the end of files.txt.

date >> files.txt

You can view your file with the cat command.

cat files.txt

Comments

One Response to “Command Line Basics: Redirecting Output”

  1. Story added...

    This story has been submitted to fsdaily.com! If you think this story should be read by the free software community, come vote it up and discuss it here:

    http://www.fsdaily.com/Beginner/Command_Line_Basics_Redirecting_Output

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>