What Is /Dev/Null - an Introduction to the Bit Bucket
If you have used Linux for any amount of time, you most likely have come across the bit bucket, normally written as /dev/null. Understanding this special file, how it works, and it's importance will become a invaluable tool. In this article, we will discuss what /dev/null is, how to use it, and why it earned the name bit bucket. We will also explore some examples of how it can be used in common practice.
Why the Name Bit Bucket?
Early computers used punch cards to store data and programming code. When the machine punched the holes out, the unused material would drop into a bucket. Over time the bit bucket became a general term for a place where useless bits were discarded.
Other names for /dev/null are black hole, null route, punch bucket, null device, or just null.
The /dev/null File
The /dev/null file is a pseudo-device file generated at boot. It has no size (0 bytes), takes up 0 blocks on disk, has read/write permissions for all users. The screenshot below shows that the file and last system reboot have the same date and time.
This is a special file known as a character device file. This allows the file to act like a device (null device) that is unbuffered and can accept streams of data. When you access a device file, you are communicating with a driver. In the case of /dev/null, it acts as a pseudo device with a driver that has a very specific purpose. It discards anything you write to it, and only returns a EOF character when read. Sending an EOF more or less means making the process aware that no additional input will be sent (End of File).
Using the /dev/null File
Suppressing Output with /dev/null
The /dev/null file is most often used to suppress output. In the following example, we use the ">" redirection operator to send the output of the stat command to the bit bucket.
$ stat /etc/passwd > /dev/null
This effectively suppresses the commands output (standard output).
We can use /dev/null to suppress errors by redirecting standard error (STDERR), like so:
stat /etc/passwdss 2> /dev/null
We changed the filename to passwdss, which doesn't exist. This would normally show a "No such file or directory" error. Although, the error was discarded since we redirected STDERR (standard error) output to /dev/null.
NOTE: To learn more about redirection and standard data streams (STDIN, STDOUT, STDERR) read "Linux I/O, Standard Streams, and Redirection".
NOTE: Data sent to /dev/null is immediately discarded and not recoverable.
Using /dev/null to input EOF (End of File)
Another common use for /dev/null is supplying a blank or null input that has nothing but the EOF character. It may not be immediately clear why you would need such a function. However, there are a lot of utilities that need a EOF character in order to proceed. For example, the mailx utility will allow you to type an email from the command line. It will continue to wait for input until it receives an EOF character, which is sent by hitting CTRL + D. This tells the utility that you do not intended to send any more input.
NOTE: EOT (End of transmission) and EOF (End of File) have technical differences which comes down to encoding and are outside the scope of this tutorial.
What if we wanted to send a blank email, we would have to enter interactive mode and hit CTRL + D with no input. This would be unpractical if being used in a script or other unattended fashion. As a workaround we can redirect /dev/null into the mailx command to do this for us.
$ mailx -s "Just Another Email" [email protected] < /dev/null Null message body; hope that's ok
Using /dev/null to Empty a File
Yet another common use for the /dev/null file is to empty contents of a file. If you use the ">" redirection operator to redirect /dev/null into a file, it effectively erases the contents of that file.
[[email protected] ~]$ cat scp.log Changing to backup directory… Sending SCP request to download fw-backup-2019_11_10… fw-backup-2019_11_10-03_03_27-2.0.32.zip 100% 536MB 2.8MB/s 03:14 [[email protected] ~]$ [[email protected] ~]$ cat /dev/null > scp.log [[email protected] ~]$ [[email protected] ~]$ ls -l scp.log -rw-r--r--. 1 savona savona 0 Nov 11 21:32 scp.log [[email protected] ~]$ cat scp.log [[email protected] ~]$
Another option is to use the cp command to copy /dev/null over the file, like so:
[[email protected] ~]$ cat scp.log Changing to backup directory… Sending SCP request to download fw-backup-2019_11_10… fw-backup-2019_11_10-03_03_27-2.0.32.zip 100% 536MB 2.8MB/s 03:14 [[email protected] ~]$ [[email protected] ~]$ cp /dev/null scp.log [[email protected] ~]$ [[email protected] ~]$ cat scp.log [[email protected] ~]$ ls -l scp.log -rw-r--r--. 1 savona savona 0 Nov 11 21:34 scp.log [[email protected] ~]$
The bit bucket (/dev/null) is an important and useful tool for system administrators, devops engineers and just about anyone using the command line. In this article we discussed some of the most common uses for /dev/null, but once you know what it does, you may find more and more ways to use it.
We would love to hear any of your experiences, questions or comments below!
Resource and Links:
This site uses Akismet to reduce spam. Learn how your comment data is processed.
Nice write up. This answered a few questions I’ve always had.
Great article! THANKS!
Nice try, but not good enough. You see, in many languages, like Russian 'null' and 'zero' have the same meaning, zero. This is a source of confusion, of course.
Yet in Linux kernel /dev/null and /dev/zero do completely different things. Can you explain how and why ?
Why same meaning-word was chosen for them ?
Thanks for your input. I do not speak Russian so I was unaware of the confusion. I will try to do a write up about /dev/zero next and explain the differences. Thank you!
Join Our Newsletter
- Bash Scripting (17)
- Basic Commands (50)
- Featured (7)
- Just for Fun (5)
- Linux Quick Tips (98)
- Linux Tutorials (60)
- Miscellaneous (15)
- Network Tools (6)
- Reviews (2)
- Security (32)
Leave a Reply