About/dev/null and/dev/zero

Source: Internet
Author: User
Tags volatile
Using/dev/null

Think of/dev/null as a "black hole". It is very equivalent to a write-only file. All content written to it is lost forever. And trying to read content from it is nothing. However,/dev/null is very useful for both the command line and the script.

Prohibit standard output.

   1 Cat $filename >/dev/null
2 # The file content is missing and will not be exported to standard output.

Prohibit standard error (from Example 12-3).

   1 RM $badname 2>/dev/null
2 # This error message [standard error] was thrown into the Pacific Ocean.

Suppresses standard output and standard error output.

   1 Cat $filename 2>/dev/null >/dev/null
2 # if "$filename" does not exist, there will be no error message prompts.
3 # if "$filename" exists, the contents of the file are not printed to standard output.
4 # So therefore, the code above does not output any information at all.
5 #
6 # It is useful when you want to test only the exit code of a command without any output.
7 #
8 #
9 # cat $filename &>/dev/null
# can also be, pointed out by Baris Cicek.

Deleting contents of a file, but preserving the file itself, with all attendant permissions (from Example 2-1 and Example 2-3):

   1 Cat/dev/null >/var/log/messages
2 # : >/var/log/messages has the same effect, but will not produce a new process. (because: is built)
3
4 Cat/dev/null >/var/log/wtmp

Automatically empties the contents of the log file (especially for those pesky "cookies" sent by a commercial web site):

example 28-1. Hide cookies and no longer used

   1 if [f ~/.netscape/cookies]  # if present, delete.
2 Then
3 rm-f ~/.netscape/cookies
4 fi
5
6 Ln-s/dev/null ~/.netscape/cookies
7 # Now all cookies will be thrown into the black hole and not stored on the disk.
Using/dev/zero

Like/dev/null,/dev/zero is also a pseudo file, but it actually produces a continuous stream of NULL (binary 0 streams, not ASCII). The output that is written to it is lost and it is difficult to read a series of NULL from/dev/zero, although this can be done through OD or a hexadecimal editor. The main use of/dev/zero is to create an empty file with a specified length for initialization, just like a temporary swap file.

example 28-2. Create an Exchange temp file with/dev/zero

1 #!/bin/bash
2 # Create an interchange file.
3
The $UID of the 4 root_uid=0 # ROOT user is 0.
5 e_wrong_user=65 # not root?
6
7 File=/swap
8 blocksize=1024
9 minblocks=40
Ten success=0
11
12
13 # This script must be run with root.
If ["$UID"-ne "$ROOT _uid"]
Then
echo; echo "You must is root to run this script." Echo
Exit $E _wrong_user
Fi
19
20
blocks=${1:-$MINBLOCKS} # If the command line is not specified,
22 #+ is set to the default 40 block.
23 # The above sentence equals:
#--------------------------------------------------
# if [-N ' $]
Num # Then
# blocks=$1
# Else
# blocks= $MINBLOCKS
# fi
#--------------------------------------------------
32
33
If ["$blocks"-lt $MINBLOCKS]
KM then
blocks= $MINBLOCKS # At least 40 blocks long.
Panax Notoginseng fi
38
39
echo "Creating swap file of size $blocks blocks (KB)."
The DD If=/dev/zero of= $FILE bs= $BLOCKSIZE count= $blocks # writes zeros to the file.
42
Mkswap $FILE $blocks # to build this file as an interchange file (or swap partition).
Swapon $FILE # Activates the swap file.
45
+ echo "Swap file created and activated."
47
Exit $SUCCESS

Another application for/dev/zero is to populate a specified size file with 0 for a specific purpose, such as mounting a file system to the loopback device (loopback device) (reference example 13-8) or "safely" deleting a file (reference example 12-55).

example 28-3. Create RAMDisk

1 #!/bin/bash
2 # ramdisk.sh
3
4 # "RAMDisk" is a section of system RAM memory,
5 #+ It can be used as a file system to operate.
6 # Its advantage is that the access speed is very fast (both read and write).
7 # Disadvantage: Volatile, loss of data when the computer restarts or shuts down.
8 #+ will reduce the amount of RAM available to the system.
9 #
10 # So what's the effect of RAMDisk?
11 # Save a larger dataset in RAMDisk, such as a table or dictionary,
12 #+ This can speed up data queries, because it is much faster to look up in memory than to find them on disk.
13
14
E_NON_ROOT_USER=70 # must be run with ROOT.
Rootuser_name=root
17
Mountpt=/mnt/ramdisk
size=2000 # 2K Blocks (can be modified as appropriate)
blocksize=1024 # 1 K (1024 byte) size per block
DEVICE=/DEV/RAM0 # First RAM device
22
Username= ' Id-nu '
If ["$username"!= "$ROOTUSER _name"]
Then
The echo "must is root to run/" ' BaseName $ '/"."
Exit $E _non_root_user
Fi
29
if [!-d ' $MOUNTPT] # test If the mount point already exists,
Then #+ if this script has been running several times, it will not be built again.
mkdir $MOUNTPT #+ Because the front has been established.
-Fi
34
The DD If=/dev/zero of= $DEVICE count= $SIZE bs= $BLOCKSIZE # fills the contents of a RAM device with 0.
36 # Why do you need to do this?
MKE2FS $DEVICE # Create a ext2 file system on a RAM device.
Mount $DEVICE $MOUNTPT # mount the device.
The chmod 777 $MOUNTPT # enables ordinary users to access this ramdisk.
40 # However, only the root can be unloading to load it.
41
The echo "/" $MOUNTPT/"now available to use."
43 # Now RAMDisk even ordinary users can be used to access files.
44
45 # Note that RAMDisk is volatile, so when the computer system restarts or shuts down, the contents of the RAMDisk disappear.
46 #
47 # Copy all you want to save the file to a regular disk directory.
48
49 # After reboot, run this script to set up a RAMDisk again.
50 # only Reload/mnt/ramdisk without other steps will not work correctly.
51
52 # If improved, this script can be placed in/etc/rc.d/rc.local,
53 #+ to automatically set up a ramdisk when the system is started.
54 # This is very suitable for the database server with high speed requirements.
55
0 exit

Finally, it is worth mentioning that Elf binaries use/dev/zero.

Turn from: http://www.linuxsir.org/main/doc/abs/abs3.7cnhtm/zeros.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.