我正在寻找一个解决方案grep最新的日志文件(比如abc-20141028-005356.log),它被修改了。我可以使用命令“ls-1t | head-1”来完成这项工作,该命令给出了解决方案,但是日志文件夹中也有其他文件被修改。。我试着用一个头-2,它给出了2个文件,然后重新映射了文件。。。说“ls-1t | head-2 | grep abc-”。
真正的问题是,当我们拥有与最新数据相同的文件名&new time&date时。如何将最新的日志文件存储到一个具有相同重复文件名的变量中,并与日期和时间一起跟踪。

PATH=`cd $1 ; ls -1t | head -1`
LOGFILE=$1$PATH

$1=/opt/server1/日志/
敬请告知
我无法根据时间戳创建一个唯一的文件,例如abc-20141028-005356.log被修改了,下一个文件是abc-20141028-005358.log,这只是一个时间差。。但完整的文件名保持不变。
需要一个解决方案,其中我可以将最新修改的文件分配给一个变量,而不考虑文件名和时间戳(modified latest)
I have couple of files here

total 0
-rw-r--r-- 1 root root 0 Nov  4 11:14 abc-20141028-005348.log
-rw-r--r-- 1 root root 0 Nov  4 11:14 abc-20141028-005358.log
-rw-r--r-- 1 root root 0 Nov  4 11:19 abc-20141029-005358.log
-rw-r--r-- 1 root root 0 Nov  4 11:19 abc-20141029-005360.log

This is the latest file i get if i use the below

[root@abcd RKP]# ls -1t | head -1
abc-20141029-005360.log

The above can be done when there is only files starting with abcd-***. Now i have the below files

-rw-r--r-- 1 root root 0 Nov  4 11:14 abc-20141028-005348.log
-rw-r--r-- 1 root root 0 Nov  4 11:14 abc-20141028-005358.log
-rw-r--r-- 1 root root 0 Nov  4 11:19 abc-20141029-005358.log
-rw-r--r-- 1 root root 0 Nov  4 11:19 abc-20141029-005360.log
-rw-r--r-- 1 root root 0 Nov  4 11:21 EFG-20141028-005348.log
-rw-r--r-- 1 root root 0 Nov  4 11:21 EFG-20141028-005358.log
-rw-r--r-- 1 root root 0 Nov  4 11:21 EFG-20141029-005358.log

When i do the above command

[root@abcd RKP]# ls -1t | head -1
EFG-20141029-005358.log

I want only files which are modified on abcd-XXXXX.XXXX based on the last modfied time.

I'm sorry for making this so confused. Please let me know if you need anything more.

The solution may be simple, but I'm not getting how to do this. !!!!

谢谢你的努力。。。。
谢谢您。。。很有效。在我的脚本中运行上面的代码时,我得到了这个错误行30:[::需要整数表达式
知道我错过了什么吗。同一部分的脚本如下。
 if [ -e $POSFile ]; then
# Read last Position
lastPosition=`cat $POSFile`
fi
fileLength=`stat -c %s $LOGFILE`

if [ "$lastPosition" -gt "$fileLength" ]; then
# Log file rolled
lastPosition=0;
fi

difference=`expr $fileLength - $lastPosition`
# New Spot  To POSFile

    enter code here

echo $fileLength > $POSFile

最佳答案

要获取某些目录中以abc-开头的最新文件,请执行以下操作:

ls -t "$1"/abc-* | head -n1

如果没有与此模式匹配的文件,则会出现$1错误。在这种情况下,检查No such file or directory的值。一些有用的命令:
echo $1
ls "$1"
ls "$1"/abc-*

你文章中的小脚本片段应该是:
LOGFILE=$(ls -t "$1"/abc-* | head -n1)

我还注意到你试图将$1设置为某个值。结局可能不太好。shell中的PATH=变量是特殊的,它包含shell查找可执行文件的路径列表。对变量使用不同的名称,例如PATH就可以了(全部小写)。
关于你问题的第二部分:
第30行:[::需要整数表达式
这必须来自这一行:
if [ "$lastPosition" -gt "$fileLength" ]; then

path=的两边必须是整数才能起作用。添加一些-gt行来检查这些变量是否真的是数字,例如:
echo lastPosition=$lastPosition
echo fileLength=$fileLength
if [ "$lastPosition" -gt "$fileLength" ]; then

另外,最好用echo重写所有的`...`

关于linux - 获取最新的日志文件并存储在变量中,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26742671/

10-14 14:19
查看更多