The difference between/dev/null and/dev/zero

Source: Internet
Author: User

The difference between/dev/null and/dev/zero

/dev/null, nicknamed the Bottomless pit, you can output any data to it, it take all, and will not hold!

/dev/zero, is an input device that you can use to initialize files. The device provides an exhaustive 0 and can use any number you need-the device offers much more. He can be used to write a string 0 to a device or file.

/dev/null--It is an empty device, also known as a bit bucket (bits bucket). Any output written to it will be discarded. If you do not want the message to display or write to the file in standard output, you can redirect the message to the bin.

If=/dev/zero of=./test.txt bs=1k count=1
Ls–l
Total 4
-rw-r--r--1 Oracle DBA 16:56 test.txt
Find/-name Access_log 2>/dev/null

Using/dev/null
Think of/dev/null as a "black hole", which is equivalent to a write-only file, and all content written to it will be lost forever. and trying to read from it is nothing. However,/dev/null is very useful for command lines and scripts.

Prohibit standard output
The cat $filename >/dev/null #文件内容丢失 without outputting to standard output.

Prohibit standard error
RM $badname 2>/dev/null #这样错误信息 [standard error] was dumped in the Pacific Ocean.

Suppress output of standard output and standard error
Cat $filename 2>/dev/null >/dev/null

If the "$filename" does not exist, there will be no error message, and if "$filename" exists, the contents of the file will not be printed to standard output. Therefore, the above code does not output any information at all. This is useful when you want to test only the exit code of a command without any output.

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 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 hex 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.

Create a swap temporary file with/dev/zero

#!/bin/bash
# Create a swap file.
The $UID of the Root_uid=0 # Root user is 0.
E_WRONG_USER=65 # not Root?
File=/swap
blocksize=1024
Minblocks=40
Success=0
# 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
blocks=${1:-$MINBLOCKS} # If the command line is not specified,


If ["$blocks"-lt $MINBLOCKS]
Then
blocks= $MINBLOCKS # A minimum of 40 blocks long.
Fi
echo "Creating swap file of size $blocks blocks (KB)."
DD If=/dev/zero of= $FILE bs= $BLOCKSIZE count= $blocks # writes zeros to the file.
Mkswap $FILE $blocks # to build this file as a swap file (or swap partition).
Swapon $FILE # Activates the swap file.
echo "Swap file created and activated."
Exit $SUCCESS

The difference between/dev/null and/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.