In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article is about how to quickly empty files under Linux. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
When working with files under the Linux terminal, sometimes we want to empty or delete the contents of the files directly but do not want to use any Linux command line editor to open them.
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.log2. 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# true > access.log3. Use the cat/cp/dd utility and / dev/null device to empty the file
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, which can usually be achieved by using the redirection mechanism, 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
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
In the following command, if represents the input file and of represents the output file.
# dd if=/dev/null of=access.log4. 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.log5. 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 Thank you for reading! This is the end of the article on "how to quickly empty files under Linux". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.