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 a large file in Linux

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

Share

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

This article shows you how to empty or delete large files in Linux. The content is concise and easy to understand. It will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

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

The easiest way to empty or empty the contents of a file using shell redirection null (an object that does not exist) is as follows:

# > access.log

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

Here we will use a symbol: it is a shell built-in command, which is essentially equivalent to the true command, and it can be used as no operation (no operation).

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

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

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

In Linux, the null device is basically used to discard the unwanted output stream of the process, or as an appropriate empty file for the input stream. This is usually done through the redirection mechanism.

/ dev/null therefore, the device file is a special file that can log out (delete) any input sent to it, or its output is the same as the output of the empty file.

In addition, you can use the cat command / dev/null to redirect the output to it (the file) as input to clear the contents of the file.

# cat / dev/null > access.log

Next, we will use the cp command to clear the contents of the file, as shown in the figure.

# cp / dev/null access.log

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

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

4. Use the echo command to empty the file

Here, you can use the echo command with an empty string and redirect it to a file, as follows:

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

Note: you should remember that empty strings are different from null. The string is already an object because it may be empty, while null simply means that the object does not exist.

Therefore, when you redirect the above echo command to a file and use the cat command to view the contents of the file, a blank line (empty string) is printed.

To send empty output to a file, use the-n flag that tells echo not to print the trailing newline character that caused the blank line generated in the previous command.

# echo-n "" > access.log

5. Use the truncate command to empty the file

The truncation command helps shrink or expand the size of the file to the specified size.

You can use the option of-s to specify the file size. To clear the contents of the file, use a command with a size of 0 (zero):

# truncate-s 0 access.log what is the Linux system Linux is a free-to-use and free-spread UNIX-like operating system, is a POSIX-based multi-user, multi-tasking, multi-threaded and multi-CPU operating system, using Linux to run major Unix tools, applications and network protocols.

The above is how to empty or delete large files in Linux. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

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