How to Redirect stdout and stderr to Separate Files in Linux

Quick Linux Tip #66:

Question: How do I save errors and normal output to different files?

Try: ./deploy.sh > success.log 2> errors.log

Info: > redirects stdout (file descriptor 1), while 2> redirects stderr (file descriptor 2). Separating the two streams keeps normal output and error messages in different files for easier troubleshooting.

Examples:

  • $ ./script > out.log 2> err.log  # Separate standard output and errors
  • $ ./script &> combined.log  # Redirect both streams to one file (Bash/Zsh)
  • $ ./script > out.log 2>&1  # Send stderr to the same file as stdout

Note: Redirection order matters: > file 2>&1 sends both streams to the file, while 2>&1 > file sends stderr to the terminal and only stdout to the file. Redirect to /dev/null when you want to discard output completely.




LinuxTeck.com
linuxteck@ubuntu:~$ ./deploy.sh > success.log 2> errors.log

linuxteck@ubuntu:~$ cat success.log
Step 1: Building Docker image... OK
Step 2: Pushing to registry... OK
Step 3: Updating Kubernetes deployment... OK
Deployment complete

linuxteck@ubuntu:~$ cat errors.log
Warning: Deprecated API version v1beta1 will be removed in Kubernetes 1.32
Warning: Image tag 'latest' is not recommended for production
linuxteck@ubuntu:~$

PREVIOUS ARTICLE Quick Linux Tip #65: How to View Child Processes in Linux with pstree NEXT ARTICLE Quick Linux Tip #67: How to Test SSD Speed in Linux with hdparm and dd
About John Britto

John Britto Founder & Chief-Editor @LinuxTeck. A Computer Geek and Linux Intellectual having more than 20+ years of experience in Linux and Open Source technologies.

View all posts by John Britto →

Leave a Reply

Your email address will not be published.