这似乎是一个微不足道的问题,但我没有找到解决方案。
我在Debian/Linux上的php安装中有许多日志文件,它们往往会增长相当多,我想每晚减少到最后500行左右。我如何做到这一点,可能是在shell中,并对*log应用命令?
发布于 2013-11-09 01:41:03
为此,我建议使用具有您喜欢的配置的logrotate
,而不是编写您自己的脚本。
发布于 2013-11-09 01:43:00
可能有一种更优雅的编程方法,但可以使用tail
和for循环来实现:
for file in *.log; do
tail -500 "$file" > "$file.tmp"
mv -- "$file.tmp" "$file"
done
发布于 2013-11-09 01:41:40
如果你想保存旧文件的历史记录,你应该查看logrotate。
否则,可以使用命令行轻松地完成此操作:
LOGS="/var/log"
MAX_LINES=500
find "$LOGS" -type f -name '*.log' -print0 | while read -d '' file; do
tmp=$(mktemp)
tail -n $MAX_LINES $file > $tmp
mv $tmp $file
done
https://stackoverflow.com/questions/19865055
复制相似问题