如何在 Linux 中清空(截断)日志文件如何在 Linux 中清空(截断)日志文件如何在 Linux 中清空(截断)日志文件如何在 Linux 中清空(截断)日志文件
  • 业务
  • 目标
  • 支持
  • 登录
找到的结果: {phrase} (显示: {results_count} 共: {results_count_total})
显示: {results_count} 共: {results_count_total}

加载更多搜索结果...

搜索范围
模糊匹配
搜索标题
搜索内容

如何在 Linux 中清空(截断)日志文件

发表 admin at 2025年2月28日
类别
  • 未分类
标签

在系统管理生命周期的某个时刻,您可能需要清空日志文件以节省系统磁盘空间或出于任何其他原因。有多种方法可以清空 Linux 系统中的文件。

使用 truncate 命令清空日志文件

在 Linux 中清空日志文件的最安全方法是使用 truncate 命令。截断命令用于将每个FILE的大小缩小或扩展至指定大小。

truncate -s 0 logfile

其中 -s 用于按 SIZE 字节设置或调整文件大小。 文件可以是相对于当前目录的路径,也可以是所提供文件的绝对路径。

如需完整的截断命令选项,请使用选项--help

$ truncate --help
Usage: truncate OPTION... FILE...
Shrink or extend the size of each FILE to the specified size

A FILE argument that does not exist is created.

If a FILE is larger than the specified size, the extra data is lost.
If a FILE is shorter, it is extended and the extended part (hole)
reads as zero bytes.

Mandatory arguments to long options are mandatory for short options too.
  -c, --no-create        do not create any files
  -o, --io-blocks        treat SIZE as number of IO blocks instead of bytes
  -r, --reference=RFILE  base size on RFILE
  -s, --size=SIZE        set or adjust the file size by SIZE bytes
      --help     display this help and exit
      --version  output version information and exit

The SIZE argument is an integer and optional unit (example: 10K is 10*1024).
Units are K,M,G,T,P,E,Z,Y (powers of 1024) or KB,MB,... (powers of 1000).

SIZE may also be prefixed by one of the following modifying characters:
'+' extend by, '-' reduce by, '<' at most, '>' at least,
'/' round down to multiple of, '%' round up to multiple of.

GNU coreutils online help: <https://www.gnu.org/software/coreutils/>
Full documentation at: <https://www.gnu.org/software/coreutils/truncate>
or available locally via: info '(coreutils) truncate invocation'

对于多个文件,您可以使用通配符,例如:

truncate -s 0 /var/log/*log

对于嵌套文件夹:

truncate -s 0 /var/log/**/*.log

或者使用 for 循环和截断:

for logfile in $(ls /var/log/*.log)
do 
  truncate -s 0 $logfile
done

使用 :> 或 true > 清空日志文件

您还可以使用 :> 清除文件内容。语法是

:> logfile

这相当于

true > logfile

请参阅下面的示例

使用 echo 命令清空日志文件

如果您对文件没有回显任何内容,它将清除内容以将其清空。

echo "" > logfile

这与

echo  > testfile

使用 dd 命令清空日志文件

使用 dd 命令的语法是

dd if=/dev/null of=logfile

或者

dd if=/dev/null > logfile

请参阅下面的示例

$ ls -l testfile 
-rw-r--r-- 1 jmutai jmutai 1338 Oct  2 23:07 testfile

$ [jmutai@arch tmp]$ ls -l testfile 
-rw-r--r-- 1 jmutai jmutai 1338 Oct  2 23:07 testfile

[jmutai@arch tmp]$ dd if=/dev/null of=testfile 
0+0 records in
0+0 records out
0 bytes copied, 0.000322652 s, 0.0 kB/s

[jmutai@arch tmp]$ ls -l testfile 
-rw-r--r-- 1 jmutai jmutai 0 Oct  2 23:33 testfile

对于多个文件,bash 中的一个简单循环就足够了。

for file in logfile1 logfile2 logfile2 ... ; do
    truncate -s 0 $file 
    #or
    dd if=/dev/null of=$file
    #or
    :>$file
done

使用 find 和 truncate 命令清空日志文件

您也可以使用 find 来查找目录中的所有 .log 文件并截断。

find /var/log -type f -iname '*.log' -print0 | xargs -0 truncate -s0

对于任何带有 log 关键字的文件:

find /var/log -type f -iname '*log' -print0 | xargs -0 truncate -s0

使用任一方法清空大型日志文件。

©2015-2025 Norria support@alaica.com