With zsh
:
mutt -s "Log" -a /path/to/*.log(.om[1]) [email protected]
That uses zsh
glob qualifiers. While other shell globs can only generate filenames based on their name, in zsh
, you can use those qualifiers ((.om[1])
above), to select based on file attributes (type, size, times, permissions...) or other criteria of your own, affect the order, apply various transformations, or select a range.
In this case we use these qualifiers:
.
: select only regular files.
om
: order by modification time (newest first)
[1]
: select only the first one.
Note that if there are no log
files in /path/to
, the glob will fail and the command will be aborted (as you'd expect, but other shells work differently).
GNUly, the equivalent would be something like:
latest=$(
find /path/to -maxdepth 1 -name '*.log' -printf '%T@:%p\0' |
sort -rzg | sed -z 's/^[^:]*://;q'
)
[ -n "$latest" ] &&
mutt -s "Log" -a "$latest" [email protected]