All we need is an easy explanation of the problem, so here it is.
I always thought of
/dev/null as a black hole, in the way that all data I send there will not be echoed back. However there’s a problem with that definition. According to Wikipedia:
/dev/null is a special file, not a
directory, so one cannot move files
into it with the Unix mv command.
So following this definition, why can I
mv stuff there?
For example, I can easily do:
# touch oi # mv oi /dev/null
What’s going on there?
How to solve :
I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.
If you’ll try to move anything to /dev/null under a plain user (not root), you will get a
Permission denied error because
mv something /dev/null is actually trying to delete
/dev/null and then move
something to what
If you will try to do it under root, it will delete
/dev/null (and then move the file provided by you in it’s place)! You can restore it by rebooting or typing the following in a root shell (on Linux):
mknod /dev/null c 1 3; chmod a+w /dev/null or in BSD:
mknod /dev/null c 3 2; chmod a+w /dev/null.
When you do this:
# mv oi /dev/null
You are actually doing the equivalent of the following
# rm /dev/null # mv oi /dev/null
# uname -sr Linux 2.6.32-ARCH # touch a # mv a /dev/null mv: inter-device move failed: `a' to `/dev/null'; unable to remove target: Permission denied
/dev/null is a character special file, which means that it is a contentless file that is marked as being able to source and sink byte streams to it. What happens to those streams is determined by their device numbers. On my BSD system at the moment:
crw-rw-rw- 1 root wheel 3, 2 Jan 25 14:57 /dev/null
the device numbers are 3,2 (the major number is 3, minor number is 2). I can create other
/dev/null like files using
mknod /dev/nil c 3 2, where the c means I want a character special file, but on BSD systems, the device number pair is dynamically created and might not be the same next boot.
To see how these facts apply in practice:
sh-2.05b# mknod /dev/nil c 3 2 sh-2.05b# ls -l /dev/nil crw-r--r-- 1 root wheel 3, 2 Jan 25 15:14 /dev/nil sh-2.05b# echo abc > /dev/nil sh-2.05b# cat < /dev/nil sh-2.05b#
Linux uses the major-minor pair 1,3, so
mknod /dev/null c 1 3 will recreate your file there.
Character special files are like most other files in many respects, and can be deleted if you have the permissions. Using
mv on a device file is just the same as if you moved a regular file, which will mean that you get a completely new file, which you can tell by looking at the inodes. For example (Mac OS this time, but all *nixes will behave the same here):
helen:tmp cas$ touch abc def helen:tmp cas$ ls -i1 3775141 abc 3775142 def helen:tmp cas$ mv abc def helen:tmp cas$ ls -i1 3775141 def helen:tmp cas$ echo clobber > def helen:tmp cas$ ls -i1 3775141 def
See how the inode associated with the filename
def changed after I used
mv, with the old file simply being deleted, but when I wrote the output of
echo to the file named
def, the inode remained the same. So your
mv will either fail, likely if you are not root, or delete the file and put a new file under path
/dev/null, which is not what this character special is for.
mv a file to
/dev/null (even if it’s possible) when you could simply delete it?
/dev/null is more useful and is intended as a destination for output data?
/dev/null is like a black hole and/or an empty file. terrific! You can put anything in here and get nothing from there.
Shell Scripting is a common use:
cat somefile >/dev/null 2>&1
Sometimes you don’t want to see/show errors etc.
Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂