One of my colleagues asked me recently: hey, what does this > sign and /dev/null mean? I saw it many times in scripts but never bothered to find out what it was.
I answered with “that’s redirection of output to special Linux object, ‘blank’ file which does not gets written to disk”.
Couple of minutes later I caught myself with a thought that I don’t know much more about /dev/null. Let’s find out.
File or device?
It is really old entity which first appeared in 1973 Version 4 Unix and is mentioned as mandatory by POSIX.
In Linux terms it is character special file, one of 2 types of device files in Linux - character and block. Character means here that data is pushed as a stream of chars to the device and from it:
1
2
|
file /dev/null
/dev/null: character special (1/3)
|
/dev/null is part of whole family of virtual devices which include random, zero etc.:
1
2
3
4
5
6
7
8
9
|
$ ls -lhG --time-style=iso /sys/dev/char/1:* | awk '{print $7" "$8" "$9}'
/sys/dev/char/1:1 -> ../../devices/virtual/mem/mem
/sys/dev/char/1:11 -> ../../devices/virtual/mem/kmsg
/sys/dev/char/1:3 -> ../../devices/virtual/mem/null
/sys/dev/char/1:4 -> ../../devices/virtual/mem/port
/sys/dev/char/1:5 -> ../../devices/virtual/mem/zero
/sys/dev/char/1:7 -> ../../devices/virtual/mem/full
/sys/dev/char/1:8 -> ../../devices/virtual/mem/random
/sys/dev/char/1:9 -> ../../devices/virtual/mem/urandom
|
How can we use it?
- send output from a program to ‘black hole’ and don’t fear for disk space / IO load:
1
|
./my_script.sh > /dev/null 2>&1
|
- get end-of-file when read from the device and provide as an input:
1
|
./myscript.sh < /dev/null
|
- wipe file without actually removing it from filesystem:
1
|
cat /dev/null > large.log
|
Links: