How to redirect standard error in bash

I am trying to redirect bash message into file named output.log. But, it is not getting redirected. How do I redirect both standard output and standard error in bash shell? In Linux, how do I redirect error messages?

Standard error (also known as stderr) is the default error output device. Use stderr to write all system error messages. The number two (2) denotes the stderr. The default stderr is the screen or monitor. Standard output (also known as stdout) is used by a command to writes (display) its output. The default stdout is the screen. It is denoted by one number (1).

ADVERTISEMENTS

2> is input redirection symbol and syntax is:

  1. To redirect stderr (standard error) to a file:
    command 2> errors.txt
  2. To redirect both stderr and stdout (standard output):
    command &> output.txt

How to redirect standard error in bash
You must replace command with the command you want to run. Let us see some examples that explains redirection of standard error in bash.

How to redirect standard error in bash

Run find command and save all error messages to find.error.txt file:
find / -name "*.conf" 2> find.error.txt
You can view find.error.txt with the cat command:
cat find.error.txt
Sample outputs:

find: ‘/boot/grub2’: Permission denied
find: ‘/proc/tty/driver’: Permission denied
find: ‘/proc/1/task/1/fd’: Permission denied
find: ‘/proc/1/task/1/fdinfo’: Permission denied
...
..
find: ‘/proc/963/task/963/ns’: Permission denied
find: ‘/proc/963/task/1078/fd’: Permission denied
find: ‘/proc/963/task/1078/fdinfo’: Permission denied
....
..
find: ‘/var/spool/postfix/public’: Permission denied
find: ‘/var/spool/postfix/saved’: Permission denied
find: ‘/var/spool/postfix/trace’: Permission denied
find: ‘/usr/share/polkit-1/rules.d’: Permission denied
find: ‘/usr/libexec/initscripts/legacy-actions/auditd’: Permission denied

You need to use “2>” when you want to redirect stderr to a file. You can redirect stdout to file named results.txt and stderr to file named errors.txt:
find / -name "*.conf" >results.txt 2>error.txt
Verify results with the cat command:
cat results.txt
cat error.txt

This is useful in shell scripts or any other purpose.

How to redirect standard error and standard output in bash

You can send both stdout and stderr to a file named output.txt
command &>output.xt
find / -name "*.pl" &>filelist.txt

Please note that both errors and actual output of the find command stored into a file:
cat filelist.txt
Sample outputs:

/usr/bin/rsyslog-recover-qi.pl
/usr/lib/grub/i386-pc/gmodule.pl
/usr/lib/firmware/dsp56k/concat-bootstrap.pl
/usr/share/doc/GeoIP-1.5.0/fetch-geoipdata-city.pl
/usr/share/doc/GeoIP-1.5.0/fetch-geoipdata.pl
/usr/share/doc/postfix-2.10.1/examples/smtpd-policy/greylist.pl
find: ‘/usr/share/polkit-1/rules.d’: Permission denied
find: ‘/usr/libexec/initscripts/legacy-actions/auditd’: Permission denied

Summary

CommandDescription/Purpose
command 2>filenameRedirect stderr to filename
command >output 2>filenameRedirect stderr to file named filename and stdout to file named output
command &> filenameRedirect stderr and stdout to filename
command 2>&-Just suppress error messages. No file created. No error message displayed on screen
command 2>&1

die(){
echo "$1" 2>&1
}
die "File not found"
Redirect error messages to standard output. Useful in shell script when you need to forcefully display error messages on screen

For more info see bash man page online or read it at the cli by using the man command:
man bash

🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallCentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNCentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
0 comments… add one

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.