There are several ways to save terminal output to a file under Linux or Unix:
- Using the
>operator: This operator redirects the output of a command to a file. For example, to save the output of thelscommand to a file named “ls_output.txt”, you would use the following command:
ls > ls_output.txt
- Using the
teecommand: Theteecommand allows you to both display the output on the screen and save it to a file. For example, to save the output of thelscommand to a file named “ls_output.txt” while also displaying it on the screen, you would use the following command:
ls | tee ls_output.txt
- Using the
scriptcommand: Thescriptcommand creates a typescript of everything that happens on the terminal. It can be used to save all the output of a terminal session to a file. For example, to save all the output of a terminal session to a file named “terminal_session.txt”, you would use the following command:
script terminal_session.txt
- Using the
journalctlcommand: Thejournalctlcommand allows you to view and save system logs. You can use the-ooption to output the logs in a specific format. For example, to save the logs to a file in the format of json, you would use the following command:
journalctl -o json > logs.json
It’s important to keep in mind that when you use the > operator, if the file already exists, it will be overwritten, and all the previous data will be lost. Also, you can use >> operator to append the output to an existing file instead of overwriting it.