The following operations can not only clear logs such as nohup.log, but also all files, including txt, etc.
The first one: 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
The second type: >./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
The third type: echo “” >./nohup.log or 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
The fourth type: :>./nohup.log (commonly used in scripts)
[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
The fifth type: 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
Sixth type: dd if=/dev/null of=./nohup.log (if means input file, of means output file)
[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
Seventh type: dd if=/dev/null of=./nohup.log (if means input file, of means output file)
[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
[Personal public account]
【非著名运维】、【暴科技】 --》 公众号回复 “资料” 送运维自学资料大礼包哦!
I will also share some operation and maintenance knowledge above. Please pay attention to it. Thank you all.