The `2>&1` Trick for Unified Logging

Quick Tip

The `2>&1` Trick for Unified Logging

Challenge: When running commands or scripts, you often want to capture both standard output (stdout) and standard error (stderr) into a single log file for easier analysis and debugging.

The Solution: Use the `2>&1` redirection operator.

your_command &> output.log # OR your_command > output.log 2>&1

Why it works: The `>` redirects stdout (file descriptor 1). `2>&1` then redirects stderr (file descriptor 2) to wherever stdout is currently pointing (in this case, `output.log`). The `&>` is a shorthand for this common redirection.

Pro-Tip: If you want to discard error messages entirely while still capturing stdout, simply use your_command > output.log 2>/dev/null.

Linux Tips & Tricks | © ngelinux.com | 6/27/2026

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Newest
Oldest Most Voted