How to save terminal output to a file under Linux/Unix

There are several ways to save terminal output to a file under Linux or Unix:

  1. Using the > operator: This operator redirects the output of a command to a file. For example, to save the output of the ls command to a file named “ls_output.txt”, you would use the following command:
ls > ls_output.txt
  1. Using the tee command: The tee command allows you to both display the output on the screen and save it to a file. For example, to save the output of the ls command 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
  1. Using the script command: The script command 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
  1. Using the journalctl command: The journalctl command allows you to view and save system logs. You can use the -o option 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.

Leave a Comment