What is /dev/zero in Linux?

In Linux, device files are quite special as they provide applications to interface with device drivers which also include the /dev/zero.

So in this article, I will walk you through what it is and why it is used for.

A Zero Generator of Linux

Yes, you guessed it right!

The /dev/zero is a dummy file that is used to create files filled with zeroes.

But why would you want to create a file filled with zeroes? Well, there are various use cases for nuking the file with zeros. Such as:

  • Formatting drive and filling space with zeros to override old data.
  • Creating dummy files for experiments.
  • Creating a temporary swap file.

Sounds interesting. Right?

So let's have a look at how you can use this powerful utility.

How to use /dev/zero

Here, I will be showing you two use cases of /dev/zero:

  1. Formatting disk partitions with zeroes.
  2. Creating dummy files.

So let's start with creating a dummy file filled with zeros.

How to create a dummy file in Linux

To create a dummy file, you would need to follow the given command syntax:

dd if=/dev/zero of=Filename bs=Block_Size count=sets_of_blocksize

Here,

  • of=Filename is where you can indicate the path and name of the file you want to create,
  • bs=Block_size is where you will enter the size of each block such as 1MB.
  • count=sets_of_blocksize is where you have to specify the count of the block size. such as if you want to create a file worth 1 gigabyte, you will use a 1Mb block and set the count to 1024.

For example, Here I have created a dummy file named Dummy worth of 100MB:

dd if=/dev/zero of=Dummy bs=1MB count=100

How to format drive with zeroes in Linux

⚠️
This process may take quite a long especially if you use using a drive with slow write speeds.

The first step in formatting is to list available disks in Linux. Here, I have used the lsblk command:

lsblk

Here, my target disk is sdb1 mounted at /media/sagar/Q1.

Once you find out the name of the drive you want to work with, you will have to unmount that partition.

This can easily be done using the umount command:

umount TargetDrive

For me, its sdb1:

umount /dev/sdb1

Now, all you need to do is use the following command to format the disk:

sudo dd if=/dev/zero of=TargetDrive bs=1M

Ignore the error message as you are wiping the disk from the beginning.

And as you can see, the drive has been formatted successfully!

🚧
It is not advised to use the `dd` command to create a file using the `/dev/zero` special file on SSDs. This is to prevent write amplification which negatively impacts the endurance of an SSD.

Wrapping Up

I would recommend using /dev/random instead of /dev/zero if you want to format the disk and make it a lot harder to recover old data.

This was a little explanatory guide on how you can use the /dev/zero. I hope you will find this helpful.

And if you have any queries, let me know in the comments.