About/dev/null and Its Usage

Source: Internet
Author: User

Set/Dev/nullAs"Black hole".It is very equivalent to writing only files. All the content written to it will be lost forever. However, you cannot read anything when trying to read the content from it. However,
/Dev/nullIt is very useful for command lines and scripts.

DisableStandard output.

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

DisableStandard Error

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, therefore, the aboveCodeIt does not output any information at all. 5 # It is very useful when you only want to test the exit code of the command and do not want to have any output. 6 # ----------- exit the test command begin -------------------- #7 # ls dddd 2>/dev/null 8 # echo $? // Exit code of the output command: 0 indicates that the command is executed normally, and 1-indicates that an error occurs. 9 # ----------- exit of the test command end ----------- #10 # Cat $ filename &>/dev/null 11 # alternatively, it can be pointed out by Baris Cicek.

Clear Log File Content

1 CAT/dev/null>/var/log/messages 2 #:>/var/log/messages has the same effect, but no new process is generated. (Because: built-in) 3 4 CAT/dev/null>/var/log/wtmp

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

1 If [-f ~ /. Netscape/cookies] # Delete. 2 then 3 Rm-f ~ if any ~ /. Netscape/cookies 4 fi 5 6 ln-S/dev/null ~ /. Netscape/cookies 7 # Now all cookies will be thrown into the black hole and will not be saved on the disk.

From: Advanced bash-scripting Guide: an in-depth injection of the art of shell Scripting

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.