TechToFreedom
Published in

TechToFreedom

You have 2 free member-only stories left this month.

3 Ways To Save Terminal Output to Files in Linux

Conveniently and flexibly

Anime scenery

If you are a programmer, especially if you’re a backend developer. It’s inevitable that you need to do something on a Linux terminal instead of a GUI. One obvious problem is that the terminal is not visual-friendly, especially when you want to check some large-size standard output (stdout).

A good solution is saving the hard-to-read stdout into a separate file and checking that file.

Based on different use scenarios, there are 3 different requirements:

  1. Just saving the terminal output to a file
  2. Print the output and save it to a file
  3. Record all input and output of the terminal and save it to a file

This article will introduce 3 methods for the above 3 tasks.

1. Angle brackets: Save Standard Output (stdout) to a File

If we just need to save stdout in a file, the angle brackets can make our lives easier.

For example, to save the list of all the files or directories under the current path to test.txt, the command is the following:

ls > test.txt

One angle bracket will overwrite the whole test.txt file. If our purpose is to append new content to the file, double brackets can help:

ls >> test.txt

2. tee Command: Print and Save

In some cases, we may need to check the stdout on the terminal and save it to a file at the same time. This is the showtime of the tee command.

The tee command occurs in many Shell scripts. Cause during the execution of a script, it can make everything clearer if what will be saved on a file can also be printed on the terminal.

The tee command is mostly used inside a pipeline, the basic structure is:

[command] | tee [options] [filename]

For instance, the following command means to print the stdout on the terminal and save it to test.txt.

ls | tee test.txt

If we would like to append the content to test.txt, just add the -a option:

ls | tee -a test.txt

Since it can be applied to a pipeline, we can implement some complex operations by very neat commands. For example, to find some specific files whose name includes “books”, we can add the grep command after the tee command:

ls | tee test.txt | grep "books"

3. script Command: Record the Whole Process

We can start a “script” environment and save everything on the test.txt:

script test.txt

By the way, if we didn’t define the file name, the script command will create a special file named typescript automatically.

After executing the script command on a Linux terminal, we enter a special environment and it will record all the stdin and stdout until we exit the “script” environment.

To exit it, just run the exit command.

The problem of this method is that all the control characters will also be written in the test.txt, which makes the file hard to read for human. The solutions for it are out of the scope of this article. (Here is a reference. )

Thanks for reading. If you like it, please follow me and become a Medium member to enjoy more great articles. 🙂

Enjoy the read? Reward the writer.Beta

Your tip will go to Yang Zhou through a third-party platform of their choice, letting them know you appreciate their story.

Get an email whenever Yang Zhou publishes.

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Medium sent you an email at to complete your subscription.

Technology gives us more and more freedom. Start learning today.

Share your ideas with millions of readers.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store