One such example is /dev/null. It’s a special file that’s present in every single Linux system. However, unlike most other virtual files, instead of reading, it’s used to write. Whatever you write to /dev/null will be discarded, forgotten into the void. It’s known as the null device in a UNIX system.
Why would you want to discard something into the void? Let’s check out what /dev/null is and its usage.
Prerequisites
Before diving deep into the usage of /dev/null, we have to have a clear grasp of the stdout and stderr data stream. Check out this in-depth guide on stdin, stderr, and stdout.
Let’s have a quick refresh. Whenever any command-line utility is run, it generates two outputs. The output goes to stdout and the error (if generated) goes to stderr. By default, both these data streams are associated with the terminal.
For example, the following command will print out the string within the double quotation mark. Here, the output is stored in stdout.
The next command will show us the exit status of the previously-run command.
As the previous command ran successfully, the exit status is 0. Otherwise, the exit status will be different. What happens when you try to run an invalid command?
$ echo $?
Now, we need to know about the file descriptor. In the UNIX ecosystem, these are integer values assigned to a file. Both stdout (file descriptor = 1) and stderr (file descriptor = 2) have a specific file descriptor. Using the file descriptor (1 and 2 in this situation), we can redirect the stdout and stderr to other files.
For starter, the following example will redirect the stdout of the echo command to a text file. Here, we didn’t specify the file descriptor. If not specified, bash will use stdout by default.
The following command will redirect the stderr to a text file.
Using /dev/null
Redirecting output to /dev/null
Now, we’re ready to learn how to use /dev/null. First, let’s check out how to filter normal output and error. In the following command, grep will try to search for a string (hello, in this case) in the “/sys” directory.
However, it will generate a lot of error as without root privilege, grep can’t access a number of files. In such a case, it’ll result in “Permission denied” errors. Now, using the redirection, we can get a clearer output.
The output looks much better, right? Nothing! In this case, grep doesn’t have access to a lot of files and those that are accessible doesn’t have the string “hello”.
In the following example, we’ll be pinging Google.
However, we don’t want to see all those successful ping results. Instead, we only want to focus on the errors when ping couldn’t reach Google. How do we do that?
Here, the contents of stdout are dumped to /dev/null, leaving only the errors.
Redirect all output to /dev/null
In certain situations, the output may not be useful at all. Using redirection, we can dump all the output into the void.
Let’s break this command a little bit. First, we’re dumping all the stdout to /dev/null. Then, in the second part, we’re telling bash to send stderr to stdout. In this example, there’s nothing to output. However, if you’re confused, you can always check if the command ran successfully.
The value is 2 because the command generated a lot of errors.
If you tend to forget the file descriptor of stdout and stderr, the following command will do just fine. It’s a more generalized format of the previous command. Both stdout and stderr will be redirected to /dev/null.
Other examples
This is an interesting one. Remember the dd tool? It’s a powerful tool for converting and copying files. Learn more about dd. Using dd, we can test the sequential read speed of your disk. Of course, it’s not an accurate measurement. However, for a quick test, it’s pretty useful.
Here, I’ve used the Ubuntu 18.04.4 ISO as the big file.
Similarly, you can also test out the download speed of your internet connection.
Final thoughts
Hopefully, you have a clear understanding of what this /dev/null file is. It’s a special device that, if written to it, discards and if read from, reads null. The true potential of this interesting feature is in interesting bash scripts.
Are you interested in bash scripting? Check out the beginner’s guide to bash scripting.
Enjoy!
from Linux Hint https://ift.tt/3bcfs58
0 Comments