以下操作不光是可以清空nohup.log这种日志,而是所有的文件都可以清空,包含txt等。
第一种: cat /dev/null > ./nohup.log
[root@redhat75::~]# ll -h
total 216K
-rw-------. 1 root root 2.0K May 27 2020 anaconda-ks.cfg
-rw-r--r-- 1 root root 41K Apr 7 14:00 nohup.log #待清空的日志文件
-rw-r--r-- 1 root root 41K Apr 7 14:00 nohup.log.bak #备份的日志文件
[root@redhat75::~]# cat /dev/null > ./nohup.log #清空日志文件
[root@redhat75::~]# ll
total 172
-rw-------. 1 root root 2012 May 27 2020 anaconda-ks.cfg
-rw-r--r-- 1 root root 0 Apr 7 14:01 nohup.log #日志文件大小已经为0,已经清空
-rw-r--r-- 1 root root 41490 Apr 7 14:00 nohup.log.bak
第二种:>./nohup.log
[root@redhat75::~]# ll -h
total 216K
-rw-------. 1 root root 2.0K May 27 2020 anaconda-ks.cfg
-rw-r--r-- 1 root root 41K Apr 7 14:00 nohup.log #待清空的日志文件
-rw-r--r-- 1 root root 41K Apr 7 14:00 nohup.log.bak #备份的日志文件
[root@redhat75::~]# >./nohup.log #清空日志文件
[root@redhat75::~]# ll
total 172
-rw-------. 1 root root 2012 May 27 2020 anaconda-ks.cfg
-rw-r--r-- 1 root root 0 Apr 7 14:02 nohup.log #日志文件大小已经为0,已经清空
-rw-r--r-- 1 root root 41490 Apr 7 14:00 nohup.log.bak
第三种:echo “” >./nohup.log 或者 echo >./nohup.log
[root@redhat75::~]# ll -h
total 216K
-rw-------. 1 root root 2.0K May 27 2020 anaconda-ks.cfg
-rw-r--r-- 1 root root 41K Apr 7 14:09 nohup.log
-rw-r--r-- 1 root root 41K Apr 7 14:00 nohup.log.bak
[root@redhat75::~]# echo "" >./nohup.log
[root@redhat75::~]# ll -h
total 176K
-rw-------. 1 root root 2.0K May 27 2020 anaconda-ks.cfg
-rw-r--r-- 1 root root 1 Apr 7 14:10 nohup.log
-rw-r--r-- 1 root root 41K Apr 7 14:00 nohup.log.bak
[root@redhat75::~]# cat nohup.log
第四种::>./nohup.log(脚本中常用)
[root@redhat75::~]# ll
total 216
-rw-------. 1 root root 2012 May 27 2020 anaconda-ks.cfg
-rw-r--r-- 1 root root 41490 Apr 7 14:12 nohup.log
-rw-r--r-- 1 root root 41490 Apr 7 14:00 nohup.log.bak
[root@redhat75::~]# :>./nohup.log
[root@redhat75::~]# ll -h
total 172K
-rw-------. 1 root root 2.0K May 27 2020 anaconda-ks.cfg
-rw-r--r-- 1 root root 0 Apr 7 14:12 nohup.log
-rw-r--r-- 1 root root 41K Apr 7 14:00 nohup.log.bak
第五种:cp /dev/null ./nohup.log
[root@redhat75::~]# ll -h
total 216K
-rw-------. 1 root root 2.0K May 27 2020 anaconda-ks.cfg
-rw-r--r-- 1 root root 41K Apr 7 14:16 nohup.log
-rw-r--r-- 1 root root 41K Apr 7 14:00 nohup.log.bak
[root@redhat75::~]# cp /dev/null ./nohup.log
cp: overwrite ‘./nohup.log’? y
[root@redhat75::~]# ll -h
total 172K
-rw-------. 1 root root 2.0K May 27 2020 anaconda-ks.cfg
-rw-r--r-- 1 root root 0 Apr 7 14:16 nohup.log
-rw-r--r-- 1 root root 41K Apr 7 14:00 nohup.log.bak
第六种:dd if=/dev/null of=./nohup.log (if表示输入文件,of表示输出文件)
[root@redhat75::~]# ll -h
total 216K
-rw-------. 1 root root 2.0K May 27 2020 anaconda-ks.cfg
-rw-r--r-- 1 root root 41K Apr 7 14:37 nohup.log
-rw-r--r-- 1 root root 41K Apr 7 14:00 nohup.log.bak
[root@redhat75::~]# dd if=/dev/null of=./nohup.log
0+0 records in
0+0 records out0 bytes (0 B) copied, 0.000278986 s, 0.0 kB/s
[root@redhat75::~]# ll -h
total 172K
-rw-------. 1 root root 2.0K May 27 2020 anaconda-ks.cfg
-rw-r--r-- 1 root root 0 Apr 7 14:37 nohup.log
-rw-r--r-- 1 root root 41K Apr 7 14:00 nohup.log.bak
第七种:dd if=/dev/null of=./nohup.log (if表示输入文件,of表示输出文件)
[root@redhat75::~]# ll -h
total 216K
-rw-------. 1 root root 2.0K May 27 2020 anaconda-ks.cfg
-rw-r--r-- 1 root root 41K Apr 7 14:44 nohup.log
-rw-r--r-- 1 root root 41K Apr 7 14:00 nohup.log.bak
[root@redhat75::~]# truncate -s 0 ./nohup.log
[root@redhat75::~]# ll -h
total 172K
-rw-------. 1 root root 2.0K May 27 2020 anaconda-ks.cfg
-rw-r--r-- 1 root root 0 Apr 7 14:44 nohup.log
-rw-r--r-- 1 root root 41K Apr 7 14:00 nohup.log.bak
【个人公众号】
【非著名运维】、【暴科技】 --》 公众号回复 “资料” 送运维自学资料大礼包哦!
上面也会分享一些运维知识,大家点点发财手关注一波,感谢大家。