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.
Notices
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.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
[drf@maplepark ~]$
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...
Code:
$ 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:
Code:
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'.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.