[Linux]/dev/null and/dev/zero

Source: Internet
Author: User
Use /Dev/null

Set/Dev/nullIt is regarded as a "black hole". It is very equivalent to a write-only file. All the content written to it will be lost forever. However, if you try to read the content from it, nothing can be read. However,/Dev/nullCommand Line and script are very useful.

DisableStandard output.

1 cat $ FILENAME>/dev/null
2 # file content is lost without being output to standard output.

DisableStandard Error(From example 12-3 ).

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

Disable Standard output and standard error output.

1 cat $ filename 2>/dev/null
2 # If "$ FILENAME" does not exist, no error message is prompted.
3 # If "$ FILENAME" exists, the file content will not be printed to the standard output.
4 # therefore, the above Code will not output any information.
5 #
6 # It is useful when you only want to test the exit code of the command and do not want to have any output.
7 #
8 #
9 # Cat $ filename &>/dev/null
10 # Yes, as specified 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 no new process will be generated (because: built-in)
3
4 CAT/dev/null>/var/log/wtmp

Automatically clear the content of log files (especially suitable for processing these annoying "cookies" sent by commercial web sites "):

Example 28-1. Hide the cookie and stop using it

1 If [-f ~ /. Netscape/cookies] # Delete the cookies if they exist.
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 instead of stored on the disk.
Use /Dev/zero

Image/Dev/nullSame,/Dev/zero is also a pseudo FileBut it actually produces continuous null streams (Binary zero streams instead of ASCII). The output written to it will be lost/Dev/zeroReading a series of null statements is also difficult, although it can also be done through OD or a hexadecimal editor./Dev/zeroIt is mainly used to create an empty file with a specified length for initialization, just like a temporary swap file.

Example 28-2.Use/dev/zero to create a temporary file for swap

1 #! /Bin/bash
2 # create a swap file.
3
4 root_uid = 0 # the root user's $ uid is 0.
5 e_wrong_user = 65 # Not root?
6
7 file =/swap
8 blocksize = 1024
9 minblocks = 40
10 success = 0
11
12
13 # The script must be run as root.
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 blocks =$ {1:-$ minblocks} # If the command line is not specified,
22 # + is set to the default 40.
23 # The above sentence is equivalent:
24 #--------------------------------------------------
25 # If [-n "$1"]
26 # Then
27 # blocks = $1
28 # else
29 # blocks = $ minblocks
30 # fi
31 #--------------------------------------------------
32
33
34 if ["$ blocks"-lt $ minblocks]
35 then
36 blocks = $ minblocks # a minimum length of 40 blocks is required.
37 fi
38
39
40 echo "creating swap file of size $ blocks (KB )."
41 dd If =/dev/Zero of = $ file BS = $ blocksize COUNT = $ blocks # writes zero to a file.
42
43 mkswap $ File $ blocks # create this file as a swap file (or a swap partition ).
44 Swapon $ file # activate the swap file.
45
46 echo "swap file created and activated ."
47
48 exit $ success

About/Dev/zeroAnother application is to fill a file of the specified size with zero fill for a specific purpose, such as mounting a file system to the loopback device (see example 13-8) or "Safely" delete an object (see example 12-55 ).

Example 28-3. Create ramdisk

1 #! /Bin/bash
2 # ramdisk. Sh
3
4 # "ramdisk" is a part of the system RAM memory,
5 # + It can be operated as a file system.
6 # its advantage is that the access speed is very fast (including reading and writing ).
7 # disadvantages: Volatile. Data is lost when the computer is restarted or shut down.
8 # + will reduce the available system Ram.
9 #
10 # What is the role of ramdisk?
11 # Save a large dataset on ramdisk, such as a table or dictionary,
12 # + this will accelerate data query, because searching in memory is much faster than searching in disk.
13
14
15 e_non_root_user = 70 # It must be run with root.
16 rootuser_name = root
17
18 mountpt =/mnt/ramdisk
19 size = 2000 #2 k blocks (which can be modified as appropriate)
20 blocksize = 1024 # each block has a size of 1 K (1024 bytes)
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 the mount point already exists,
31 then # + If the script has been run several times, the directory will not be created again.
32 mkdir $ mountpt # + because it has been created earlier.
33 fi
34
35 dd If =/dev/Zero of = $ device COUNT = $ size BS = $ blocksize # Fill the content of the ram device with zero.
36 # Why?
37 mke2fs $ device # create an ext2 File System on the RAM device.
38 Mount $ device $ mountpt # mount the device.
39 chmod 777 $ mountpt # enable normal users to access this ramdisk.
40 # However, it can only be uploaded by the root user.
41
42 echo "\" $ mountpt \ "now available for use ."
43 # Now ramdisk can be used to access files even for common users.
44
45 # note that ramdisk is easy to lose, so the content in ramdisk disappears when the computer system is restarted or shut down.
46 #
47 # copy all the files you want to save to a regular disk directory.
48
49 # Run the script to create a ramdisk again after restart.
50 # Only reload/mnt/ramdisk and no other steps will work correctly.
51
52 # If it is improved, this script can be placed in/etc/rc. d/rc. Local,
53 # + to automatically set up a ramdisk when the system starts.
54 # This is suitable for database servers with high speed requirements.
55
56 exit 0

It is worth mentioning that,The ELF binary file uses/dev/zero..

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.