8

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?

3
  • 4
    Hi, I'm the author of ack. There is no such option, although you're not the first person to want it (including me). The reason that ack can't do that is that it would have to load up the entire list of files that it would search into memory at once, and then sort them all by date, and then go through all the files again. What if you had 1,000,000 possible files in a directory? You'd have to wait for all 1,000,000 files to get scanned and sorted before you even saw the first result, and you might even run out of memory before that happened. Oct 14, 2014 at 14:30
  • Thanks @AndyLester That makes sense. I wonder if saving the list under tmp or similar could help. That may not work for every environment, but I guess it could prevent that problem.
    – Josh
    Oct 14, 2014 at 14:48
  • @AndyLester It would be ok to collect all the results and only load those into memory (i.e. get file dates post-hoc). I struggle to understand why this would go beyond a modern computer's capability. If it is intended as a visual tool, is the case of visually inspecting 1,000,000 search hits really that realistic? Even if, it could just give you a note, for those cases where it really exceeds the max capabilities. May 15, 2021 at 0:34

2 Answers 2

8

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.

6

With GNU tools:

grep -H something * | awk -F: '{"date -r \""$1"\" +\"%F %R\"" | getline d; print d,$0}' | sort -n

Or,

ack something | awk -F: '{"date -r \""$1"\" +\"%F %R\"" | getline d; print d,$0}' | sort -n

This uses awk to separate out the file name from the usual grep output and awk calls date to retrieve the files date. grep is given the -H option to assure that a filename is shown on output even if only one appears on the command line.

As an example of the output:

$ grep setupcon /etc/init.d/* | awk -F: '{"date -r \""$1"\" +\"%F %R\"" | getline d; print d,$0}' | sort -n
2011-12-05 12:05 /etc/init.d/keymap.sh:# If setupcon is present, then we've been superseded by console-setup.
2011-12-05 12:05 /etc/init.d/keymap.sh:if type setupcon >/dev/null 2>&1; then
2011-12-08 18:00 /etc/init.d/console-setup:     if setupcon --save; then
2011-12-08 18:00 /etc/init.d/console-setup:test -f /bin/setupcon || exit 0
2012-04-28 02:41 /etc/init.d/kbd:if which setupcon >/dev/null
2012-09-30 18:02 /etc/init.d/keyboard-setup:    if setupcon -k 2>/dev/null; then
2012-09-30 18:02 /etc/init.d/keyboard-setup:test -f /bin/setupcon || exit 0

Explanation of awk commands

  • -F:

    This sets the field separator to : so that the first field is the file name. (We have to hope that the file name itself does not contain a colon.)

  • "date -r \""$1"\" +\"%F %R\"" | getline d

    This runs date -r filename +"%F %R" to get the file's modification date in a form suitable for sorting. The date is stored in the variable d.

  • print d,$0

    This prints the data followed by the line of output from grep.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .