5 Easy Ways to Save Terminal Output to File (Linux)

5/5 - (3 votes)

βœ… Problem Formulation: What are the various ways to save terminal output in Linux? For example, you might need to capture output for later review, document command results for reporting, or track error messages for debugging.

Method 1: Redirection Operator

Use the > operator to redirect output to a file. For example, ls > file.txt saves the output of ls to file.txt.

echo "Hello World" > hello.txt

This uses echo to print "Hello World" and the > operator redirects this output to hello.txt. If the file doesn’t exist, it’s created.

Method 2: Append Operator

To append output to an existing file, use the >> operator, e.g., ls >> file.txt.

echo "Adding a line" >> hello.txt

Here, echo adds “Adding a line” to the end of hello.txt. The >> operator is used for appending instead of overwriting.

Method 3: tee Command

The tee command reads from standard input and writes to standard output and files. For example, ls | tee file.txt displays output on the terminal and saves it to file.txt.

ls | tee directory_list.txt

This command lists directory contents using ls and simultaneously displays them on the terminal and saves to directory_list.txt using tee.

Method 4: script Command

Use script to capture all terminal activity. Start with script output.txt, run your commands, and exit with exit.

script -c 'ls' session.txt

The script command starts a new shell session, runs ls, and saves all terminal activity to session.txt, including the output of ls.

Method 5: Redirecting Standard Error

To save error messages, redirect standard error using 2>, e.g., ls non_existing_folder 2> error.txt.

ls nonexistent_directory 2> error_log.txt

Summary and More Methods

To efficiently write the output of a command to a file in a terminal, there are several methods you can use, each with specific behaviors regarding standard output (stdout) and standard error (stderr).

These methods vary based on whether you want the output visible in the terminal, visible in the file, and whether an existing file should be overwritten or appended to:

  1. Simple Redirection (> and >>):
    • command > output.txt: Redirects stdout to a file, overwriting it if it exists. Stdout is not visible in the terminal.
    • command >> output.txt: Redirects stdout to a file, appending to it if it exists. Stdout is not visible in the terminal.
  2. Redirecting Standard Error (2> and 2>>):
    • command 2> output.txt: Redirects stderr to a file, overwriting it. Stderr is not visible in the terminal.
    • command 2>> output.txt: Redirects stderr to a file, appending to it. Stderr is not visible in the terminal.
  3. Redirecting Both stdout and stderr (&> and &>>):
    • command &> output.txt: Redirects both stdout and stderr to a file, overwriting it. Neither is visible in the terminal.
    • command &>> output.txt: Redirects both stdout and stderr to a file, appending to it. Neither is visible in the terminal.
  4. Using tee for stdout (| tee and | tee -a):
    • command | tee output.txt: Copies stdout to a file while keeping it visible in the terminal, overwriting the file.
    • command | tee -a output.txt: Copies stdout to a file, appending to it if it exists, while keeping it visible in the terminal.
  5. Using tee for Both stdout and stderr (|& tee and |& tee -a):
    • command |& tee output.txt: Copies both stdout and stderr to a file while keeping them visible in the terminal, overwriting the file.
    • command |& tee -a output.txt: Copies both stdout and stderr to a file, appending to it if it exists, while keeping them visible in the terminal.

πŸ’‘ Note: The |& syntax may not work in older versions of bash, such as those found on macOS. In such cases, using 2>&1 | is a suitable alternative. Additionally, there isn’t a shorthand syntax in Bash for piping only stderr to a command; complex scenarios may require stream swapping or other advanced techniques.