Q&A trip to Linux’s Black Hole - /dev/null

Q&A trip to Linux’s Black Hole - /dev/null

As per NASA, “A black hole is a place in space where gravity pulls so much that even light can not get out”. Something similar exists in the Linux universe as well - it discards anything written to it and when read, just returns an EOF (end-of-file). It’s a special file which is also referred to as null device - /dev/null

So, it’s just a file?

Yes and most of the things in Linux is a file but /dev/null is not a regular file – lets dig deeper.

/dev/null 1

c in crw-rw-rw- tells us that it's a character special file, which means it processes data character by character. This can be checked using test -c as well:

/dev/null 2

What are the contents of the file?

Let’s check that using the cat command:

/dev/null 3

As stated earlier, it just returns an EOF (end-of-file) when read. So, it's empty!

What more can we know about the file?

Let’s find out using the stat command:

/dev/null 4

This tells us that its size is 0. Also, it’s good to note that the file’s read and write permission is enabled for everyone but it doesn't require execute permission. 

What happens to the file’s size when we write data to it?

Let’s try that:

/dev/null 5

The cat command returned nothing and as per the stat command, its size did not change.

As stated earlier, it discards anything written to it. You may write any amount of data to it, which will be immediately discarded, so its size will always remain 0 – Singularity?

In other words, you cannot change /dev/null

Is it useful? How to use it?

Yes, it's super useful and it comes handy in many scenarios.

Keeping in mind its 2 key properties (discards anything written to it and returns EOF on reading), let’s explore its common use-cases:

  1. We can discard output (stdout) or error (stderr) of any command/script by redirecting to /dev/null

     

    Discarding stdout:/dev/null 6

     

    Discarding stderr:/dev/null 7

     

    For reference, here is a quick refresher on the redirection operators:

    Redirect stdout to file

    > file
    
    1> file

    Redirect stderr to file

    2> file

    Redirect stdout and stderr to file

    &> file
    
    > file 2>&1
    /dev/null 8

     

    Hence, it comes handy when you run a command/script and it generates output or error that you want to ignore.

  2. We can empty a file (clear a file’s content) by redirecting output of /dev/null to it

    /dev/null 9
  3. We can provide a null or empty input to a command/script, if required

    < /dev/null

    This will provide null or empty input. In other words, it will instantly send an EOF to the command/script so that it does not wait for input.

    Note that the redirection operator < is for input (stdin).

    For example: In mail command, if we want to avoid email body, we can do so by using:

    < /dev/null

    Example: $ mail -s hello mail@nawazabbasi.com < /dev/null

What happens if we delete /dev/null ?

So you want to see the consequences of deleting the black hole? :D

Well, first let’s try to find out if we can delete it.

In order to delete a file, we need to have write permission on the directory in which the file is.

Let’s check the permission of /dev directory:

/dev/null 10

In the first column, the first character d denotes that /dev is a directory.

The subsequent characters are for owner, group and others – 3 characters each, respectively.

The owner has all the permissions rwx which means the owner of the directory can read, write and search the directory. Note that write permission allows the owner to create or delete files in the directory.

The permissions for group and others is r-x which means members of the group and other users can read and search the directory but cannot create or delete files in it.

Now let’s try deleting it:

/dev/null 11

Apparently, no permission to delete it.

However, the owner or user with root privileges should be able to delete it, right?

Let’s keep that part as DIY and a follow up question for curiosity:

If deleted, will it be automatically recreated?

Fire up your terminal & find out - All the best!

The commands used in the article are summarized in the link below.

You may download or execute it directly - BlackHole

Nawaz is a Linux CLI enthusiast and likes sharing tips and tutorials related to command-line and shell scripting. He can be reached via LinkedIn.

Load Disqus comments