Use command 2> errors.log to save a Linux command’s standard error (stderr) to a file. The 2 is file descriptor 2, > writes to the file, and an existing file is normally truncated before the command runs.
The basic command
ls /does-not-exist 2> errors.log
cat errors.log
The diagnostic from ls is written to errors.log instead of appearing in the terminal. Any normal output sent to stdout remains connected to the terminal. Conventional Unix-like programs use file descriptor 0 for stdin, 1 for stdout, and 2 for stderr; redirection acts on these streams rather than identifying messages by their wording.
By contrast, command > output.log redirects stdout only, so errors can still appear on screen. Bash documents these descriptors and redirection rules in its reference manual.
Overwrite or append the error log
Overwrite on each run
command 2> errors.log
The shell creates the file if needed and normally truncates it before executing the command.
Recommended Free Tools
#1 Best Overall
Keep earlier entries
command 2>> errors.log
>> appends instead of truncating, making it suitable for recurring jobs such as:
./backup.sh 2>> backup-errors.log
Send stdout and stderr to separate files
command > output.log 2> errors.log
command >> output.log 2>> errors.log
The first form starts fresh files; the second appends to both. Separate streams are useful when stdout is machine-readable or consumed by another program while stderr needs independent review or alerting.
Put both streams in one file
Portable shell form
command > command.log 2>&1
command >> command.log 2>&1
These forms work in POSIX-style shell scripts: stdout is redirected first, then stderr is pointed at stdout’s current destination.
Bash shorthand
command &> command.log
command &>> command.log
&> and &>> are Bash forms, not universal Linux syntax. Use the longer form when a script may run under sh, Dash, or another shell. The Bash manual documents the equivalent forms at gnu.org.
Rank #2
Why redirection order matters
| Command | What happens |
|---|---|
command > all.log 2>&1 |
stdout goes to all.log, then stderr is duplicated to that same file. |
command 2>&1 > all.log |
stderr first follows the terminal’s stdout; stdout then goes to all.log, so stderr usually remains visible. |
Bash processes redirections from left to right. 2>&1 means “make descriptor 2 a duplicate of descriptor 1’s current destination”; it does not refer to a file named 1.
Show output while saving it with tee
command 2>&1 | tee command.log
command 2>&1 | tee -a command.log
The first command displays combined output and overwrites the log; the second appends. GNU tee copies standard input to standard output and to its file arguments, with -a selecting append mode. See the GNU Coreutils manual. A current GNU tee manual mirrored by Linux man-pages is available at man7.org; distributions may ship a different coreutils version.
To log only stderr while keeping it visible in Bash:
command 2> >(tee -a errors.log >&2)
Process substitution is Bash-specific. Portable scripts should use a temporary file or another shell-appropriate method.
Free tools Windows power users keep installed
One-click scans. No signup required.
Rank #3
Understand redirection in pipelines
A pipe connects the preceding command’s stdout to the next command’s stdin. Stderr does not automatically travel through |.
command1 2> errors.log | command2
command1 | command2 2> errors.log
The first captures only command1’s stderr; the second captures only command2’s stderr. To combine a pipeline’s output and diagnostics into one file:
command1 | command2 > pipeline.log 2>&1
For diagnostics from both commands, give them a shared descriptor:
{ command1 2>&3 | command2 2>&3; } 3> errors.log
When using tee in automation, Bash normally reports the status of the last pipeline command. Enable pipefail if the original command’s failure must affect the pipeline status:
Rank #4
set -o pipefail
command 2>&1 | tee -a command.log
status=$?
Redirect an entire script
Only script errors
#!/usr/bin/env bash
exec 2> errors.log
After this line, later commands in the script inherit the redirected stderr unless they override it.
Both streams
exec > script.log 2>&1
exec >> script.log 2>&1
The first starts a new log; the second appends. exec changes the current shell’s file descriptors and does not launch another command. To capture parse or startup diagnostics that occur before a script reaches its exec line, redirect from outside:
bash script.sh 2> script-errors.log
Permissions, paths, and failed redirections
The shell opens the destination before running the command. A missing parent directory, insufficient permission, read-only filesystem, invalid path, disk-full condition, or quota limit can therefore prevent the command from running at all.
mkdir -p "$HOME/logs"
command 2> "$HOME/logs/errors.log"
pwd
ls -ld "$(dirname "$HOME/logs/errors.log")"
touch "$HOME/logs/errors.log"
Quote variable-based paths, especially when they may contain spaces. If opening the target fails, the shell’s own diagnostic cannot be captured in that same file.
Best Value
- 1. IDEAL SIZE FOR EASY DISPLAY Available in 8 x 12 inches this metal tin sign is designed with the perfect proportions for clear visibility and attractive wall decoration. Its compact size fits easily into a variety of spaces while making a stylish decorative statement without overwhelming your room.
- 2. PREMIUM METAL CONSTRUCTIONCrafted from durable, high-quality metal with vibrant HD printing, this vintage tin sign is waterproof, UV-resistant, rust-resistant, and fade-resistant for long-lasting indoor or outdoor use. The smooth surface and rounded edges provide a clean appearance and safe handling.
- 3. QUICK & EASY TO HANGEach metal wall sign comes with four pre-drilled mounting holes, allowing for fast installation using screws, nails, hooks, or double-sided adhesive tape (hardware not included). Lightweight yet sturdy, it can be displayed effortlessly on walls, doors, fences, or other flat surfaces.
- 4. CLASSIC VINTAGE STYLEFeaturing timeless artwork and retro-inspired design, this decorative metal sign adds character and charm to any setting. Whether your décor is farmhouse, rustic, industrial, modern, country, or vintage, this wall plaque creates a unique focal point and enhances the overall atmosphere of your space.
- 5. Perfect Gift for Decoration LoversA unique and thoughtful gift choice for family, friends, and collectors who love vintage artwork and wall decorations. Ideal for birthdays, housewarming, holidays, Christmas, or any special occasion.
Protected system logs and sudo
sudo command > /var/log/example.log
Here, the current unprivileged shell performs >, so it may report “Permission denied” before sudo runs the command. Let a privileged shell perform the redirection instead:
sudo sh -c 'command > /var/log/example.log'
To display output and append it through a privileged writer:
command 2>&1 | sudo tee -a /var/log/example.log
Use this deliberately because the command’s output is being passed to a privileged process.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Suppress diagnostics when appropriate
command 2> /dev/null
command > /dev/null 2>&1
The first discards stderr; the second discards both streams. Bash also permits command &> /dev/null. Suppression does not make a command reliable: capture or check its exit status when failures matter.
PC Slower Than It Used to Be?
A free scan shows the junk files, broken settings and background clutter dragging Windows down - then fixes them in one click.Free scan · Windows 10 & 11Outdated Drivers Are Slowing You Down
One free scan finds every outdated or missing driver and matches the right update for your exact hardware.Free scan · exact hardware matchQuick Recap
Common surprises
- An existing log is empty:
>truncates; use>>to preserve history. - Errors still appear: you redirected stdout only, used reversed
2>&1order, or the program writes diagnostics to another descriptor. - “Error” text is missing: redirection captures descriptor 2, not every line that looks like an error. Some programs write such text to stdout.
&>is rejected: the script is running under a shell that does not support Bash syntax.- Combined output order looks odd: stdout and stderr can be buffered differently, so merging descriptors does not guarantee the order messages were conceptually generated.
- The log exists but the command failed: file creation proves only that the shell established redirection, not that the command succeeded.
Quick reference
| Goal | Command |
|---|---|
| Only stderr, overwrite | command 2> errors.log |
| Only stderr, append | command 2>> errors.log |
| Separate stdout and stderr | command > out.log 2> err.log |
| Both streams, overwrite | command > all.log 2>&1 |
| Both streams, append | command >> all.log 2>&1 |
| Show and save combined output | command 2>&1 | tee all.log |
| Show and append combined output | command 2>&1 | tee -a all.log |
| Discard stderr | command 2> /dev/null |
| Script-wide stderr | exec 2> errors.log |
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




