This topic was published by DevynCJohnson and viewed 824 times since "". The last page revision was "".
- Topics - 444
"/dev/null" is one of the most commonly known files on Linux and Unixoid systems. This file has many special purposes and uses. /dev/null is not like traditional files. There is actually a lot that can be learned about this single file.
/dev/null is the most commonly known /dev/ device file. Obviously, this is a file named "null" that is in the "/dev/" directory. The "/dev/" directory is a mountpoint for the devfs (Device Filesystem) pseudo-filesystem. devfs is a device manager in the form of a filesystem that displays each device as files. This pseudo/virtual filesystem is not on the hard-drive. Rather, it is in memory. The "files" are generated by the kernel to provide software and processes (including userland programs and scripts) access to devices. In other words, the /dev/ directory is an interface to devices.
FUN FACT: /dev/null is equivalent to the "NULL" file call used by Windows (\Device\Null), OpenVMS (NL:), Amiga (NIL:), and DOS and CP/M (NUL: or NUL).
Since /dev/null is a generated fake file, does it have a file size and what are some of its file properties? Running "stat /dev/null", "file /dev/null", and "ll /dev/null" ('ll' = 'ls -l' on most systems) yield answers to such questions. As many Linux users will see, the file has no size. The creation date is the same as the system's boot-up time (given by "who -b"). The file's User ID (UID) and Group ID (GID) is zero. It is also interesting to notice that the file has read and write permissions for all users. This is needed since many programs may use /dev/null. However, executable permissions are not needed because this is not a file that can be executed.
/dev/null is a special type file known as a character device file. This means that the file acts like a device that is unbuffered and can accept streams of data. Any data written to /dev/null is discarded. However, the write operation will return successful. Despite all the data that is sent to /dev/null, it cannot fill-up, get full, or increase in size. /dev/null behaves like a file, not a directory. As a result, commands like "mv" cannot be used to remove directories or dump directories into /dev/null.
NOTE: To delete files and directories, use the commands "rm" and "rmdir", respectively.
When read by the command "cat", nothing is seen in the output. However, something is returned. When read, the /dev/null will return the end-of-line character (EOF) as required/stated by the POSIX (IEEE 1003.1) standard. However, when read by "less", users will get the error message "/dev/null is not a regular file" because "less" has a special way of opening and displaying files that does not work with /dev/null.
NOTE: To prove that /dev/null returns an end-of-line character, run [CODE]echo "`cat /dev/null`"[/CODE] in a Bourne-compatible shell.
When piping data to /dev/null (date | /dev/null), BASH users will get the error "bash: /dev/null: Permission denied", DASH users will see "dash: 1: /dev/null: Permission denied", and CSH users will get "/dev/null: Permission denied". This is because /dev/null is not an executable that can accept pipes.
To properly send data to /dev/null, use redirections such as ">". For instance, "date > /dev/null" will write the "date" command's output to /dev/null as if it were a file. No output will be seen; that data is permanently gone. Any data that is written to /dev/null is not recoverable. When using /dev/null, applications perceive an I/O operation, although no I/O operations occur on the hardware. When data is sent to /dev/null, the data stream is very quickly removed from existence. For instance, running the command "time (dmesg)" takes more time to execute than "time (dmesg > /dev/null)". In other words, it is faster to write to /dev/null than to print to the screen or send the data elsewhere.
/dev/null can be used to remove error messages. For example, if it is undesirable to get error messages in the output, then redirect errors to /dev/null. The syntax is "CMD 2> /dev/null" (replace “CMD” with a command). Also, to save the output (but not errors) to a file, users could use syntax like "CMD 2> /dev/null > /PATH/FILE.TXT". However, to save the good data (stdout) to a file, but allow the errors (stderr) to go to the screen, use a format like "CMD 1> /PATH/FILE.TXT". For more info on shell command redirections, check out the two redirection articles under "Further Reading".
NOTE: When writing to /dev/null, think of it as a black hole. Anything that goes into it is gone and will never return.
If users need to empty a file so that it only contains the end-of-file character (EOF), then /dev/null can be used. To do so, type "cat /dev/null > /PATH/FILE". All files must end in "EOF", so this is an effective and portable method for creating an empty/null file. However, typing "> /PATH/FILE" or ": > /PATH/FILE" would yield the same results on some shells. In addition, to create a new empty file, users could use the "touch" command instead of /dev/null.
FUN FACT: Running "tail -f /dev/null" is the best way to do nothing forever without polling or wakeups.
The source code for /dev/null (in the Linux kernel) is under "./drivers/char/mem.c" (https://github.com/torvalds/linux/blob/master/drivers/char/mem.c). This source code file contains the code for many character devices that reside on devfs.
- What are those /dev/ Files? - http://dcjtech.info/topic/what-are-those-dev-files/
- Bash Constructs - http://dcjtech.info/topic/bash-constructs/
Attachments:You must be logged in to view attached files.