I'm rewriting some 20+ year old Unix scripts and I want the output to go to the screen and a file. I know this is possible with tee. Is there a similar command that stacks new lines on top, at the beginning of a file? I'd like the file to be output in reverse chronological order.  The script output will redirect to a WWW folder. For remotely viewing the output, I want to F5 in a browser and have new lines at the top of the browser window. I'll recycle the file (cp log log_$(date '+%Y%m%d') &amp;&amp; cp /dev/null log) everyday to keep it from growing too large.  I was hoping tee had a complimentary utility like cat has with tac, but alas no luck.
79395
I want to insert text at the top of an already existing file. How can I achieve this. I tried echo and tee but was not successful.  I was trying to insert repo line at the top of sources.list file from terminal.  Note  I need an one line quick solution, because another answer's method was known to me already
109280
Consider the two shell samples  $ ls myDoc.html SomeDirectory someDoc.txt   and  $ echo $(ls) myDoc.html SomeDirectory someDoc.txt   The first executes ls which, as I understand, appends the contents of the current working directory to the stdout file (which is what the terminal displays).  Is this correct?  The second takes the value of the ls command (which means is the contents of the current working directory) and prints it to the stdout file. Is this correct?  Why do the two commands give different outputs?
71966
I have a bash application that is producing some result, and I'd like to echo the result to either stdout or to a user chosen file. Because I also echo other interactive messages going to the screen, requiring the user to explicitly use the &gt; redirection when he wants to echo the result to a file is not an option (*), as those messages would also appear in the file.  Right now I have a solution, but it's ugly.  if [ -z $outfile ] then     echo "$outbuf"    # Write output buffer to the screen (stdout) else     echo "$outbuf" &gt; $outfile  # Write output buffer to file fi   I tried to have the variable $outfile to be equal to stdout, to &amp;1 and perhaps something else but it would just write to file having that name and not actually to stdout. Is there a more elegant solution?  (*) I could cheat and use stderr for that purpose, but I think it's also quite ugly, isn't it?
115023
I've got a batch file written that runs a program we've developped, passing in a number of parameters. Whilst running the program in debug mode, a large number of debug messages are output, which it would be handy to review later, although I still want to be able to see the program running "live" as it were, within the cmd.exe window.  In a *nix environment I could use Tee to output to both the text file and stdout. Any suggestions?
110915
&gt; can do it.  echo "text" &gt; file   tee can do it.  echo "test" | tee file    Can sed do it without using either of the above? Is it possible to save the output of a sed command to a file without using either &gt; or tee?
93273
I would like to clear all output from a Terminal window (including all historical output) to get it back to a clean state as if you had just opened a new Terminal window, or a new Terminal tab.  I can use the clear command to clear just the current screenful, but then you can still scroll up to see all the historical output. I'm using the Terminal to trace program output and at the end I would just like to do ⌘ + A to select all the text so I can grab the output from the just last run, without having to scroll up and see where it started. I could also just open a new tab, I was just wondering if this can be done in the same window?
57019
Normally we use a different file to redirect the output.  For example :  cat &lt; first &gt; temp   In this command the contents of first are redirected to temp instead of the standard output.  Then why does it truncates the file if I use the same file name?  Why can't it overwrite the same file?  cat &lt; first &gt; first
62738
Let's say I compile the following file with pdflatex -shell-escape test.tex:   \documentclass{minimal}  \begin{document}  File listing is:  \immediate\write18{ls /usr}  \end{document}   This will send the output of the command ls /usr to report/log of pdflatex (primarily to stdout).   There are then two cases I'd like to utilize:    The output of ls /usr being included directly in the document (LaTeX stream). The output of ls /usr becoming the contents of a \newcommand (by this I mean, that I'd like the script to be executed when the \newcommand is executed first - and upon subsequent calls to the new command, the shell code should not be executed anew).    I have read through , but I'm not sure if this "pipe input" can be applied to \newcommand.   I have also read through  - and it seems that Tex' file I/O should be used; but I'm quite disliking the fact that I'd still have to redirect the script output (actually, in this case, the ls /usr output) to a file, and then read it in, to have it as contents of a command.   So, is there an easier way to achieve what I'd want (hopefully, illustrated through an example based on the above code)?     EDIT: Ehm, I should have asked one more question earlier :) I'll try with an edit here, although it will probably get missed .. :)   I originally asked for a \newcommand that will execute shell code only upon its definition (i.e., in a sense it is "cached"); and the answer from @egreg does exactly that. But then - would it be possible to have a different \newcommand definition, such that each time this newcommand is called, the shell command is executed anew? I.e. executing something like \@@input|"cat tempfile" (which cannot be executed as such), where tempfile changes between calls?
120194
I used this command to display the first result of files in my directory.    ls | head -n 1   My simple question is, how can I modify this command to display say the nth result?  Thanks!
67894
Can I write the output of the time difference command to a file?  For example, I tried:  $ time foo.sh &gt; bar.txt   But it only gives the output of foo.sh to bar.txt.
33792
I have text file. Task - get first and last line from file after   $ cat file | grep -E "1|2|3|4" | commandtoprint  $ cat file 1 2 3 4 5   Need this without cat output (only 1 and 5).  ~$ cat file | tee &gt;(head -n 1) &gt;(wc -l) 1 2 3 4 5 5 1   Maybe awk and more shorter solution exist...
110061
To output all lines into a file /tmp/ps.txt  $ ps -e &gt;/tmp/ps.txt       To count it with wc -l  $ wc -l /tmp/ps.txt 172   To count it without exporting a file.  $ ps -e | wc -l 173   Why ps -e | wc -l  get one more line?  I don't think  for my question.  $ echo "test" | wc -l 1   Please try it in your terminal, it would yield 2 as .
8107
How can you you insert a newline from your batch file output?  I want to do something like:  echo hello\nworld   Which would output:  hello world
16488
Currently I have a shell script which logs messages to a log file like this:  log_file="/some/dir/log_file.log" echo "some text" &gt;&gt; $log_file do_some_command echo "more text" &gt;&gt; $log_file do_other_command   When executing this script, there is no output to screen, and, since I'm connecting to the server via putty, I have to open another connection and do "tail -f log_file_path.log", because I can't terminate the running script and I want to see the output in real time.  Obviously, what I want is that the text messages are printed on screen and into file, but I'd like to do it in one line, not two lines, one of which has no redirection to file.  How to achieve this?
31159
I have a bash file that I need to redirect all output to one file, debug log as well as to the terminal. I need to redirect both stdout and stderr to the debug and log it for all commands in the script.  I do not want to add 2&gt;&amp;1 | tee -a $DEBUG for every single command in the file. I could live with | tee -a $DEBUG.  I remember there was a way to do it with something like exec 2&gt;&amp;1.  Currently I'm using something like the following:  #!/bin/bash DEBUGLOG=/tmp/debug exec 2&gt;&amp;1 somecommand | tee -a $DEBUGLOG somecommand2 | tee -a $DEBUGLOG somecommand3 | tee -a $DEBUGLOG   but it does not work. Does anyone have a solution/can explain the cause?
83278
Edit: Since it appears that there's either no solution, or I'm doing something so non-standard that nobody knows - I'll revise my question to also ask: What is the best way to accomplish logging when a python app is making a lot of system calls?  My app has two modes.  In interactive mode, I want all output to go to the screen as well as to a log file, including output from any system calls.  In daemon mode, all output goes to the log.  Daemon mode works great using os.dup2().  I can't find a way to "tee" all output to a log in interactive mode, without modifying each and every system call.    In other words, I want the functionality of the command line 'tee' for any output generated by a python app, including system call output.  To clarify:   To redirect all output I do something like this, and it works great:  # open our log file so = se = open("%s.log" % self.name, 'w', 0)  # re-open stdout without buffering sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)  # redirect stdout and stderr to the log file opened above os.dup2(so.fileno(), sys.stdout.fileno()) os.dup2(se.fileno(), sys.stderr.fileno())   The nice thing about this is that it requires no special print calls from the rest of the code.  The code also runs some shell commands, so it's nice not having to deal with each of their output individually as well.  Simply, I want to do the same, except duplicating instead of redirecting.  At first thought, I thought that simply reversing the dup2's should work.  Why doesn't it?  Here's my test:   import os, sys  ### my broken solution: so = se = open("a.log", 'w', 0) sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)  os.dup2(sys.stdout.fileno(), so.fileno()) os.dup2(sys.stderr.fileno(), se.fileno()) ###  print("foo bar")  os.spawnve("P_WAIT", "/bin/ls", ["/bin/ls"], {}) os.execve("/bin/ls", ["/bin/ls"], os.environ)   The file "a.log" should be identical to what was displayed on the screen.
11550
I know there is a simple unix command that allows you to view things from the top and/or allows you to scroll indefinitely toward the top. Not sure what to call it, so it's difficult to google. Thanks to anyone who can explain how to do either one for me