What is /dev/zero in Linux and its Uses

Linux TLDR
Last Updated:
Reading time: 4 minutes

In this article, you will learn what the “/dev/zero” file is and what its purpose is, but before you understand that, you must know what the special files in Linux are.

What are “Special Files” in Linux?

In Linux, a “special file” is a type of file that represents a device or a kernel object rather than a regular file or directory.

Special files are typically found in the “/dev” directory and represent devices that are attached to your system. However, these files are not intended to be read like normal files.

They are used to communicate with the device drivers and hardware, and their content represents the data stored on the device.

List of Special Files in Linux

The following is a list of some common special files in Linux:

  • /dev/null“: A special file that discards all the data forwarded to it and always returns end-of-file when read.
  • /dev/sda“: A special file that represents the SCSI (stands for “Small Computer System Interface”), basically your hard disks, USB flash drives, etc.
  • /dev/random“: A special file that provides a source of random data.
  • /dev/zero“: A special file that provides endless streams of null data (and the primary objective of this article).

So, let’s talk about the main objective of this article:

What is “/dev/zero” in Linux?

In Linux, “/dev/zero” is a special file that provides an endless stream of null (0) bytes that can be used to create files filled with zeroes.

If you are wondering what could be the reason for filling the files with zeros, let me tell you that there are various use cases for this, such as:

  • Creating a swap file.
  • Creating a dummy file for testing.
  • Formatting the drive and filling the empty space with null bytes will overwrite the data for security reasons.

Interesting, right? Now let’s see how you can use this file.

Usage of /dev/zero in Linux

In the following examples, you will learn how to use the “/dev/zero” file to perform the previously mentioned use cases. starting with

1. Creating a Swap File in Linux

Swap is the space on a disk that is used when the physical RAM memory is full. So, once the RAM is full, the rest of the processing is done in the swap space to prevent any failure.

Now, swap space can be in the form of a dedicated swap partition or a swap file. The swap partition is only available when the user has given a separate partition for swap while installing the Linux system.

If you forget to do so, then don’t worry; you can create and use a temporary 1 GB swap file by following the below steps.

📝
The swap files are slower than swap partitions and can cause performance issues. And also, the size of the swap should be equal to the size of your RAM.

1. Creating a 1 GB swap file using the dd command:

$ sudo dd if=/dev/zero of=/swapfile bs=1M count=1024

The above command will create a 1 GB file named “/swapfile” and fill it with null bytes.

2. Change the file permissions and set it as a swap file.

$ sudo chmod 600 /swapfile
$ sudo mkswap /swapfile

The above command will change the permissions to read and write only for the owner and make the file usable as a swap area.

3. Activate the swap file.

$ sudo swapon /swapfile

To make the changes persistent after the next reboot, you need to edit the “/etc/fstab” file. To do so, open the file with your choice of text editor, and then add the following line at the end of the file.

/swapfile none swap sw 0 0

4. To verify the swap is active, either use the swapon or free command.

$ sudo swapon --show

#OR

$ sudo free -h

Now, let’s see the next use case for this file.

2. Creating a Dummy File for Testing

The following command will use the dd command to create a 100 MB dummy file for testing using the “/dev/zero” file in Linux.

$ dd if=/dev/zero of=dummyfile bs=1M count=100

Whereas,

  • The stream of null bytes is used from the “/dev/zero” file.
  • The data will be written on the “dummyfile“.
  • The “bs” option represents the block size.
  • The “count” option represents the number of input blocks to be copied.

Calculation: bs (1) x count (100) = zero_file (100MB).

You can also create a file with a specific size in GB or other units by adjusting the block size and count accordingly. For example, the following command will create a 5 GB file of zero bytes.

$ dd if=/dev/zero of=dummyfile bs=1G count=5

Using these dummy files, you can perform various tests, such as testing file transfer speed, backup and restore speed, disk I/O performance, and many more.

3. How to Format a Drive with Zeros in Linux

A formatted drive, disk, or partition can be shredded using the “/dev/zero” file to overwrite all the data on it with null bytes, making file recovery nearly impossible.

If you intend to perform overwriting on any of them with zeroes, keep in mind that it may take quite a long time to complete this process, especially on devices with slow write speeds.

1. Check the list of all available drives, disks, and partitions in your system using the lsblk command.

$ lsblk

The above command will give you a list of all the drives/disks/partitions in your system. You can identify them based on their capacity.

2. Assuming “/dev/sdb1” is the disk you are interested in, first unmount it using the umount command.

$ umount /dev/sdb1

3. Lastly, execute the following command to format the disk with zero bytes:

📝
It is not recommended to format SSDs using the dd command because they have a limited number of write cycles before they fail, and this method can cause the drive to fail prematurely.
$ sudo dd if=/dev/zero of=/dev/sda bs=1M

Ignore any error messages like “No space left on device“, as that is normal to have.

So, that was the last example of this article.

I hope in this article you learned something interesting about the Linux special files.

If you have any questions or queries related to this topic, then feel free to ask them in the comment section.

Till then, peace!

Join The Conversation

Users are always welcome to leave comments about the articles, whether they are questions, comments, constructive criticism, old information, or notices of typos. Please keep in mind that all comments are moderated according to our comment policy.