Is it possible to change the order of a glob?

In zsh, you can control the order of matches (among other things) with a glob qualifier.

echo /var/log/messages*        # usual lexicographic order
echo /var/log/messages*(On)    # reverse lexicographic order
echo /var/log/messages*(om)    # reverse chronological order (ascending mtime)
echo /var/log/messages*(Om)    # chronological order order (descending mtime)

(See the manual for more possibilities.) You can even define your own sorting order by supplying a comparison function in recent versions, with oe or o+.

Here, the proper order of files is chronological order. You can emulate it easily based on the name, though, and that works even in bash:

grep squiggle /var/log/messages{-*,}

I don't know of a way to change the globbing order, but there's an easy workaround for your case:

grep squiggle /var/log/messages-* /var/log/messages

i.e. don't match the messages files in your glob pattern, and add it to the end of grep's argument list.


You can use backticks combined with ls -tr (sort by mod time and in reverse) like this:

grep squiggle `ls -tr /var/log/messages*`

Tags:

Bash

Wildcards