The use of/dev/null under Linux

Source: Internet
Author: User

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

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 # It is useful when you want to test only the exit code of a command without any output.
6 #-----------The exit begin----------------------of the test command #
7 # ls dddd 2>/dev/null 8 8 # echo $?     Output command exit code: 0 for the command to execute correctly, 1-255 for error. 9 #-----------The exit end-----------of the test command #
10# Cat $filename &>/dev/null 11 # can also be noted by Baris Cicek.

Clear Log File contents

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

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.

Related Article

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.