简体   繁体   English

如何使用sort命令但不使用ls -lrt命令列出文件

[英]How to list the files using sort command but not ls -lrt command

I am writing a shell script to check some parameters like errors or exception inside the log files which are getting generated in last 2 hours inside the directory /var/log. 我正在编写一个shell脚本来检查日志文件中的一些参数,如错误或异常,这些参数是在目录/ var / log中的最近2小时内生成的。 So this is command I am using: 所以这是我使用的命令:

find /var/log -mmin -120|xargs egrep -i "error|exception"

It is displaying the list of file names and its corresponding parameters(errors and exceptions) but the list of files are not as per the time sequence. 它显示文件名列表及其相应参数(错误和异常),但文件列表不按时间顺序排列。 I mean the output is something like this(the sequence): 我的意思是输出是这样的(序列):

/var/log/123.log:RPM returned error
/var/log/361.log:There is error in line 1
/var/log/4w1.log:Error in configuration line

But the sequence how these 3 log files have been generated is different. 但是这3个日志文件的生成顺序是不同的。

/var/log>ls -lrt
Dec24 1:19 361.log
Dec24 2:01 4w1.log
Dec24 2:15 123.log

So I want the output in the same sequence,I mean like this: 所以我想要输出相同的序列,我的意思是这样的:

/var/log/361.log:There is error in line 1
/var/log/4w1.log:Error in configuration line
/var/log/123.log:RPM returned error

I tried this: 我试过这个:

find /var/log -mmin -120|ls -ltr|xargs egrep -i "error|exception"

but it is not working. 但它不起作用。 Any help on this is really appreciated. 对此的任何帮助都非常感谢。

If your filenames don't have any special characters (like newline characters, etc), all you need is another call to xargs : 如果你的文件名没有任何特殊字符(比如换行符等),你只需要另外调用xargs

find . -type f -mmin -120 | xargs ls -tr | xargs egrep -i "error|exception"

Or if your filenames contain said special chars: 或者,如果您的文件名包含所述特殊字符:

find . -type f -mmin -120 -print0 | xargs -0 ls -tr | xargs egrep -i "error|exception"

You can prepend the modified time using the -printf argument to find , then sort , and then remove the modified time with sed : 您可以使用-printf参数添加修改时间,以find ,然后sort ,然后使用sed删除修改时间:

find /var/log -mmin -120 -printf '%T@:%p\n' | sort -V | sed -r 's/^[^:]+://' | xargs egrep -i "error|exception"
  • find ... -printf '%T@:%p\\n' prints out each found file ( %p ) prepended by the seconds since the UNIX epoch ( %T@ ; eg, 1419433217.1835886710 ) and a colon separator ( : ), each on a new line ( \\n ). find ... -printf '%T@:%p\\n'打印出每个找到的文件( %p通过从unix新纪元秒前缀)( %T@ ;例如, 1419433217.1835886710 )和结肠分离器( : ),每个都在一个新的行( \\n )。

  • sort -V sorts the files naturally by modification time because it is at the beginning of each line (eg, 1419433217.1835886710:path/to/the/file ). sort -V按修改时间自然地对文件sort -V排序,因为它位于每一行的开头(例如, 1419433217.1835886710:path/to/the/file )。

  • sed -r 's/^[^:]+://' takes each line in the format 123456789.1234:path/to/the/file and strips out the modification time leaving just the file path path/to/the/file sed -r 's/^[^:]+://'采用123456789.1234:path/to/the/file格式的每一行123456789.1234:path/to/the/file并删除修改时间,只留下文件路径path/to/the/file

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM