Bash: How to redirect every command’s output to a file

aliasbash

I often run searches on my source code repo like this:

ack -l "foo bar" .

The searches can take 10-60 seconds and produce output like this:

path/to/file1       
path/to/file2
path/to/file3

I frequently use this alias to open all of the files found in vim:

alias go='gvim `fc -s` -p'

This works OK, but fc -s reruns the previous command, which can take seconds or minutes.


It would be very useful to append something like this to every command I run interactively:

| tee /tmp/lastCommand to all commands.  

This way, if the output reveals itself to be useful, then I can do something else to it.

Example usage:

Type:

find . -type f 

Which executes the following:

find . -type f | tee $lastOutLocation # Where lastOutputLocation=/tmp/lastOutput

You could then use something like this, to filter the previous command:

lastOut | grep "SomeString" # Where lastOut is an alias to cat $lastOutputLocation

Is there a mechanism I can use to do something like this? I don't think that aliases expose this kind of behaviour.

Potential Hurtles:

  • Getting something that works with compound commands: echo 'baz' ; echo 'bar'
  • Figure out how to modify the command the user typed before running it
  • Ignore things like interactive input. Don't want to capture password prompts

Possible solutions, I am currently exploring:

Best Answer

It would be very useful to append something like this to every command I run interactively:

| tee /tmp/lastCommand to all commands.  

A potential start might be to use ~/.inputrc to bind a key. (That's the configuration file for the GNU Readline library.) For example, using the letter, o, as a mnemonic for output ...

Control-o: " | tee /tmp/lastCommand"

One could bind to the Return/Enter key, but that action would have unintended effects.

Related Question