Open In App

Shell Scripting – Difference between /dev/null and /dev/zero

Last Updated : 07 Nov, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Device files/nodes are special files that allow applications to interface with device drivers. I/O operations on device nodes are handled by the driver for that device. Device nodes are the standard mechanism for accessing hardware on Unix systems.

Several device nodes are present on all Linux systems. Some of these device nodes are special – for example, NULL device /dev/null, ZERO device /dev/zero, etc.

/dev/null /dev/zero
The null device lives at /dev/null. The zero device lives at /dev/zero.
/dev/null has a major number of 1 and a minor number of 3. /dev/zero has a major number of 1 and a minor number of 5.
/dev/null is owned by root. /dev/zero is owned by root.
/dev/null is readable and writable by all users. /dev/zero is readable and writable by all users.
All write requests to /dev/null are discarded. All write requests to /dev/zero are discarded.
All read requests to /dev/null return end-of-file (EOF). Reading from /dev/zero returns an infinite stream of null bytes.

Usage:

/dev/null

This file is like a black-hole, whatever is written to this file vanishes, and reading from the file outputs nothing. So the use-case for this file is – when we want to eliminate the standard output or error of a shell command, we can redirect it to this file.

Example usages:

1. Eliminate unwanted standard output:

cat unwanted_file >/dev/null

 

2. Eliminate unnecessary error prints:

rm file1 2>/dev/null

 

3. Eliminate both standard output and errors:

cat file.txt >/dev/null 2>/dev/null

If the file exists, then it will be read but the output will be eliminated and if the file does not exist or any other error scenario, the error will be eliminated.

 

4. /dev/null can also be used to clear a file’s contents

cat /dev/null > some_file

This will clear the file’s contents, but will not delete the file.

 

Though the examples here use file operation commands to demonstrate the usage of /dev/null, it can be used with any command while working with command line or shell scripting.

/dev/zero

This file provides an endless stream of zeros upon reading and anything written to the file vanishes. 
So the use-case for this file is – when we want zero-filled memory or file this file can be used.

Example usages:

1. Get a zero-filled file:

dd bs=10 count=10 if=/dev/zero of=file

This will create a file of size 100B filled with null characters.

 

2. Get zero-filled memory:

/dev/zero can be mapped to obtain zero-filled memory.

void *p;

int fd;

fd = open(“/dev/zero”, O_RDWR);

p = mmap(NULL, getpagesize(), PROT_READ|PRO_WRITE, MAP_PRIVATE, fd, 0);

close(fd);

Pointer “p” points to a page of memory filled with zeroes.
Such usage of /dev/zero is also an alternative approach to get anonymous mapping using mmap(). An anonymous mapping is one that doesn’t have a corresponding file. Another approach to get anonymous mapping is to specify MAP_ANONYMOUS as a flags argument and specify fd as -1.

3. Creating a swap space:

A regular file can be used as a swap space. Create an empty file, initialize it as a swap and add it to the swap pool:

dd if=/dev/zero of-swap_file bs=1024k count=num_mb

mkswap swap_file

swapon swap_file

swap_file is the name of the new swap file, and num_mb is the desired size in megabytes. 


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads