Unix and Linux: Redirect Error Output To null Command

I‘m a new Linux system user. How can I redirect command error output /dev/null on a Linux or Unix-like system using Bash shell?

Your shell comes with three file descriptors as follows:
  1. stdin – 0 – Standard Input (usually keyboard or file)
  2. stdout – 1 – Standard Output (usually screen)
  3. stderr – 2 – Standard Error (usually screen)
Tutorial details
Difficulty level Easy
Root privileges No
Requirements None
Est. reading time 1m

What is a null (/dev/null) file in a Linux or Unix-like systems?

/dev/null is nothing but a special file that discards all data written to it. The length of the null device is always zero. In this example, first, send output of date command to the screen and later to the /dev/null i.e. discards date command output:

### Show on screen ###
### Discards date command output ###
date > /dev/null

Syntax: Standard Error (stderr -2 no) to a file or /dev/null

The syntax is as follows:

command 2>/dev/null
command arg1 arg2 2>/dev/null
date bar 2>/dev/null
ls -foo 2>/dev/null

In this example, send output of find command to /dev/null:
$ find /etc -type f -name '*' 2>/dev/null
The following example will cause the stderr ouput of a program to be written to a file called errors.txt:
$ find /etc/ -type f -name "*" 2> errors.txt

Linux and Unix redirect all output and error to file

The syntax is:

## send command output to output.txt and error message to error.txt ##
command > output.txt 2> error.txt
command  -arg1 -arg2 > output.txt 2> error.txt

If you want both stderr and stdout in same file, try:

command > log.txt 2>&1

Use cat command to display log.txt on screen:
cat log.txt

See man pages for more information – ksh(1).

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 4 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersdf duf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Modern utilitiesbat exa
Network UtilitiesNetHogs dig 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 glances gtop jobs killall kill pidof pstree pwdx time vtop
Searchingag grep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
4 comments… add one
  • Unix Eye Dec 5, 2013 @ 10:35

    How about?

    command  2>&1 | tee out.txt
    command opt opt2 foo 2>&1 | tee out.txt
    • 🐧 Nix Craft Jun 5, 2014 @ 10:53

      Your command will send output to both screen and file.

  • John Jun 4, 2014 @ 13:33

    command 2>&1 > log.txt
    Should be:
    command > log.txt 2>&1

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum