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
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Steady State Response In this article, we are going to discuss the steady-state response. We will see what is steady state response in Time domain analysis. We will then discuss some of the standard test signals used in finding the response of a response. We also discuss the first-order response for different signals. We
9 min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read
Polymorphism in Java Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca
7 min read
Decorators in Python In Python, decorators are a powerful and flexible way to modify or extend the behavior of functions or methods, without changing their actual code. A decorator is essentially a function that takes another function as an argument and returns a new function with enhanced functionality. Decorators are
10 min read
3-Phase Inverter An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power
13 min read
What is Vacuum Circuit Breaker? A vacuum circuit breaker is a type of breaker that utilizes a vacuum as the medium to extinguish electrical arcs. Within this circuit breaker, there is a vacuum interrupter that houses the stationary and mobile contacts in a permanently sealed enclosure. When the contacts are separated in a high vac
13 min read
Linux Commands Cheat Sheet Linux, often associated with being a complex operating system primarily used by developers, may not necessarily fit that description entirely. While it can initially appear challenging for beginners, once you immerse yourself in the Linux world, you may find it difficult to return to your previous W
13 min read