Emptying files with shell scripts

Source: Internet
Author: User
Emptying files with shell scripts

In recent projects, a lot of log files are generated, so you need to write a script to empty them manually, here are a few typical ways.

Method 1 Code
#!/bin/bash##方法1echo "" > $1echo "$1 cleaned up."
Run

Description

I think you should notice that the file is not actually emptied, but there is a blank line.

Method 2 Code
#!/bin/bash##方法2: > $1echo "$1 cleaned up."
Run

Description

Where: is an empty command that acts as a placeholder. The emptied file here no longer has a blank line to make the real sense of emptying.

Method 3 Code
#!/bin/bash##方法3cat /dev/null > $1echo "$1 cleaned up."
Run

Description

/dev/null can be seen as a "black hole". All content written to it will be lost. Read from it and read nothing. The emptied file here no longer has a blank line to make the real sense of emptying.

Summarize

Here I only listed three methods, I would like to clear the Linux file method is still a lot of, I personally prefer Method 2.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Emptying files with shell scripts

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.