简体   繁体   English

Bash:如何解析包含 ls -ltr 的 output 的日志文件,使用 bash 脚本提取在特定时间之前修改的文件名

[英]Bash: How to parse a log file that contains the output of ls -ltr using bash script to extract the file names that are modified before specific time

I am trying to get all the files in SFTP directories that were last modified before 24 hours using Unix bash script.我正在尝试使用 Unix bash 脚本获取 SFTP 目录中在 24 小时之前最后修改的所有文件。 Please note that lftp option is not supported in my environment.请注意,我的环境不支持 lftp 选项。 So, I have created a script that will list all the sub directories of the SFTP directory to a local file, then I have used a loop to traverse all those sub directories names, printed the content of the directory using ls -ltr and directed that output to a local file.因此,我创建了一个脚本,将 SFTP 目录的所有子目录列出到本地文件,然后我使用循环遍历所有这些子目录名称,使用 ls -ltr 打印目录的内容并指示output 到本地文件。

Now I have a file with below content and let's assume that the current time is Oct 28th 22:32.现在我有一个包含以下内容的文件,假设当前时间是 10 月 28 日 22:32。

-rw------- 1 200      100             1930 Oct 25 08:31 File1
-rw------- 1 200      100              280 Oct 25 11:32 File2
-rw------- 1 200      100              280 Oct 25 12:17 File3
-rw------- 1 200      100              280 Oct 25 22:31 File4
-rw------- 1 200      100              280 Oct 26 22:32 File5
-rw------- 1 200      100              280 Oct 27 22:31 File6
-rw------- 1 200      100              280 Oct 16 09:59 File7
-rw------- 1 200      100              280 Oct 16 09:59 File8
-rw------- 1 200      100              280 Oct 16 10:00 File9

Is it possible to get all the filenames and their last modified time stamps with last modified date < sysdate - 24 hours?是否可以获得所有文件名及其最后修改的时间戳,最后修改日期 < sysdate - 24 小时?

For automation, I strongly suggest using 'lftp'.对于自动化,我强烈建议使用“lftp”。 The 'sftp' is a simple interface, appropriate for interactive usage (eg, no status code for failures, etc.). 'sftp' 是一个简单的接口,适合交互使用(例如,没有失败的状态码等)。

The 'lftp' has lot of options beyond 'sftp'. 'lftp' 有很多超越 'sftp' 的选项。 In particular, it has 'client side' listing (cls):特别是,它具有“客户端”列表(cls):

sftp
open sftp://host/path
user user password
cls -l --time-style='%Y-%m-%d %H:%M:%S'
Output:
drwxr-xr-x    2 root     root         4096 2019-10-13 16:01:46 /bin/
drwxr-xr-x    3 root     root         4096 2019-06-30 08:07:02 /boot/
drwxr-xr-x    2 root     root         4096 2019-06-09 19:36:33 /cdrom/
drwxr-xr-x   18 root     root         3960 2019-10-19 08:08:01 /dev/
drwxr-xr-x  150 root     root        12288 2019-10-20 15:49:39 /etc/
drwxr-xr-x    4 root     root         4096 2019-06-09 23:18:04 /home/

If you are looking for cutoff, you can also sort by time (I believe --sort=date , and optioanlly reverse -r ), and stop processing once you match the date range.如果您正在寻找截止日期,您还可以按时间排序(我相信--sort=date和 optioanlly reverse -r ),并在匹配日期范围后停止处理。

Side note: sftp has a limited 'find' command.旁注:sftp 有一个有限的“查找”命令。 Unfortunately, it does not offer filtering by date.不幸的是,它不提供按日期过滤。

Most other 'sftp' clients that I've tried were using the default LS format.我尝试过的大多数其他“sftp”客户端都使用默认的 LS 格式。 The exception are the SFTP modules for Perl, Python, which provide the client with the date in "Unix" format, allowing any test.例外的是 Perl、Python 的 SFTP 模块,它们为客户端提供“Unix”格式的日期,允许任何测试。 However, this is significantly more work than using lftp .但是,这比使用lftp的工作要多得多。

I ended up parsing the log file line by line, extracted the last modified time to a variable and file name to another variable.我最终逐行解析日志文件,将上次修改时间提取到一个变量并将文件名提取到另一个变量。 Then I converted the last modified time string to Unix time format and then to epoch number.然后我将最后修改的时间字符串转换为 Unix 时间格式,然后转换为纪元数。 Similarly I got the epoch number for sysdate-1.同样,我得到了 sysdate-1 的纪元。 Finally I compared those two values and filtered the file names matched my criteria.最后我比较了这两个值并过滤了符合我标准的文件名。 code snippet is given below代码片段如下

prev_day_time=$(date +%s -d "$(date --date='1 day ago')")
file_mod_time=$(date +%s -d "$(date -d "$(echo $(echo $files_list | awk '{print $1}') | sed -e 's/\(.....\)/\1 /')")")

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

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