Jan 23, 2023 4 min read

What is /dev/null in Linux?

Table of Contents

If you have been using Linux user for a while, you must have come across the term /dev/null, at least in memes.

/Dev is a directory where most of the external peripherals are mounted such as the disk drives.

But this /dev/null file is special due to its characteristics, which I will discuss in this guide.

What is /dev/null?

It is a virtual file with a unique way of dealing with files. Anything written on /dev/null will disappear and can not be recovered. A command is supposed to generate huge output? Redirect it to /dev/null and the output will not be seen anywhere in the system, not even in the /tmp. This is why it is called the black hole of Linux.

Let me share a quick example here.

Here, I redirected my standard output to the /dev/null:

echo "Linuxhandbook" > /dev/null
rediredt standard output in linux

And as you can see, when I used the cat command to print the contents of /dev/null, it showed nothing.

Now, let's dive deep by learning the file properties of /dev/null.

File properties of /dev/null

For better readability, I will be using the stat command to know the status of the filesystem.

stat /dev/null
know the status of filesystem using the stat command

And it explains a lot.

  • It is empty
  • The creation date is the same as my last boot
  • The file's UID and GID are 0
  • And every user is allowed to read and write on /dev/null
  • It is a character device file which means it will act like an unbuffed device and can accept data streams

So you can send any number of files to the /dev/null, it can't be filled or won't increase in size.

The /dev/null acts as a file and as a result, you can not use the mv command to remove or dump directories with dev/null.

Use of /dev/null in Linux

As I mentioned earlier, it is a black hole and whatever is sent there, can not be recovered.

One of the most common use of /dev/null is to redirect either error or output of a command to this file.

Imagine a command runs for some time and it generates both standard output and errors. If you are only interested in reading the error, you can redirect the output to /dev/null so it only displays the errors.

command 1> /dev/null

Similarly, if you only want to see the standard output, not the errors, you can redirect the errors to the /dev/null.

command 2> /dev/null

You can send both standard output and the error to /dev/null. This will run the command but won't show anything on the screen.

command 2>&1 /dev/null

If all those 2>&1 stuff look like algebra to you, I highly recommend reading on stdout, stdin and stderr redirection in this article.

Input Output & Error Redirection in Linux [Beginner’s Guide]
Redirection is an essential concept in Linux. Learn how to use stdin, stdout, stderr and pipe redirection in Linux command line.

Let me so a silly but simple example.

Here, I have used two commands ls and apt update in which the command to update repositories (apt update) will require superuser privileges.

And in the ideal case, it should get me the following output:

ls && apt update
execute two commands at once using the & operator in linux

Now, I will redirect the error part to the /dev/null:

ls && apt update 2> /dev/null
redirect output to /dev/null in linux

And as you can see, the error part of the output is no longer displayed.

There are more dev files...

The /dev/null is not the only special device file. There is /dev/zero for creating files filled with zeroes or /dev/random that is used for generating random numbers.

What is /dev/zero in Linux?
One of the special device files in Linux, /dev/zero is used for creating files filled with zeroes.

And to end this article, I'll share a cool /dev/null meme with you 😎

/dev/null Linux meme
Sagar Sharma
A software engineer who loves to tinker with hardware till it gets crashed. While reviving my crashed system, you can find me reading literature, manga, or watering my plants.
Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to Linux Handbook.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.