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

What is the command for the Linux system to empty the contents of the file?

2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article is to share with you what the command for emptying the contents of the file in the Linux system is, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

Can you clear the contents of a file without a text editor in a Linux system? The answer is yes, and there are some commands in the Linux system that can directly empty the contents of the file without having to open the file using a text editor.

1. The easiest way to empty the contents of a file or make a file blank by redirecting to Null is to redirect null (things that don't exist) to the file through shell as follows:

# > access.log

two。 Use the 'true' command redirection to empty the file below 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, 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 # true > access.log

3. Use the cat/cp/dd utility and / dev/null device to empty files 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 by redirection, so the / dev/null device file is a special file that empties all inputs sent to it. 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

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 while in the following command, if represents the input file and of represents 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 to redirect the contents of the empty string to the file, as follows:

# echo "" > access.log or # echo > access.log 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

5. Using the truncate command to empty the contents of a 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

The above is the command for Linux system to clear the contents of the files. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please 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

Development

Wechat

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

12
Report