Black hole in Linux - /dev/zero, /dev/null, Creation, Differences, Use cases

Linux provides two psuedo/special files which are very useful for developers.
1. /dev/zero
2. /dev/null

/dev/zero: Used by developers to create a file with no meaningful data but a particular size
/dev/null: Used by developers for redirecting unwanted output/error etc to this file. Useful for creating files with zero size.

Both /dev/zero and /dev/null acts as data sink, whereas /dev/zero is also data source.


How they are created

/dev/null and /dev/zero is created by the following command:

  mknod -m 666 /dev/null c 1 3
  mknod -m 666 /dev/zero c 1 5
  chown root:root /dev/null /dev/zero

Differences between /dev/zero and /dev/null

/dev/null
/dev/zero
Read
Returns End of file (read returns 0)
Returns endless bytes of zeroes (\0 characters)
Write
Data written is discarded
Data written is discarded


Use cases of /dev/zero and /dev/null

1. Suppressing stdout : $ cat file > /dev/null or $ cat file > /dev/zero
2. Deleting the contents of the file : $ cat /dev/null > /tmp/file
3. Destroying existing data on the file system partition : $ dd if=/dev/zero of=/dev/<destination partition>
4. Creating a 1 MB file: $ dd if=/dev/zero of=/tmp/file count=1024 bs=1024



Comments

Popular posts from this blog

bb.utils.contains yocto

make config vs oldconfig vs defconfig vs menuconfig vs savedefconfig

PR, PN and PV Variable in Yocto