BASH: How to Redirect Output to File, AND Still Have it on Screen
Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
drf@maplepark ~]$ echo 'Output from echo' >archivefile
[drf@maplepark ~]$ echo 'Output from subsequent file run' |tee -a archivefile
Output from subsequent file run
[drf@maplepark ~]$ cat archivefile
Output from echo
Output from subsequent file run
Last edited by david1941; 04-02-2011 at 02:43 PM.
Reason: fix code tags
Now, what I actually want is to Append the file each time I run a command. I have already tried this but...
$ ls >> file |tee file
This is more complicated than it looks at the first glance and you should check man bash to understand better how it works. First, | "assigns" stdout of ls to stdin of tee (see "pipeline" and then "redirection" in man bash). Then, stdout of ls is "assigned" to file (leaving stdin of tee with no input at all), so ls fills file with the list of files. Then, stdout of tee is copied to file, too. Tee receives nothing on its stdin, so it writes an empty file over file before you get any chance to read the file file.
You may want to try:
ls | tee -a file
if you want to append the list of files to file and to display it on screen at the same time.
If you have nothing better to do, you can also have fun with:
What if i redirect into a file (ls -l > log.txt) and want it to show in realtime on another console (as if the command would run locally)? 'Cat log.txt' does only show a momentary snapshot. But it should permanently put out new data once appended/logged. An example would be 'tcpdump -i eth0 -n > log.txt'.