- by x32x01 ||
When you run a command or script in a terminal, it prints output on the screen. Often you want to keep that output - for logs, reports, debugging, or sharing results. Instead of copy-paste, Linux gives easy ways to save command output to a file. This guide shows simple methods: redirection, append mode, redirecting errors, and the tee command. All examples work on most distributions (Ubuntu, Debian, Fedora, etc.). 
Why save command output?
Saving output is useful when you want to:

#1 Use redirection operators > and >>
The simplest way to save output is with shell redirection.
Examples:
If output.txt does not exist, both commands create it. Using > will overwrite previous data — so be careful. Using >> is safer when you want to keep prior runs. 
Capture both stdout and stderr (errors) 2>&1
Many commands print useful messages to stderr (error stream). To store both normal output and errors into the same file, append 2>&1:
Explanation:

#2 Use tee to display and save at the same time
If you want to see output live on the terminal while also saving it to a file, use tee. It reads stdin and writes to both stdout and a file.
tee is great for interactive sessions or when you run a long command and want a log without losing the terminal output.
To capture both stdout and stderr with tee:
This writes everything to run.log while letting you watch the output live. Perfect for monitored scripts. 
Useful real-world examples
Save output of a network test:
Save package install logs:
Capture a script run:
These patterns are reliable and easy to automate in cron jobs or CI pipelines.
Best practices and tips
Troubleshooting common issues
Quick recap

Why save command output?
Saving output is useful when you want to:- Keep a record of a command run (logs).
- Share results with teammates.
- Process or parse output later with scripts.
- Debug failures by capturing both stdout and stderr.
#1 Use redirection operators > and >>
The simplest way to save output is with shell redirection.- > writes output to a file and replaces existing content.
- >> appends output to the end of a file.
Examples:
Bash:
# Replace file contents (create output.txt if missing)
ping -c 3 google.com > output.txt
# Append to file (keep previous results)
ping -c 3 bing.com >> output.txt Capture both stdout and stderr (errors) 2>&1
Many commands print useful messages to stderr (error stream). To store both normal output and errors into the same file, append 2>&1: Bash:
# Append stdout and stderr to output.txt
some-command >> output.txt 2>&1
# Or overwrite file with both streams
some-command > output.txt 2>&1 Explanation:
- 1 is stdout, 2 is stderr.
- 2>&1 redirects stderr into stdout, then file redirection captures both.
#2 Use tee to display and save at the same time 
If you want to see output live on the terminal while also saving it to a file, use tee. It reads stdin and writes to both stdout and a file. Bash:
# Save output to file and show it on screen
ls -la /var/log | tee files-list.txt
# Use append mode with -a
ls -la /var/log | tee -a files-list.txt tee is great for interactive sessions or when you run a long command and want a log without losing the terminal output.
To capture both stdout and stderr with tee:
Bash:
# Pipe both streams to tee
some-command 2>&1 | tee run.log Useful real-world examples
Save output of a network test: Bash:
# Save ping results, include errors
ping -c 5 example.com > ping-results.txt 2>&1 Save package install logs:
Bash:
# For Debian/Ubuntu
sudo apt update 2>&1 | tee apt-update.log
sudo apt install -y nginx 2>&1 | tee -a apt-update.log Capture a script run:
Bash:
# Run a Python script and save everything
python3 myscript.py > myscript.log 2>&1 These patterns are reliable and easy to automate in cron jobs or CI pipelines.
Best practices and tips
- Check file paths: Always confirm the path where you save logs (e.g.,
/var/log/myjob/vs~/logs/). - Rotate logs: For long-running systems, use logrotate or name files with timestamps:
some-command > "run-$(date +%F_%H%M%S).log" 2>&1
- Avoid overwriting if you need history - prefer
>>ortee -a. - Protect sensitive data: Logs may contain secrets. Secure files with proper permissions:
chmod 600 secret-log.txt - Use structured output (JSON) when parsing logs programmatically. That makes downstream automation easier.

Troubleshooting common issues
- File seems empty after redirection? Check you used the correct device path or that the command actually produced output.
- Permission denied? Use sudo if you’re writing to a protected folder (but be careful):
sudo some-command > /root/out.txt 2>&1
Or run the command and pipe into sudo tee to preserve permissions:
some-command 2>&1 | sudo tee /root/out.txt - Very large log files? Compress them (gzip) or rotate regularly.
Quick recap
>- overwrite output file.>>- append to file.2>&1- capture stderr and stdout together.tee- show output and save at once (tee -ato append).
Last edited: