Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am trying to show all instances of a particular message from the syslog in chronological order by doing something like the following:

grep squiggle /var/log/messages*

Unfortunately the glob pattern matches the currently active file first. eg.


This means that recent messages show up first followed by the historical messages in chronological order.

Is it possible to adjust the glob pattern behaviour somehow to make the empty match (ie. just messages) show up at the end of the list?

If not, what would be a good way to address this problem?

share|improve this question
You could reverse output using tac: grep squiggle /var/log/messages* | tac... – pbm Mar 13 '12 at 10:17
up vote 4 down vote accepted

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.

share|improve this answer
Thanks for that. Been making the problem more complex than it needed to be! – Burhan Ali Mar 13 '12 at 12:28

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{-*,}
share|improve this answer
I'm increasingly impressed by zsh the more I read about it. – Burhan Ali Mar 14 '12 at 10:20

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

grep squiggle `ls -tr /var/log/messages*`
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.