Sunday, March 04, 2007

Output to Screen and Append to a File

There are a few occasions when you might want to display the output of a command and then capture its output to a file at the same time. Here is an example below.

File capture_file is created and then appended to by the tee command.
# date > capture_file; ls -l /etc | tee -a capture_file

Here is another example of output going to the screen and then appended to a file.

# echo " " >> capture_file
# echo " " >> capture_file
# cat myfile1 | tee -a capture_file
# cat myfile2 | tee -a capture_file

2 comments:

Anonymous said...

This is techinally incorrect. The pipe character is missing.

So, for example.

ls -al | tee capture_file

esofthub said...

You're right anon. I did have a "|" in there before but the Blogger parser discarded it. I imagine there a few others with a similar infraction. That's one reason I started using the textarea tag later on. Let me know if you find others. Thanks.