是否有可能使grep命令输出文件path与文件修改date如下所示:
12-02-2015 /file/path/to/the/file 16-02-2015 /file/path/to/the/file 25-02-2015 /file/path/to/the/file 03-04-2015 /file/path/to/the/file
要么:
/file/path/to/the/file 12-02-2015 /file/path/to/the/file 12-02-2015 /file/path/to/the/file 12-02-2015 /file/path/to/the/file 12-02-2015
grep本身没有这个function。 但是你可以使用awk 。 使用该语法:
grep -Hr pattern . | awk -F: '{"stat -c %z "$1 | getline r; print r": "$0 }'
这迫使grep打印文件名-H 。 -r表示searchrecusive在给定的目录. 。 awk的字段分隔符被设置为: 。 第一个varibale $1现在包含文件名。 awk在每个文件名上调用stat -c %z ,这给出了可读格式的修改时间。 保存到每个search结果前面的variablesr中。
这是一个更容易理解的替代使用xargs
grep -rl pattern | xargs stat -c %n':'%z | awk -F: '{print $1}'