8

I often use find-dired and find-named-dired.
But I wanted to get the results sorted by date and by size.

How can I do that?

nephewtom
  • 2,219
  • 17
  • 29

2 Answers2

3

After doing some googling for the Linux find parameters and looking at the find-dired function, I came with these two functions that seem to work.

Not sure if this is the optimal way, but at least they have the effect I desired:

(defun find-dired-by-date (dir args)
  (interactive (list (read-directory-name "Run find in directory: " nil "" t)
             (read-string "Run find (with args): " find-args
                  '(find-args-history . 1))))
  ;; Set to this value in order to get a find sorted by date
  (setq find-ls-option '("-exec ls -lt {} + | cut -d ' ' -f5-" . "-lt"))
  (find-dired dir args)
  (setq find-ls-option '("-ls" . "-dilsb")))

(defun find-dired-by-size (dir args)
  (interactive (list (read-directory-name "Run find in directory: " nil "" t)
             (read-string "Run find (with args): " find-args
                  '(find-args-history . 1))))
  ;; Set to this one to get it sorted by size
  (setq find-ls-option '("-exec ls -lSr {} + | cut -d ' ' -f5-" . "-lSr"))
  (find-dired dir args)
  (setq find-ls-option '("-ls" . "-dilsb")))
nephewtom
  • 2,219
  • 17
  • 29
2

If you use library find-dired+.el then you can do the date part using command find-time-dired:

find-time-dired is an interactive compiled Lisp function.

(find-time-dired DIR MIN-TIME MAX-TIME &optional DEPTH-LIMITS EXCLUDED-PATHS)

Find files in directory DIR newer or older than a timestamp.

The output is shown in a Dired buffer.

MIN-TIME is a format-time string parsable by parse-time-string, such as "2014-12-25 23:59:00". Only files newer than this are shown. If MIN-TIME is nil or a string matching regexp "^\s-*$", there is no lower time limit.

MAX-TIME is also a format-time string parsable by parse-time-string. Only files older than this time are shown. If MAX-TIME is nil or a string matching regexp "^\s-*$", the upper time limit is the current system time.

Optional arg DEPTH-LIMITS is a list (MIN-DEPTH MAX-DEPTH) of the minimum and maximum depths. If nil, search directory tree under DIR.

Optional arg EXCLUDED-PATHS is a list of strings that match paths to exclude from the search. If nil, search all directories.

If args DEPTH-LIMITS and EXCLUDED-PATHS are both non-nil then the command run is essentially the following:

find . -mindepth MIN-DEPTH -maxdepth MAX-DEPTH
       \( -path EXCLUDE1 -o -path EXCLUDE2 ... \)
       -prune -o \( -TIME-SWITCH -SINCE-MIN -TIME-SWITCH +SINCE-MAX \)
       LS-SWITCHES

where:

  • EXCLUDE1, EXCLUDE2... are the EXCLUDED-PATHS, but shell-quoted.
  • TIME-SWITCH is find-diredp-time-prefix concatenated with "min".
  • SINCE-MIN is the elapsed time since MIN-TIME in minutes.
  • SINCE-MAX is the elapsed time since MAX-TIME in minutes.
  • LS-SWITCHES is (car find-ls-option).

User option find-diredp-time-prefix determines whether to use the time of the last modification of a file (its contents) or the time of the last change of a file's status.

@nephewtom shows how to use find-dired to find by size (and time).

Drew
  • 75,699
  • 9
  • 109
  • 225