What is / dev / null and how can it help you?

If we already have some notions of the GNU / Linux directory tree, we should at least be familiar with the / dev / reference, which is effectively where all the files related to hardware devices.

If we look in the directory / dev / we will see a "file" called null, but if we want to open it to see its content, the system will tell us that it is not possible since it is not ordinary content. I have enclosed the word file because, as you all know for Linux everything (hardware and software) is represented as a file.

This is a contribution from Daniel Durante, thus becoming one of the winners of our weekly competition: «Share what you know about Linux«. Congratulations Daniel!

What device does / dev / null correspond to?

For practical purposes, imagine a garbage can, a bottomless pit or outer space into which to throw anything without the possibility of recovering it (no matter how hard the guys from NASA try).

But if I already have commands like rm, why do I want something new to delete?

Because the way both “black holes” work is completely different: how would you go about overriding the standard output of an error in a command within a shell script at runtime? This is where / dev / null comes in.

Let's see it with an example.

We have created a file called tests containing the string "Hello World". If we want to represent the content of that file on the command line, we could do it in the following way:

user @ laptop: ~ $ cat test
Hello World

If the file did not exist or was named as tests (with an 's' at the end), we would get the following error in the console:

user @ laptop: ~ $ cat tests
cat: tests: The file or directory does not exist

What could we do to avoid the error message? Well, simply redirect the output of the command, in case of error, to a "garbage can", that is to / dev / null

How do we specify it to be in case of error? Here you enter the standard input, output and error values ​​for a program: STDIN, STDOUT and STDERR (which can be substituted for 0, 1 and 2 respectively). In this way, if we put ...

user @ laptop: ~ $ cat tests 2> / dev / null
user @ laptop: ~ $

… We will see that the error message will not be produced on the console.

You have to be careful because the syntax is essential: between the characters 2 and> there should be no space. Otherwise, it would give the following:

user @ laptop: ~ $ cat tests 2> / dev / null
cat: tests: The file or directory does not exist
cat: 2: The file or directory does not exist

In contrast, a space between> and / dev / null will not negatively affect the result.

We could also use error redirection, for example, to capture errors in a log file as follows:

user @ laptop: ~ $ cat tests 2> err.log

Another interesting case would be the collection of results in another file as long as an error does not occur, for which we would put:

user @ laptop: ~ $ cat test 1> output_result 2> err.log

Finally, it is worth putting the expression «> / dev / null 2> & 1» in which the standard output and the error output are combined, redirecting them so that in no case the output information is obtained.


The content of the article adheres to our principles of editorial ethics. To report an error click here!.

7 comments, leave yours

Leave a Comment

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

*

*

  1. Responsible for the data: Miguel Ángel Gatón
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.

  1.   Guido Ignatius Ignatius said

    Ah, but one of the most used utilities that / dev / null has is missing, which is emptying files: $ cat / dev / null> file.log In this way, the file.log file will be empty. Add it!

    1.    Edward H. said

      It was precisely the explanation he was looking for.
      I support the motion to add it =)

      Greetings!

  2.   Pablo said

    Hello, first of all the article is very good! the second I would like to contribute something with this link on the subject cron job in php from Cpanel and the third congratulations for the blog!

  3.   Pablo said

    Nice article on dev / null, what a shame I think I mistakenly commented in the wrong place before! I apologize

  4.   Anonymous said

    good contribution thanks

  5.   jerseys said

    Greetings I am getting null attack. I use andrirc and I get a private from my nickname with the word Null. 2 seconds later the program closes I have been reading and from what I see this can only be done by the Shell, not someone external. I've tried to ignore myself / ignore -lrpcntikd and Nothing surprises me the command keeps coming. If you have any way to try to block it I would appreciate it. Cheers

  6.   Sofia martinez said

    What happens if when executing the sentence the symbol> is not placed?

    Could someone guide me please?