linux清空日志内容的命令(Linux不停止服务快速清空日志文件)

以下操作不光是可以清空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

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 lqy2005888@qq.com 举报,一经查实,本站将立刻删除。