Think of /dev/null as a "black hole". It is the nearest equivalent to a write-only file. Everything written to it disappears forever. Attempts to read or output from it result in nothing. Nevertheless, /dev/null can be quite useful from both the command line and in scripts.
Suppressing stdout or stderr (from Example 12-2):
1 rm $badname 2>/dev/null 2 # So error messages [stderr] deep-sixed. |
Deleting contents of a file, but preserving the file itself, with all attendant permissions (from Example 2-1 and Example 2-2):
1 cat /dev/null > /var/log/messages 2 # : > /var/log/messages has same effect, but does not spawn a new process. 3 4 cat /dev/null > /var/log/wtmp |
Automatically emptying the contents of a logfile (especially good for dealing with those nasty "cookies" sent by Web commercial sites):
Like /dev/null, /dev/zero is a pseudo file, but it actually contains nulls (numerical zeros, not the ASCII kind). Output written to it disappears, and it is fairly difficult to actually read the nulls in /dev/zero, though it can be done with od or a hex editor. The chief use for /dev/zero is in creating an initialized dummy file of specified length intended as a temporary swap file.
Example 29-2. Setting up a swapfile using /dev/zero
1 #!/bin/bash 2 3 # Creating a swapfile. 4 # This script must be run as root. 5 6 ROOT_UID=0 # Root has $UID 0. 7 E_WRONG_USER=65 # Not root? 8 9 FILE=/swap 10 BLOCKSIZE=1024 11 MINBLOCKS=40 12 SUCCESS=0 13 14 if [ "$UID" -ne "$ROOT_UID" ] 15 then 16 echo; echo "You must be root to run this script."; echo 17 exit $E_WRONG_USER 18 fi 19 20 21 if [ -n "$1" ] 22 then 23 blocks=$1 24 else 25 blocks=$MINBLOCKS # Set to default of 40 blocks 26 fi # if nothing specified on command line. 27 28 if [ "$blocks" -lt $MINBLOCKS ] 29 then 30 blocks=$MINBLOCKS # Must be at least 40 blocks long. 31 fi 32 33 34 echo "Creating swap file of size $blocks blocks (KB)." 35 dd if=/dev/zero of=$FILE bs=$BLOCKSIZE count=$blocks # Zero out file. 36 37 mkswap $FILE $blocks # Designate it a swap file. 38 swapon $FILE # Activate swap file. 39 40 echo "Swap file created and activated." 41 42 exit $SUCCESS |
Another application of /dev/zero is to "zero out" a file of a designated size for a special purpose, such as mounting a filesystem on a loopback device (see Example 13-6) or securely deleting a file (see Example 12-31).
Example 29-3. Creating a ramdisk
1 #!/bin/bash 2 # ramdisk.sh 3 4 # A "ramdisk" is a segment of system RAM memory 5 #+ that acts as if it were a filesystem. 6 # Its advantage is very fast access (read/write time). 7 # Disadvantages: volatility, loss of data on reboot or powerdown. 8 # less RAM available to system. 9 # 10 # What good is a ramdisk? 11 # Keeping a large dataset, such as a table or dictionary on ramdisk 12 #+ speeds up data lookup, since memory access is much faster than disk access. 13 14 15 E_NON_ROOT_USER=70 # Must run as root. 16 ROOTUSER_NAME=root 17 18 MOUNTPT=/mnt/ramdisk 19 SIZE=2000 # 2K blocks (change as appropriate) 20 BLOCKSIZE=1024 # 1K (1024 byte) block size 21 DEVICE=/dev/ram0 # First ram device 22 23 username=`id -nu` 24 if [ "$username" != "$ROOTUSER_NAME" ] 25 then 26 echo "Must be root to run \"`basename $0`\"." 27 exit $E_NON_ROOT_USER 28 fi 29 30 if [ ! -d "$MOUNTPT" ] # Test whether mount point already there, 31 then #+ so no error if this script is run 32 mkdir $MOUNTPT #+ multiple times. 33 fi 34 35 dd if=/dev/zero of=$DEVICE count=$SIZE bs=$BLOCKSIZE # Zero out RAM device. 36 mke2fs $DEVICE # Create an ext2 filesystem on it. 37 mount $DEVICE $MOUNTPT # Mount it. 38 chmod 777 $MOUNTPT # So ordinary user can access ramdisk. 39 # However, must be root to unmount it. 40 41 echo "\"$MOUNTPT\" now available for use." 42 # The ramdisk is now accessible for storing files, even by an ordinary user. 43 44 # Caution, the ramdisk is volatile, and its contents will disappear 45 #+ on reboot or power loss. 46 # Copy anything you want saved to a regular directory. 47 48 # After reboot, run this script again to set up ramdisk. 49 # Remounting /mnt/ramdisk without the other steps will not work. 50 51 exit 0 |