Skip to content
Log In Sign Up info@muvcloud.com

What /dev/null Is

It is a character device provided by the kernel, not a regular file. Writing to it always succeeds and the data is gone; reading from it returns nothing at once. Every user may read and write it. Check it with:

ls -l /dev/null

The line starts with c for character device. A program has three standard streams: input (0), output (1), and errors (2). Redirections send output or errors to /dev/null.

Discard Normal Output

sudo apt-get update > /dev/null

Normal output disappears, and error messages still appear on screen, which is often what you want.

Discard Errors Only

find / -name '*.conf' 2> /dev/null

2> redirects stream 2. Here it hides the many permission denied messages that find prints for folders you cannot read, and keeps the results.

Discard Both, in the Right Order

command > /dev/null 2>&1    # both discarded
command 2>&1 > /dev/null    # errors still on screen
command &> /dev/null         # Bash short form

Redirections are read from left to right. In the first line, output goes to /dev/null, then errors go wherever output goes now, which is /dev/null. In the second, errors are pointed at the screen first, and only output is moved afterwards. The short form &> works in Bash but not in /bin/sh on Debian and Ubuntu, where it means something else; use the long form in cron jobs and sh scripts.

In Cron Jobs

Cron mails the output of a job to the account that owns it, when a mail program is installed. Many crontabs add > /dev/null 2>&1 to stop that mail. The cost is that errors vanish too, and a failing backup goes unnoticed. Better choices:

# keep errors, drop normal output
0 2 * * * /usr/local/bin/backup.sh > /dev/null

# keep everything in a log file
0 2 * * * /usr/local/bin/backup.sh >> /var/log/backup.log 2>&1

Rotate such a log with logrotate. The schedule format is in how to create a cron job.

With nohup and Background Jobs

nohup ./worker.sh > /dev/null 2>&1 &

Without the redirection, nohup writes output to nohup.out in the current folder. Send it to /dev/null only when the job writes its own log; see nohup command.

Use It as Empty Input

command < /dev/null

A command that would wait for keyboard input gets end-of-file at once. This matters inside loops that read a file: a command such as ssh would otherwise read the rest of the file. ssh -n does the same thing.

Empty a File

cat /dev/null > /srv/app/debug.txt
: > /srv/app/debug.txt
truncate -s 0 /srv/app/debug.txt

All three leave an empty file with the same owner and permissions. A program that has the file open keeps writing to it, unlike after rm, where it keeps writing to a deleted file that still takes disk space. For log files, let logrotate handle this.

/dev/zero and /dev/urandom

/dev/zero returns an endless stream of zero bytes, used for example to create a file of a set size, and /dev/urandom returns random bytes, as in head -c 32 /dev/urandom | base64 for a random string.

If /dev/null Is Broken

If a command run as root removed /dev/null and a redirection then created a regular file in its place, ls -l no longer shows c at the start. A reboot recreates it, because /dev is filled by the kernel at boot. To fix it at once:

sudo rm /dev/null
sudo mknod -m 666 /dev/null c 1 3

Updated:

Linux VPS with Full Root Access

MUVCloud cloud servers run on KVM with Enterprise NVMe SSD storage and full root access, in Turkey, the Netherlands, and the USA.

Cloud VPS Plans Starting at2.99 $/mo Cheap Linux VPS

No Limits on Service or Support

You are not on your own as your projects grow. Our support engineers are ready 24/7, on several channels, whenever you need them.

Support specialist
  • 100% customer satisfaction goal
  • Our own hardware
  • 24/7 ticket and email support
  • 4 countries with locations