Unix and Linux Redirect Error Output To null Command

See all Bash/Shell scripting related FAQ
I‘m a new Linux system user, and here is how to redirect command error output /dev/null file on a Linux or Unix-like system using Bash shell.

Your shell such as Bash, KSH, SH and others 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 Linux or Unix terminal
Category Linux shell scripting
OS compatibility BSD Linux macOS Unix
Est. reading time 3 minutes

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

The ‘/dev/null‘ file is a unique file that simply removes any data written to it. It always has a length of zero. To illustrate, you can use the ‘date’ command as an example. First, you can send the output of the ‘date’ command to the screen and then send it to ‘/dev/null’ to discard the command output or result. For instance:

### Show date command outputs on the screen ###
date
 
### NOW, Discards date command output by sending to the /dev/null###
date > /dev/null
Unix and Linux: Redirect Error Output To null Command

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

The syntax is as follows to redirect stderr outputs to a /dev/null file on a Linux, macOS, FreeBSD, and Unix-like system:

# DO NOT PUT gap between '2' and '>' #
command 2>/dev/null
/path/to/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 the /dev/null on Linux:
$ 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
Want to view those errors saved in the errors.txt file? Try the “cat“, “bat“, “more“, or “less” as follows:
$ cat errors.txt
Linux can check disk space using the df command, and we can hide messages such as “Permission denied” using the following syntax:
$ df -h /backups/users 2>/dev/null
Please do not leave a gap (white/blank space) between 2 and > /dev/null. This will result in errors:
$ df -h /backups/users 2    >    /dev/null
Outputs showing errors:

df: /backups/users : Permission denied
df: 2: No such file or directory

The correct syntax is as follows without any blank/white space between 2 and > /dev/null:
$ df -h /backups/users 2>/dev/null

Linux and Unix redirect both (all) output and error to file

The syntax is as follows to send both stdout and stderr to a file.

## 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 redirected in the same file, try the following syntax:

command > log.txt 2>&1

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

Summing up

When using macOS, *BSD, Linux, or Unix-like systems, you can redirect stdout and stderr to /dev/null or even to a text file, as described in this tutorial. For more information, check out the manual pages using man command, help command, or info command. Here’s an example:
$ man bash
$ man ksh
$ info

🥺 Was this helpful? Please add a comment to show your appreciation or feedback.

nixCrat Tux Pixel Penguin
Hi! 🤠
I'm Vivek Gite, and I write about Linux, macOS, Unix, IT, programming, infosec, and open source. Subscribe to my RSS feed or email newsletter for updates.

6 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

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

  • Paul Jun 14, 2023 @ 17:27

    Didn’t work for me

    tar --no-overwrite-dir -k -xv -f test.tar 2>/dev/null

    tar: 2: Not found in archive
    tar: Exiting with failure status due to previous errors

    • 🛡️ Vivek Gite (Author and Admin) Vivek Gite Jun 14, 2023 @ 20:32

      Make sure there is no gap between 2 and >/dev/null. In other words:

      tar  -xv -f test.tar  2>/dev/null

      Syntax to redirect error and output messages to /dev/null:

      tar  -xv -f test.tar  &>/dev/null

      Also please note that on GNU/tar (Linux version) you can not use the ‘-k‘/--keep-old-files‘ with ‘--no-overwrite-dir

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre> for code samples. Your comment will appear only after approval by the site admin.