Views:
4,035,174β
Votes: 25β
Tag :
command-line
Link:
π See Original Answer on Ask Ubuntu β§ π
URL:
https://askubuntu.com/q/1198795
Title:
How do I save terminal output to a file?
ID:
/2019/12/27/How-do-I-save-terminal-output-to-a-file_
Created:
December 27, 2019
Edited: December 13, 2021
Upload:
September 15, 2024
Layout: post
TOC:
false
Navigation: false
Copy to clipboard: false
The script
command
There are two different questions here. The first is in the title:
How do I save terminal output to a file?
The second question is in the body:
How do I save the output of a command to a file?
All the answers posted here address the second question but none address the first question which has a great answer in Unix & Linux:
This answer uses a little known command called script
which saves all your shellβs output to a text file until you type exit
. The command output still appears on your screen but also appears in the text file.
The process is simple. Use:
$ script ~/outputfile.txt
Script started, file is /home/rick/outputfile.txt
$ command1
$ command2
$ command3
$ exit
exit
Script done, file is /home/rick/outputfile.txt
Then look at your recorded output of commands 1, 2 & 3 with:
cat ~/outputfile.txt
This is similar to earlier answer of:
command |& tee ~/outputfile.txt
- But you donβt have to use
|& tee ~/outputfile.txt
after eachcommnd
. - The
script
command has added benefit (or disadvantage) of reloading~/.bashrc
when it starts. - The
script
command shows the command prompt ($PS1
) followed by the command(s) you entered. - The
script
command records all the details in full color.
Send output to clipboard
Many times we want the output to go to the clipboard so we can paste it later. From this answer you can use:
cat ~/.bashrc | xclip -selection clipboard
Now you can use Ctrl+V in almost any application to paste the terminal output into your document. To paste the terminal output in the clipboard back into your terminal use Ctrl+Shift+V instead.