Shell Scripting - Difference between /dev/null and /dev/zero
Last Updated :
07 Nov, 2022
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.
Similar Reads
Shell Scripting - Difference between Korn Shell and Bash shell Korn Shell: Korn Shell or KSH was developed by a person named David Korn, which attempts to integrate the features of other shells like C shell, Bourne Shell, etc. Korn Shell allows developers to generate and create new shell commands whenever it is required. Korn shell was developed a long year bac
3 min read
Difference between Terminal, Console, Shell, and Command Line Terminal, Console, Shell, and Command line all are ways to give the command to the computer but all these have different functions. A terminal is a text-based interface that is used to type commands or take input and view the output. A console is a type of terminal that is used to interact with oper
5 min read
Difference between Thompson Shell and POSIX Shell The Thompson Shell and the POSIX Shell are Unix command-line interpreters, however, they are different in their uses and were created at different time of Unix advancement. The Thompson Shell is one of the first Unix shells developed in the first half of the 1970 s and provided basic functionalities
5 min read
Difference between File Descriptor and File Pointer File descriptor is simply an index into the file descriptor table. For each process in our operating system, there is a process control block(PCB). PCB keeps track of the context of the process. So one of the fields within this is an array called file descriptor table. This array keeps track of all
3 min read
Bash Script - Difference between Bash Script and Shell Script In computer programming, a script is defined as a sequence of instructions that is executed by another program. A shell is a command-line interpreter of Linux which provides an interface between the user and the kernel system and executes a sequence of instructions called commands. A shell is capabl
4 min read