Linux不停止服务快速清空日志文件(包含所有文件,不光是日志)

本文涉及的产品
日志服务 SLS,月写入数据量 50GB 1个月
简介: 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

相关实践学习
【涂鸦即艺术】基于云应用开发平台CAP部署AI实时生图绘板
【涂鸦即艺术】基于云应用开发平台CAP部署AI实时生图绘板
相关文章
|
4月前
|
Linux 开发工具
7种比较Linux中文本文件的最佳工具
7种比较Linux中文本文件的最佳工具
7种比较Linux中文本文件的最佳工具
|
2月前
|
存储 数据管理 Linux
区分Linux中.tar文件与.tar.gz文件的不同。
总之,".tar"文件提供了一种方便的文件整理方式,其归档但不压缩的特点适用于快速打包和解压,而".tar.gz"文件通过额外的压缩步骤,尽管处理时间更长,但可以减小文件尺寸,更适合于需要节约存储空间或进行文件传输的场景。用户在选择时应根据具体需求,考虑两种格式各自的优劣。
361 13
|
2月前
|
监控 Linux 应用服务中间件
linux查看日志文件tail -f用法
在 Linux 中,查看和监控日志文件是系统管理员和开发者常用的操作之一。tail 命令就是用来查看文件内容的,它默认显示文件的最后部分。tail -f 是 tail 命令的一个非常有用的选项,用于实时查看和跟踪日志文件的更新,尤其是在监控运行中的服务时非常有用。
416 0
|
3月前
|
安全 Linux
Linux赋予文件000权限的恢复技巧
以上这些步骤就像是打开一扇锁住的门,步骤看似简单,但是背后却有着严格的逻辑和规则。切记,在任何时候,变更文件权限都要考虑安全性,不要无谓地放宽权限,那样可能
124 16
|
3月前
|
存储 Linux 数据处理
深入剖析Linux中一切即文件的哲学和重定向的机制
在计算机的奇妙世界中,Linux的这套哲学和机制减少了不同类型资源的处理方式,简化了抽象的概念,并蕴藏着强大的灵活性。就像变戏法一样,轻轻松松地在文件、程序与设备之间转换数据流,标准输入、输出、错误流就在指尖舞动,程序的交互和数据处理因此变得既高效又富有乐趣。
54 4
|
4月前
|
Linux
【Linux】 Linux文件I/O常见操作技巧
以上就是Linux文件I/O操作的一些技巧,接纳它们,让它们成为你在Linux世界中的得力伙伴,工作会变得轻松许多。不过记住,技巧的运用也需要根据实际情况灵活掌握,毕竟,最适合的才是最好的。
122 28
|
4月前
|
Ubuntu Linux
"unzip"命令解析:Linux下如何处理压缩文件。
总的来说,`unzip`命令是Linux系统下一款实用而方便的ZIP格式文件处理工具。本文通过简明扼要的方式,详细介绍了在各类Linux发行版上安装 `unzip`的方法,以及如何使用 `unzip`命令进行解压、查看和测试ZIP文件。希望本文章能为用户带来实际帮助,提高日常操作的效率。
582 12
|
5月前
|
Linux Shell
在Linux、CentOS7中设置shell脚本开机自启动服务
以上就是在CentOS 7中设置shell脚本开机自启动服务的全部步骤。希望这个指南能帮助你更好地管理你的Linux系统。
402 25
|
3月前
|
Linux
linux文件重命名命令
本指南介绍Linux文件重命名方法,包括单文件操作的`mv`命令和批量处理的`rename`命令。`mv`可简单更改文件名并保留扩展名,如`mv old_file.txt new_name.txt`;`rename`支持正则表达式,适用于复杂批量操作,如`rename 's/2023/2024/' *.log`。提供实用技巧如大小写转换、数字序列处理等,并提醒覆盖风险与版本差异,建议使用`-n`参数预览效果。
|
5月前
|
Linux Shell
Linux系统下快速批量创建和删除文件的方法
总的来说,使用shell脚本来批量处理文件是一种非常强大的工具,只要你愿意花时间学习和实践,你会发现它能大大提高你的工作效率。
301 19

热门文章

最新文章