Linux文件清空的五種方法總結(jié)分享
更新時間:2017年03月25日 17:06:12 作者:LinuxPark
這篇文章主要給大家總結(jié)了Linux文件清空的五種方法,其中包括使用重定向的方法、使用true命令重定向清空文件、使用cat/cp/dd命令及/dev/null設(shè)備來清空文件、使用echo命令清空文件以及使用truncate命令清空文件等方法,需要的朋友可以參考下。
本文主要介紹的是Linux文件清空的五種方法,下面話不多說,來看看詳細的介紹吧。
1、使用重定向的方法
[root@centos7 ~]# du -h test.txt 4.0K test.txt [root@centos7 ~]# > test.txt [root@centos7 ~]# du -h test.txt 0 test.txt
2、使用true命令重定向清空文件
[root@centos7 ~]# du -h test.txt 4.0K test.txt [root@centos7 ~]# true > test.txt [root@centos7 ~]# du -h test.txt 0 test.txt
3、使用cat/cp/dd命令及/dev/null設(shè)備來清空文件
[root@centos7 ~]# du -h test.txt 4.0K test.txt [root@centos7 ~]# cat /dev/null > test.txt [root@centos7 ~]# du -h test.txt test.txt ################################################### [root@centos7 ~]# echo "Hello World" > test.txt [root@centos7 ~]# du -h test.txt 4.0K test.txt [root@centos7 ~]# cp /dev/null test.txt cp:是否覆蓋"test.txt"? y [root@centos7 ~]# du -h test.txt test.txt ################################################## [root@centos7 ~]# echo "Hello World" > test.txt [root@centos7 ~]# du -h test.txt 4.0K test.txt [root@centos7 ~]# dd if=/dev/null of=test.txt 記錄了0+0 的讀入 記錄了0+0 的寫出 0字節(jié)(0 B)已復(fù)制,0.000266781 秒,0.0 kB/秒 [root@centos7 ~]# du -h test.txt test.txt
4、使用echo命令清空文件
[root@centos7 ~]# echo "Hello World" > test.txt [root@centos7 ~]# du -h test.txt 4.0K test.txt [root@centos7 ~]# echo -n "" > test.txt ==>要加上"-n"參數(shù),默認情況下會"\n",也就是回車符 [root@centos7 ~]# du -h test.txt 0 test.txt
5、使用truncate命令清空文件
[root@centos7 ~]# du -h test.txt 4.0K test.txt [root@centos7 ~]# truncate -s 0 test.txt -s參數(shù)用來設(shè)定文件的大小,清空文件,就設(shè)定為0; [root@centos7 ~]# du -h test.txt 0 test.txt
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
相關(guān)文章
在centos7上搭建mysql主從服務(wù)器的方法(圖文教程)
這篇文章主要介紹了在centos7上搭建mysql主從服務(wù)器的方法,本文通過圖文并茂的形式給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-11-11
Centos 6.5 下配置DNS服務(wù)器的方法(圖文詳解)
這篇文章主要介紹了Centos 6.5 下配置DNS服務(wù)器的方法詳解,需要的朋友可以參考下2017-05-05
在linunx系統(tǒng)中搭建靜態(tài)文件服務(wù)的流程步驟
在服務(wù)器上有一些文件,想共享給其他用戶下載,同時因為ftp和sftp被禁用,且使用起來不太方便,需要找一種搭建成本低,安全高效的方式來完成此功能,因此linux上的httpd服務(wù)是一個很好的選擇,所以本文給大家介紹了在linux系統(tǒng)中搭建靜態(tài)文件服務(wù)的流程步驟2024-02-02

