/dev/zero and /dev/null are two pseudo files which are useful for creating empty files. Many people consider there is no difference or puzzled with what could be the difference between two files. There is considerable difference when writing data using these hardware files.
/dev/zero: This file is used to create a file with no data but with required size(A file with all zero’s). In other words this will create a data file with all zeros in the file which will give the size to a file.
Create a file with /dev/zero file
dd if=/dev/zero of=/opt/abc.txt bs=4096 count=1000
We can see what /dev/zero file writes to a file using below strace command.
root@linuxnix.com:/dev# strace cat /dev/zero
execve(“/bin/cat”, [“cat”, “/dev/zero”], [/* 46 vars */]) = 0
<—->
fadvise64(3, 0, 0, POSIX_FADV_SEQUENTIAL) = 0
read(3, “”…, 32768) = 32768
write(1, “”…, 32768) = 32768
read(3, “”…, 32768) = 32768
write(1, “”…, 32768) = 32768
read(3, “”…, 32768) = 32768
write(1, “”…, 32768) = 32768
read(3, “”…, 32768) = 32768
write(1, “”…, 32768) = 32768
read(3, “”…, 32768) = 32768
write(1, “”…, 32768) = 32768
read(3, “”…, 32768) = 32768
write(1, “”…, 32768) = 32768
read(3, “”…, 32768) = 32768
If you see the strace command on /dev/zero, it’s continuous zeros which it will write to a file.
So we conclude that /dev/zero is a file use full for creating a file with some required size without any meaning to the data.
/dev/null: This is one more Pseudo file which is useful in many places like redirecting unwanted output/error etc to this file. This file acts as a black hole(Which eat up everything and do not show any output). So whenever you feed some data to this file, you can not retrieve the data which is fed to it. This file even useful for creating files with zero size.
Example:
surendra@linuxnix.com:/etc/init.d$ dd if=/dev/null of=/home/surendra/abc.read bs=512 count=10
0+0 records in
0+0 records out
0 bytes (0 B) copied, 4.506e-05 s, 0.0 kB/s
surendra@linuxnix.com:/etc/init.d$ du -hs /home/surendra/abc.read
0 /home/surendra/abc.read
surendra@linuxnix.com:/etc/init.d$ dd if=/dev/zero of=/home/surendra/abc.read bs=512 count=10
10+0 records in
10+0 records out
5120 bytes (5.1 kB) copied, 0.000392949 s, 13.0 MB/s
surendra@linuxnix.com:/etc/init.d$ du -hs /home/surendra/abc.read
8.0K /home/surendra/abc.read
Now you can see the difference between /dev/null and /dev/zero.
Latest posts by Surendra Anne (see all)
- Docker: How to copy files to/from docker container - June 30, 2020
- Anisble: ERROR! unexpected parameter type in action:
Fix - June 29, 2020 - FREE: JOIN OUR DEVOPS TELEGRAM GROUPS - August 2, 2019
- Review: Whizlabs Practice Tests for AWS Certified Solutions Architect Professional (CSAP) - August 27, 2018
- How to use ohai/chef-shell to get node attributes - July 19, 2018