linux - What does "/dev/null" mean at the end of shell commands - Stack Overflow
Purpose of /dev/null
Does "/dev/null" always exist ?
ELI5: How does /dev/null work?
What is the difference between `> /dev/null` and `2> /dev/null`?
Are /dev/zero and /dev/null the same?
Is /dev/null a real file?
2> means "redirect standard-error" to the given file.
/dev/null is the null file. Anything written to it is discarded.
Together they mean "throw away any error messages".
As mentioned in I/O Redirection:
- 1 is the file descriptor for standard output
- 2 is the file descriptor for standard err.
Then sometimes you find 2>&1: that means redirecting stderr to stdout.
Could someone explain what exactly the purpose of /dev/null is? Wouldn't regularly deleting something be safer and quicker?
No, the easiest way would be to not write it anywhere at all. The data doesn't have to go somewhere before being discarded – it can be discarded before it goes there.
In general, when you read or write a file, you're not directly accessing a disk, you're just asking the OS kernel to read or write a file, so the kernel has authority on deciding whether that file is going to be stored on disk or not. With files like /dev/null, there is no reason for writes to end up on actual storage – if the kernel recognizes that you're writing to the special "null" device, it can just discard them long before that happens.
There are several ways this could be implemented:
You can have reserved file names that are recognized by OS.
This is almost never done in Linux, but it's how the equivalent
NULfile works in Windows and MS-DOS. Whenever a program in Windows tries to write to a file named "nul", those writes are discarded before they even reach the filesystem driver, much less the actual disk.You can have special file attributes that activate specific handling.
On Linux (and other Unix systems), most items in /dev have their own distinct type – they are not regular files but device files, one of several types1 of Unix special files. They do exist on disk, but only as a name – there's never any actual storage allocated to them; when a device file is opened, the kernel uses custom handlers for read/write operations (instead of letting the filesystem do it).
If you run
ls -l /dev, you will see the first column showingborcas the item type ("block" or "character" subtypes), and the size column being replaced by a pair of "major, minor" values that tell the kernel how to handle access to that device. Themknodcommand can be used to create such objects.For example, in Linux, /dev/null is listed as having major number 1, which is handled by drivers/char/mem.c. Within that file, minor number 3 is handled through the
null_fopstable. In this structure, the "write" handler is set towrite_null, which does... literally nothing.All other /dev nodes have their special behavior implemented in the same way. For example,
/dev/ttyS0is associated with "fops" functions that read from a physical serial port. (In the Linux source, look for "register_chardev" and "misc_register" functions; in BSD sources, look for a giant "cdevsw" list that enumerates each major type.)1 There are also more special file types – symbolic links (
ln -s); named pipes (mkfifo); sockets; doors (Solaris); whiteouts (NetBSD) – all of which are handled specially and don't occupy any disk storage.You can also have whole filesystems that do something different than storing files on disk.
Typically you can mount different disks at different filesystem paths (e.g.
/orC:\is your SSD,/dataorD:\is your HDD,/media/sandiskorE:\is your USB stick), with the OS associating each location with a specific filesystem driver and a specific physical device.However, the OS may also support virtual filesystem types that don't use a physical device.
For example, on Linux, the entire
/devis usually mounted from a tmpfs filesystem, which behaves like a normal filesystem but uses only RAM as its storage. Even if you create completely regular files in /dev and start writing data to them, it will be stored in RAM only – not on any disk. Sometimes/tmpis a tmpfs, too.(In the past, a similar result was achieved by mounting a regular filesystem but giving it a "RAM disk" – i.e. just a dedicated region of RAM – as the storage device. You'll still find ramdisk software for Windows, which has no tmpfs equivalent.)
The
/procand/sysdirectories also have special filesystems mounted on them, so the entire contents of /proc are entirely virtual. (Because of this, even though they look like regular files and not device files, they still show all kinds of irregular behavior.)
The data is not stored and discarded, it's just discarded.
pg@TREX:~$ ls -l /dev/null
crw-rw-rw- 1 root root 1, 3 Sep 18 08:09 /dev/null
As you can see /dev/null is a character device. Data streams there unbuffered, character by character and gets immediately discarded, character by character. It contains nothing, so if you try to read from it, the only thing it gives back is EOF => cat /dev/null > filename creates a zero-byte file.
As the data never gets stored, using /dev/null doesn't degrade your drive.
Some articles about or touching /dev/null:
- https://www.networkworld.com/article/3025497/sending-data-into-the-void-with-dev-null.html
- https://tldp.org/LDP/abs/html/zeros.html
- http://ibgwww.colorado.edu/~lessem/psyc5112/usail/concepts/filesystems/everything-is-a-file.html