Wednesday, April 17, 2019

What are /dev/zero and /dev/null files ?

"/dev/zero" and "/dev/null" are two dummy devices files which are useful for creating empty files.


"/dev/zero": 

It is used to create a file with no data but with required size(A file with all zero’s written on it).

Let's create file with /dev/zero

[root@localhost ~]# dd if=/dev/zero of=/opt/zero.txt bs=2048 count=2048
2048+0 records in
2048+0 records out
4194304 bytes (4.2 MB) copied, 0.150465 s, 27.9 MB/s

it creates a file that has continuous zeros in it. So we can say that /dev/zero is a file which is used to create a new file with some required size without any meaning to the data.


"/dev/null":

This file is useful in cases like redirecting unwanted output/error etc to this file. Actually it acts as a black hole which absorbs this unwanted input. 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.

e.g.

If you see i have few files and folders in current directory:

[root@localhost ~]# ls
anaconda-ks.cfg  Documents  initial-setup-ks.cfg  Pictures  Templates
Desktop          Downloads  Music                 Public    Videos

If i will redirect "ls" output to "/dev/null" file, the output data can't be retrive again even after reading null file:

[root@localhost ~]# ls > /dev/null

[root@localhost ~]# cat /dev/null
[root@localhost ~]#

No comments:

Post a Comment