Is there a way that I can save the terminal command output to a file, without having to write the command ' | tee /abc.txt' every-time around?
3 Answers
One option is to use the script
program
NAME
script — make typescript of terminal session
SYNOPSIS
script [-a] [-c command] [-e] [-f] [-q] [-t[=file]] [-V] [-h] [file]
DESCRIPTION
script makes a typescript of everything printed 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).
Note however that script
saves everything in the terminal (rather than just command output) - including ANSI color sequences, which can make it difficult to extract plain text from the resulting file in the case of color terminals.
DISCLAIMER:
The following is just an idea, there may be gotchas that I'm not aware of associated with using it
Similar to @MomentumMori's suggestion of spawning a tee
-d bash subshell, if you want to get fancy with redirections you could probably do something like
exec 3>&1
to save the current standard output file descriptor, then
exec 1> >(tee -a outfile)
From this point on, standard output gets redirected to a tee
sub process that appends everything to outfile
. Because we haven't redirected the subprocess's output stream, a copy of the command output still appears in the terminal as well.
Once you're done with the commands whose output you want to capture, you can reverse the redirect and close the temporary file descriptor
exec 1>&3 3>&-
after which you can look in outfile
to see the outputs of the commands
cat outfile
-
Thanks for the idea, will try to get a 'proof of concept' and let you know.– ZuhayerMay 23, 2014 at 6:13
-
I actually 'proved the concept' before I posted. The only obvious gotcha is if you forget about it and start catting the outfile while the redirect is still in place - it gets big pretty fast ;) May 23, 2014 at 6:21
-
Yes.
Use tee
on your terminal.
$ bash | tee /abc.txt
Explanation
bash
will start another shell inside the one that you are currently using. It will look like nothing happened but, if you try to close the terminal you'll be prompted with something like :There is still a process running in this terminal. Closing the terminal will kill it.
That process, it's
bash
. It will read your command like your other shell without any issue because your first shell is already, most probably, runningbash
too. It's just an example. There are other shells and you can find out about them here : [Reviewer, insert link 1 here]When you'll be done, you may want to type
exit
to close it.|
is the character used to represent pipes. M. Jackson explains what they are here : http://www.dsj.net/compedge/shellbasics1.htmlPipes. UNIX philosophy urges the use of small yet highly focused programs that can be used together to perform complex tasks. So, learning how to string together a number of small commands on the commandline is an intrinsic part of being comfortable with Bash. To do this, we direct the standard output of one program into the standard input of another program, but we don't use redirectors, we use the pipe operator, ``|''. In UNIX, processes connected by pipes run together dynamically as data flows between them.
- And
tee
, for the sake of completeness : [Reviewer, insert link 2 here][reads] from standard input and write to standard output and files
bash
(more precisely the output of the commands run inside it) will be handed to tee
as its input. Effectively removing the need of using | tee /abc.txt
after each of your commands to see their output on the shell as well as saving it on your file system.
-
I have never done bash scriting, can u kindly elobrate the 'bash |' portion? Thanks.– ZuhayerMay 23, 2014 at 6:16
-
No.
This has to do with how the sockets called "standard input" and "standard output" work, and with the overall Unix/Linux toolchain philosophy of "Do One Thing and Do It Well." You may wish to take a minute or two to understand http://www.tldp.org/LDP/abs/html/io-redirection.html "IO Redirection in Bash"
All of that having been said, you could write a script to wrap commands with that redirection, but it still doesn't save you very many keystrokes.