Ack/Grep Search and Sort by Modification Date

ackfilesgrep

Is there any way to have ack sort the results found by date of modification? (ideally showing the date next to the result?). It doesn't look like ack has a date option, but just in case.

If this isn't possible with ack, how about grep or using a combination of tools?

Best Answer

Neither ack or grep have any notion of a file's modification dates. For that you'll need to generate the list of files first, and then sort them based on afterwards.

You can use xargs to run the output of either ack or grep into another command which will provide the modification dates. For the modification dates you can use stat to do that.

Example

$ grep -Rl awk * | xargs -n 1 stat --printf "%y ------ %n\n"
2013-11-12 10:06:16.000000000 -0500 ------ 100855/tst_ccmds.bash
2013-11-13 00:32:11.000000000 -0500 ------ 100911/cmd.bash
2013-11-23 03:16:17.000000000 -0500 ------ 102298/cmd.bash
2013-12-14 20:06:04.467708173 -0500 ------ 105159/cmd.txt
2013-12-16 03:20:48.166016538 -0500 ------ 105328/cmds.txt
2013-01-14 14:17:39.000000000 -0500 ------ 106932/red5-1.0.1.tar.gz

NOTE: This method will only show you the names of the files that matched your query along with the modification date.

Related Question