Friday, December 26, 2008

Capturing output of terminal on Unix

Terminals do not have a great scroll length and even though you can increase the number of lines that can be scrolled up, it is always useful to have the output captured in a file so that you can search on it. Here are few ways

1. Redirection

cat foo.txt > bar.txt

Outout of foo.txt is captured in bar.txt

2. Redirection with "append"

ls >> bar.txt

This will append the output of "ls" to bar.txt ( Instead of overwriting it)

3. "tee"
Redirect and redirect with append will not display the content on console and hence if you want to see the content and also redirect it to a file , use "tee"

ls | tee bar.txt

this will display the contents of ls on terminal and also redirect it to bar.txt

4. output to multiple files

ls | tee a.txt b.txt c.txt

This will output contents of ls to a.txt, b.txt, c.txt

5. Outout all the terminal interaction to a file

script -f out.txt

No comments: