Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Shell Scripting
  • Kali Linux
  • Ubuntu
  • Red Hat
  • CentOS
  • Docker in Linux
  • Kubernetes in Linux
  • Linux interview question
  • Python
  • R
  • Java
  • C
  • C++
  • JavaScript
  • DSA
Open In App
Next Article:
What is /Dev/Null in Linux?
Next article icon

What is /Dev/Null in Linux?

Last Updated : 31 Jul, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

If you have been learning shell programming, you may already have come across something like /dev/null. In this article, we will understand what it is and how it is used. Let's start off by understanding what /dev is.

What is /dev?

In the Linux file system, everything is a file or a directory. Even devices are accessed as files. Your hard drive partitions, Pen drive, speakers, for all of these, there exists a file from which these are accessed. Now to understand how devices are accessed as files, think of it in this way: what do we do with a file? We read data from it and write data to it. A device like a speaker can input data to produce sound, a hard disk can be used to read and write data, a printer takes the input to print files, etc. Files and devices are similar in this way

/dev is a directory that stores all the physical and virtual devices of a Linux system. Physical devices are easy to understand, they are tangible devices like pen-drive, speakers, printers, etc. A Linux system also has virtual devices which act as a device but represent no physical device. 

What is /dev/null?

It is a virtual device, which has a special property: Any data written to /dev/null vanishes or disappears. Because of this characteristic, it is also called bitbucket or blackhole. Let us see a demonstration.

Redirect output to /dev/null

 

In this demo, you can see that we added some data, but that data was not stored. This particular characteristic of /dev/null has some use cases. Let's see them.

Usage of /dev/null

Since it discards anything written to it, you can move files to /dev/null to delete them. But this is not a widely used use case. It is mainly used to discard standard output and standard error from an output.

What are standard output and standard error?

The output of a Linux command contains two streams of data: standard output (stdout) and standard error (stderr0), while the standard output is the normal output if the command has no errors, the standard error is the error generated by the command. Output may contain both standard output and standard input. Example of stdout and stderr:

Let's run the following shell script

# /bin/sh
ls
apt update
 

The first command ran successfully, but the second one generated error. Since stdout and stderr are two separate data streams, they can be handled separately.

How to access stderr and stdout?

Stdout and stderr are streams of data, both of which are treated as a file in Linux. If you wish to perform any action on these, you use a file descriptor that uniquely identifies the file stream. The file descriptor for stdout is 1 and for stderr is 2.

To access them separately, let's see the following command which is performed on the same script file which was used in the previous example.

./script.sh 1>stdout.txt
 

In this, you can see that the output contains standard errors but the standard output was not displayed. This is because the stdout stream was sent to the stdout.txt file, and you can see that the stdout.txt file contains the standard output.

While writing a shell script, we may want to discard the standard error from the output. Whatever stream we may want to suppress from the output, that stream of data can be written into /dev/null. You could write that data into another file, just like the above example, but if you have no use for that data, why would you want to waste memory on it, it's better to discard it completely. Let's see the following example

./script.sh 2>/dev/null
 

And just like that, we have removed the stderr from the output. If you wish to discard the complete output, you can use the following command

command >/dev/null 2>&1

The &> command redirects the output of the file descriptor which is mentioned on the left (in the above command, the output of 2) to the stream of file descriptor mentioned on the right-hand side. So, the output of stderr (2) gets redirected to stdout(1), which in turn gets written in /dev/null and thus gets destroyed.

 

Secure File Deletion

'/dev/null' finds utility in secure file deletion. By redirecting '/dev/null' to a file, you can effectively overwrite its content with null bytes, making it challenging to recover any meaningful data.

For example:

cat /dev/null > sensitive_file

This command replaces the contents of 'sensitive_file' with null bytes, ensuring its secure deletion.

Cleaning Log Files

Log files can accumulate substantial data over time. To clear log files without interrupting running services, you can redirect their content to '/dev/null.'

For example:

cat /dev/null > /var/log/syslog

This command empties the 'syslog' file by redirecting its content to '/dev/null,' effectively cleaning the log without deleting the file itself.

Tips and Tricks:

  • Redirecting standard output to '/dev/null': 'command > /dev/null'
  • Redirecting standard error to '/dev/null': 'command 2> /dev/null'
  • Redirecting both output and error to '/dev/null': 'command >/dev/null 2>&1'

Conclusion

In this article we discussed `/dev/null' in Linux is a virtual black hole where data disappears when written to it. It is commonly used to discard output and errors, making command execution cleaner. It can also be employed for secure file deletion and log file cleaning. Understanding its functionality allows for efficient command-line operations in Linux.


Next Article
What is /Dev/Null in Linux?

J

jivendrasah
Improve
Article Tags :
  • Linux-Unix

Similar Reads

    What Is Linux
    The Linux ope­rating system is a collection of open-source­ software programs designed to function similarly to Unix syste­ms. Linus Torvalds, a Finnish software enginee­r, initially launched the Linux kerne­l, which serves as the core­ component, on Septembe­r 17, 1991. This kernel acts as a vital
    12 min read
    What is Open API in UNIX?
    There is a set of generic APIs in UNIX which is used to manipulate files. One of these APIs is the open API. The open API is used to create new files and also to establish a connection between a process and a file. After a file is created, any process can call the open function and it gets a file de
    5 min read
    File Management in Linux
    In Linux, most of the operations are performed on files. And to handle these files Linux has directories also known as folders which are maintained in a tree-like structure. Though, these directories are also a type of file themselves. Linux has 3 types of files: Regular Files: It is the common file
    4 min read
    Device Drivers in Linux
    Drivers are used to help the hardware devices interact with the operating system. In windows, all the devices and drivers are grouped together in a single console called device manager. In Linux, even the hardware devices are treated like ordinary files, which makes it easier for the software to int
    3 min read
    Swapon Command in Linux
    The Linux swapon command serves the purpose of activating a swap partition in Linux systems. A swap partition is essentially a dedicated space used when the system's RAM (Random Access Memory) becomes fully utilized. It temporarily holds data that can be retrieved when required, making it particular
    5 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences