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

7.png

第二种:>./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

8.png

第三种: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


9.png

第四种::>./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

10.png

第五种: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

11.png

第六种: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

12.png

第七种: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

13.png

相关实践学习
日志服务之使用Nginx模式采集日志
本文介绍如何通过日志服务控制台创建Nginx模式的Logtail配置快速采集Nginx日志并进行多维度分析。
相关文章
|
3天前
|
监控 Linux Perl
【专栏】Linux 命令小技巧:显示文件指定行的内容
【4月更文挑战第28天】本文介绍了Linux中显示文件指定行内容的方法,包括使用`head`和`tail`命令显示文件头尾部分,利用`sed`的行号指定功能以及`awk`处理文本数据。文章还列举了在代码审查、日志分析和文本处理中的应用场景,并提醒注意文件编码、行号准确性及命令组合使用。通过练习和实践,可以提升Linux文本文件处理的效率。
|
1天前
|
存储 监控 Linux
|
2天前
|
安全 Linux
【亮剑】如何在Linux使用 chattr 命令更改文件或目录的扩展属性?
【4月更文挑战第30天】`chattr`是Linux中用于管理文件和目录扩展属性的命令,影响文件系统处理方式。常用属性包括:`a`(追加)、`i`(不可变)、`s`(安全删除)和`S`(同步更新)。通过`chattr [选项] <模式> <文件或目录>`设置属性,如`chattr +i <文件名>`使文件不可变,`-i`移除不可变属性。`lsattr`用于查看属性。注意,只有root用户有权更改属性,不是所有文件系统都支持所有属性,且更改关键文件属性前应备份。`chattr`有助于提升系统安全性和数据保护。
|
2天前
|
Linux 开发者
【亮剑】Linux 中的文件锁定命令:flock、fcntl、lockfile、flockfile
【4月更文挑战第30天】本文介绍了Linux系统中的四种文件锁定机制:flock、fcntl、lockfile和flockfile,用于多进程环境下协调共享资源访问,防止数据损坏和竞争条件。flock适合脚本,fcntl提供底层灵活性,lockfile用于管理锁定文件,flockfile则结合两者功能。选择锁定策略时需考虑应用场景,如脚本可选flock,复杂需求则用fcntl。理解并正确使用这些工具对保证系统稳定性和数据一致性至关重要。
|
2天前
|
存储 算法 Linux
【Linux】文件打包解压_tar_zip
【Linux】文件打包解压_tar_zip
14 0
【Linux】文件打包解压_tar_zip
|
2天前
|
Linux 开发者
【Linux】:文件查看 stat、cat、more、less、head、tail、uniq、wc
【Linux】:文件查看 stat、cat、more、less、head、tail、uniq、wc
12 1
|
2天前
|
安全 Linux 数据处理
|
2天前
|
存储 弹性计算 运维
统计/var/log 有多少个文件
【4月更文挑战第29天】
9 1
|
2天前
|
Linux
|
2天前
|
弹性计算 运维 Shell