Several ways to empty Linux files

Source: Internet
Author: User

1. How to use redirection
[email protected] ~]# du-4. 0K    test.txt test.txt [[email protected] ~]# du-0 test.txt  

2. Use the true command to redirect the empty file
[email protected] ~]# du-4. 0K    true > test.txt [[email protected] ~]# du-0 test.txt /c5>  

3. Use CAT/CP/DD command and/dev/null device to clear the file
[Email protected] ~]# du-h test.txt
4.0K Test.txt
[Email protected] ~]#cat/dev/null > Test.txt
[Email protected] ~]# du-h test.txt
Test.txt
###################################################
[Email protected] ~]# echo "Hello World" > Test.txt
[Email protected] ~]# du-h test.txt
4.0K Test.txt
[Email protected] ~]#cp/dev/null test.txt
CP: Do you want to overwrite "test.txt"? Y
[Email protected] ~]# du-h test.txt
Test.txt
##################################################
[Email protected] ~]# echo "Hello World" > Test.txt
[Email protected] ~]# du-h test.txt
4.0K Test.txt
[Email protected] ~]#DD If=/dev/null of=test.txt
Recorded 0+0 read-in
Recorded the writing of 0+0.
0 bytes (0 B) copied, 0.000266781 seconds, 0.0 kb/sec
[Email protected] ~]# du-h test.txt
Test.txt

4. Use the echo command to clear the file
 [[email protected] ~]# echo  "hello World "> Test.txt [[email protected] ~]# du-h test.txt 4.0k test.txt  [[email protected] ~]# echo-n     test.txt/* To add"-n "parameter, by default use" \ n ", carriage return      [[email protected] ~]# du-h test.txt 0 test.txt999            

5. Empty the file using the truncate command
[[email protected] ~]# du-4. 0K    test.txt0 test.txt-s parameter is used to set the file size, empty the file, set to 0; [email Protected] ~]# du-0 test.txt     

Several ways to empty Linux files

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.