Skill pack! Five methods for clearing or deleting large files in Linux

Source: Internet
Author: User
Tags echo command line editor

Skill pack! Five methods for clearing or deleting large files in Linux
GuideWhen processing files on a Linux terminal, sometimes we want to directly clear the file content without using any Linux Command Line Editor to open these files. So how can we achieve this goal? In this article, we will introduce several methods to clear file content by using some practical commands.

Note: because everything in Linux is a file, you must always pay attention to ensuring that the files you want to clear are not important user files or system files. Clearing important system files or configuration files may cause serious application failures or system errors.

Tip: In the following example, we will use a file named access. log as an example.

1. Redirect to Null to clear the File Content

The simplest way to clear or make a file blank is to redirect null (non-existent) to the file through shell as follows:

# > access.log

2. Use the 'true' command to redirect to clear files

Here we will use the symbol, which is a built-in shell command, equivalent to the true command, which can be used as a no-op (that is, no operation is performed ). Another way to clear a file is to redirect the output of the built-in command "true" to the file, as follows:

# : > access.log# true > access.log

3. Use cat/cp/dd utility and/dev/null device to clear files

In Linux, the null device is basically used to discard output streams that are no longer needed by a process, or as a blank file of an input stream. These can usually be achieved through the redirection mechanism, therefore, the/dev/null device file is a special file. It will empty all input sent to it, and its output can be considered as an empty file. In addition, you can use the cat command to display/dev/null and redirect the content to a file to clear the file.

# cat /dev/null > access.log

Next, we will use the cp command to copy/dev/null content to a file to clear the file, as shown below:

# cp /dev/null access.log


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

# dd if=/dev/null of=access.log

4. Run the echo command to clear the file.

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

# Echo ""> access. log or # echo> access. log


Note: Empty strings are not equivalent to null. A string indicates that it is a specific thing, but its content may be empty, but null means that a thing does not exist. For this reason, when you redirect the echo command output to a file as input and use the cat command to view the content of the file, you will see a blank line (an empty string ).
To use null as the output input to the file, you should use the-n option, which will tell echo not to output the new line ending with the command above.

# echo -n "" > access.log

5. Run the truncate command to clear the file content.

Truncate can be used to narrow down or expand a file to a specified size.
You can use it and the-s parameter to specify the file size. To clear the file content, set the file size to 0 in the following command:

# truncate -s 0 access.log

I want to introduce so much. In this article, we introduce several methods to clear or clear file content by using some simple command line tools and shell redirection mechanisms.

From: https://linux.cn/article-8024-1.html

Address: http://www.linuxprobe.com/linux-clear-files.html


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.