Five ways to empty or delete large file contents in Linux _linux

Source: Internet
Author: User
Tags echo command pack line editor

When working with files in a Linux terminal, sometimes we want to empty the contents of the file directly without having to open the files with any Linux command-line editor. So how do we achieve this goal? In this article, we'll introduce several ways to empty the contents of a file with some practical commands.

Note: Since everything in Linux is documented, you need to be aware that the files you are about to empty are not important user files or system files. Emptying important system files or configuration files can cause serious application failures or system errors.

Hint: In the following example, we will use a file named Access.log as the sample sample.

1. Empty file contents by redirecting to Null

The simplest way to empty or make a file blank is to redirect null (nonexistent things) through the shell to the file as follows:

# > Access.log

Skill Pack! Linux to empty or delete large file content 5 ways Skill pack! 5 ways to empty or delete large file content under Linux

2. Use ' true ' command redirection to empty files

Here we will use the symbol, which is a built-in command of the shell, equivalent to the true command, which can be used as a no-op (that is, not doing anything). Another way to empty a file is to redirect the output of a built-in command, or True, to a file as follows:

#: > Access.log
# True > Access.log

Skill Pack! Linux to empty or delete large file content 5 ways Skill pack! 5 ways to empty or delete large file content under Linux

3. Use CAT/CP/DD utility and/dev/null device to empty files

In Linux, a null device is basically used to discard output streams that a process no longer needs, or as a blank file for an input stream, which can usually be achieved by using the redirection mechanism, so the/dev/null device file is a special file that empties all input into it. And its output can be treated as an empty file. In addition, you can use the Cat command to display the contents of the/dev/null and then redirect the output to a file, which is the purpose of emptying the file.

# Cat/dev/null > Access.log

Skill Pack! Linux to empty or delete large file content 5 ways Skill pack! 5 ways to empty or delete large file content under Linux

Below, we will use the CP command to copy the contents of/dev/null to a file for the purpose of emptying the file, as shown below:

# Cp/dev/null Access.log

Skill Pack! Linux to empty or delete large file content 5 ways Skill pack! 5 ways to empty or delete large file content under Linux

In the following command, the if represents the input file, and the of represents the output file.

# dd If=/dev/null Of=access.log

Skill Pack! Linux to empty or delete large file content 5 ways Skill pack! 5 ways to empty or delete large file content under Linux

4. Use the echo command to empty the file

Here, you can use the echo command to redirect the contents of an empty string to a file, as follows:

# echo "" > Access.log
or
# echo > Access.log

Skill Pack! Linux to empty or delete large file content 5 ways Skill pack! 5 ways to empty or delete large file content under Linux

Note: You should remember that an empty string is not equivalent to NULL. A string indicates that it is a specific thing, except that its contents may be empty, but null means that something does not exist. For this reason, when you redirect the output of the echo command to a file as input, and you use the Cat command to view the contents of the file, you will see a blank line (that is, an empty string).

To input null as output into a file, you should use the-n option, which tells Echo to not output the new line at the end of the command like the one above.

# echo-n "" > Access.log

Skill Pack! Linux to empty or delete large file content 5 ways Skill pack! 5 ways to empty or delete large file content under Linux

5. Use the TRUNCATE command to empty the contents of the file

Truncate can be used to shrink or extend a file to a given size.
You can use it and the-s parameter to specifically specify the size of the file. To empty the contents of a file, set the size of the file to 0 in the following command:

# truncate-s 0 Access.log

Skill Pack! Linux to empty or delete large file content 5 ways Skill pack! 5 ways to empty or delete large file content under Linux

That's all I'm going to introduce. In this article, we describe several ways to clear or empty the contents of a file by using a few simple command-line tools and shell redirection mechanisms.

The above is a small set to introduce the Linux to empty or delete large file contents of the five methods, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.