Saving Command Output to a File in Linux (original) (raw)

Last Updated : 7 May, 2026

Linux provides a powerful Command-Line Interface (CLI) for executing commands efficiently. Command output can be stored in files for logging, analysis, debugging or documentation. Saving output is especially useful when working with large results or generating system reports.

Methods to Save Command Output in Linux

Using Redirection Operators

We can use redirection operators to save command output to files. Redirection operators redirect the output of a command to a file rather than the terminal.

**Two primary operators are:

Example 1: Using > (Overwrite Mode)

Store command output in a file and remove any existing content.

**Command:

ls > sample.txt

**Note: If the file does not exist, it is created automatically.

**Output:

redirect-op

Example 2: Using >> (Append Mode)

Add new output to the end of a file without deleting existing content.

**Command:

date >> sample.txt

**Output:

redirect-app-op

Syntax of Redirection

command > filename
command >> filename

**Note: If the file does not exist, Linux creates it automatically.

Using the tee Command

Redirection operators send output only to a file. To display output on the terminal and save it simultaneously, use the tee command. tee reads standard input and writes it to both the terminal and a file.

**Syntax:

command | tee filename

**Note: Existing file content is overwritten unless the append option is used.

Example 1: Using tee

Display command output on the terminal while saving it to a file at the same time.

**Command:

ls | tee sample.txt

**Output:

tee-op

Example 2: Using tee -a (Append Mode)

Append command output to a file while displaying it on the terminal.

**Command:

echo "Hello Linux" | tee -a sample.txt

**Output:

tee-app-op

Also Check