Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to clear or delete the contents of large files in Linux

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

Today, I will talk to you about how to empty or delete the contents of large files in Linux. Many people may not know much about it. In order to make you understand better, the editor summed up the following contents for you. I hope you can get something according to this article.

1. Clear the contents of the file by redirecting to Null

The easiest way to empty or make a file blank is to redirect null (things that don't exist) to the file through shell as follows:

# > access.log

Use Null redirection to empty large files under Linux

two。 Use the 'true' command redirect to empty the file

Next we will use the: symbol, which is a built-in command for shell, which is equivalent to the true command, and can be used as a no-op (that is, to do nothing).

Another way to empty a file is to redirect the output of: or true built-in commands to a file, as follows:

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

Use the Linux command to empty large files

3. Use the cat/cp/dd utility and / dev/null device to empty the file

In Linux, null devices are basically used to discard output streams that are no longer needed by a process, or as blank files for an input stream, which can usually be achieved using redirection mechanisms.

So the / dev/null device file is a special file that empties all inputs sent to it, while its output can be treated as an empty file.

In addition, you can empty the file by displaying the contents of / dev/null using the cat command and then redirecting the output to a file.

# cat / dev/null > access.log

Use the cat command to empty the file

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

# cp / dev/null access.log

Use the cp command to empty the file

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

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

Use the dd command to clear the contents of the file

4. Use the echo command to empty the file

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

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

Use the echo command to empty the file

Note: you should remember that an empty string is not the same as null. A string indicates that it is a concrete thing, except that its content 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 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 into a file as output, you should use the-n option, which tells echo not to output the new line at the end like the command above.

# echo-n "" > access.log

Use Null redirection to empty files

5. Use the truncate command to clear the contents of the file

Truncate can be used to shrink or expand a file to a given size.

You can use it and the-s parameter to specify the size of the file. To empty the contents of the file, set the size of the file to 0 in the following command:

# truncate-s 0 access.log

After reading the above, do you have any further understanding of how to empty or delete large files in Linux? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report