最后用 grep 修改

最后用 grep 修改

我想将过去两个小时内修改的文件的列表添加到 stdout。使用 grep 的目录。它必须是自动的(不能询问用户现在是什么时间或类似的东西),并且我必须仅使用 grep,而不是条件语句。这就是我现在所做的:

#!/bin/bash

#redirect output to a file otherwise grep will read all as one line not giving a readable   output
ls -lrt > ls_out
date > date_extr

# extract hour from date. E interpret what follows as an extended regex. o print only what is matched. m 1 stop after one match
#I'll go back 2 hours
time_=$(date | grep -Eo [0-9]+?: | grep -Eom 1 [0-9]+?)
time_1=$[$time_-1]
time_2=$[$time_-2]

#grep day and month from the previous file "" to include spaces in date_
date_=$(grep -oE [0-9]+\ [A-Za-z]+\  date_extr)
grep "$date_" ls_out > ls_date

#match time as it is if it has 2 digits for example 11, 19, 21. \ number avoid matches as 19: if time_ is 9:
grep \ $time_:      ls_date
grep \ $time_1:     ls_date
grep \ $time_2:     ls_date

#match 09: if time_ output was 9:
grep 0$time_:      ls_date
grep 0$time_1:     ls_date
grep 0$time_2:     ls_date

#cleaning up
rm ls_out ls_date date_extr

它工作正常,唯一的问题是,如果脚本在凌晨 00 或 1 点运行,它不会列出前一天修改的文件

问题是 ii 应该 grep 日期和 ls -lrt 的输出,我似乎需要一个 if。

如果您问这个问题的原因,这是我经过一些修改后的链接: http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_04_05.html。问题 7。

答案1

使用find . -maxdepth 1 -mmin -120。如果您绝对必须使用,grep可以 grep 查找空字符串:grep -l '' $(find . -maxdepth 1 -mmin -120)

相关内容