【发布时间】:2015-12-06 14:12:49
【问题描述】:
我在一个文件夹中有一堆日志文件。当我 cd 进入文件夹并查看文件时,它看起来像这样。
$ ls -lhat
-rw-r--r-- 1 root root 5.3K Sep 10 12:22 some_log_c48b72e8.log
-rw-r--r-- 1 root root 5.1M Sep 10 02:51 some_log_cebb6a28.log
-rw-r--r-- 1 root root 1.1K Aug 25 14:21 some_log_edc96130.log
-rw-r--r-- 1 root root 406K Aug 25 14:18 some_log_595c9c50.log
-rw-r--r-- 1 root root 65K Aug 24 16:00 some_log_36d179b3.log
-rw-r--r-- 1 root root 87K Aug 24 13:48 some_log_b29eb255.log
-rw-r--r-- 1 root root 13M Aug 22 11:55 some_log_eae54d84.log
-rw-r--r-- 1 root root 1.8M Aug 12 12:21 some_log_1aef4137.log
我想查看最新日志文件中的最新消息。我现在可以手动复制最新日志的名称,然后对其执行尾部操作,这样就可以了。
$ tail -n 100 some_log_c48b72e8.log
这确实涉及体力劳动,所以我想使用 bash-fu 来做这件事。
我目前找到了这种方法;
filename="$(ls -lat | sed -n 2p | tail -c 30)"; tail -n 100 $filename
它有效,但我很沮丧,我需要将数据保存到一个变量中才能做到这一点。是否可以在 bash 中执行此操作而不将中间结果保存到变量中?
【问题讨论】:
-
最好的解决方案是让您的记录器为日志文件使用基于时间戳的名称,并为最近的文件使用固定名称,如
current。 (或具有固定名称的符号链接到最新的。)