2

Is there a way in bash or zsh to automatically pipe all output from any commands I run through another program?

For example, if I ran this command

$ cat /tmp/it

I'd like it to run like I had written it like so:

$ cat /tmp/it | tee /tmp/cmdoutput

where the | tee /tmp/cmdoutput part could be any command at all. My use cases are for colorization and for saving output for commands I run so I can edit them in vim, or search them in vim. I do these things already manually but want to know if there's a way to do this automatically, and am looking for a general solution!

5
  • Do you need the command you typed in your output file?
    – oliv
    Commented Jun 26, 2018 at 14:01
  • I'm mostly interested in having any command I run, no matter what, stored in a temporary file somewhere - so if I want to review the output of any command I ran at any point today for example, I could look in a temp folder that had that had my commands stored with a time stamp, and the output there as well. Kind of a rolling log of what I'm doing at the command line. But the colorization idea is another thing I might like to use this process for as well, without having to manually pipe or alias certain commands.
    – Brad Parks
    Commented Jun 26, 2018 at 14:50
  • 1
    I advise to look at auditd. Otherwise a raw output of commands of an interractive session can simply be done with bash | tee /tmp/cmdoutput
    – oliv
    Commented Jun 26, 2018 at 14:57
  • 1
    Another alternative might be script.
    – mpy
    Commented Jun 26, 2018 at 16:49
  • How about adding an alias for the commands you mostly use?
    – Fanatique
    Commented Jul 3, 2018 at 12:09

1 Answer 1

3
+50

Use the script command :

script makes a typescript of everything displayed on your terminal. It is useful for students who need a hardcopy record of an interactive session as proof of an assignment, as the typescript file can be printed out later with lpr(1).

If the argument file is given, script saves the dialogue in this file. If no filename is given, the dialogue is saved in the file typescript.

When you are ready to start recording a log file, type:

script /tmp/cmdoutput

Now, until you stop the script, all input and output in the Terminal will be stored in cmdoutput. When you are done, just type:

exit

Source: How do I log all input and output in a terminal session?

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .