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

相关实践学习
日志服务之使用Nginx模式采集日志
本文介绍如何通过日志服务控制台创建Nginx模式的Logtail配置快速采集Nginx日志并进行多维度分析。
相关文章
|
3天前
|
Linux
Linux 常用文件查看命令
`cat` 命令用于连接文件并打印到标准输出,适用于快速查看和合并文本文件内容。常用示例包括:`cat file1.txt` 查看单个文件,`cat file1.txt file2.txt` 合并多个文件,`cat > filename` 创建新文件,`cat >> filename` 追加内容。`more` 和 `less` 命令用于分页查看文件,`tail` 命令则用于查看文件末尾内容,支持实时追踪日志更新,如 `tail -f file.log`。
21 5
Linux 常用文件查看命令
|
3天前
|
缓存 Java 编译器
|
15天前
|
存储 Linux Shell
Linux|Transfer.sh 轻松实现文件共享
Linux|Transfer.sh 轻松实现文件共享
28 2
Linux|Transfer.sh 轻松实现文件共享
|
1月前
|
存储 网络协议 Linux
【Linux】进程IO|系统调用|open|write|文件描述符fd|封装|理解一切皆文件
本文详细介绍了Linux中的进程IO与系统调用,包括 `open`、`write`、`read`和 `close`函数及其用法,解释了文件描述符(fd)的概念,并深入探讨了Linux中的“一切皆文件”思想。这种设计极大地简化了系统编程,使得处理不同类型的IO设备变得更加一致和简单。通过本文的学习,您应该能够更好地理解和应用Linux中的进程IO操作,提高系统编程的效率和能力。
77 34
|
8天前
|
Linux 网络安全 虚拟化
linux怎么把文件传到docker里面
在现代应用开发中,Docker作为流行的虚拟化工具,广泛应用于微服务架构。文件传输到Docker容器是常见需求。常用方法包括:1) `docker cp`命令直接复制文件;2) 使用`-v`选项挂载宿主机目录,实现数据持久化和实时同步;3) 通过SCP/FTP协议传输文件;4) 在Dockerfile中构建镜像时添加文件。选择合适的方法并确保网络安全是关键。
90 1
|
10天前
|
安全 Linux iOS开发
Gitea Enterprise 23.4.0 (Linux, macOS, Windows) - 本地部署的企业级 Gti 服务
Gitea Enterprise 23.4.0 (Linux, macOS, Windows) - 本地部署的企业级 Gti 服务
32 0
Gitea Enterprise 23.4.0 (Linux, macOS, Windows) - 本地部署的企业级 Gti 服务
|
24天前
|
Linux
Linux文件与目录的日常
目录的切换 一般使用(”pwd“)显示当前所在的目录 比如:当前目录是在home下面的,与用户名相同的文件夹,可以使用(”cd“)命令来切换目录; 进入下载目录(”cd home/a/下载“)这种从给目录开头的一长串路经”叫做绝对路径“; 进入图片目录(”cd .. /图片/“)".."代表当前路径的上级路径,相对于当前的目录而言的”叫做相对路径“,(”.“)代表当前路径; 如果,想快速切换,上一个所在目录可以(”cd - / cd..“); 如果,想快速切换,追原始的目录可以(”cd --“); 查看目录及文件
38 14
|
1月前
|
运维 应用服务中间件 nginx
docker运维查看指定应用log文件位置和名称
通过本文的方法,您可以更高效地管理和查看Docker容器中的日志文件,确保应用运行状态可控和可监测。
150 28
|
19天前
|
Linux Shell
Linux 将所有文件和目录名重命名为小写
Linux 将所有文件和目录名重命名为小写
25 3
|
2月前
|
Linux Docker 容器
Linux 中停止 Docker 服务报 warning 导致无法彻底停止问题如何解决?
在 Linux 系统中,停止 Docker 服务时遇到警告无法彻底停止的问题,可以通过系统管理工具停止服务、强制终止相关进程、检查系统资源和依赖关系、以及重置 Docker 环境来解决。通过以上步骤,能够有效地排查和解决 Docker 服务停止不彻底的问题,确保系统的稳定运行。
193 19