5

I need to do log analysis and I am facing problem that cat access.log* display latest log file first.

I tried to sort logs which takes a lot of time because I have logs from couple of years back. And they are already sorted and just displayed in different order than I need it to.

So I need to display files contents in folowing order:

access.log.4
access.log.3
....
access.log

How do I achieve that?

  • 1
    I can't believe that turnkey, unexplained and simple answers to almost non problem generate that many upvotes... No surprise that bash stuff are really trendy today. It's just really a shame that as a consequence, the quality and usefulness of the site as a whole plunges toward zero... – user123418 Jul 18 '15 at 2:03
8

Try this:

ls -rt access.log* | xargs cat 

First list the files from oldest to newest and then cat each one of them.

  • Great answer, solved my problem. I should learn to use xargs :) – insanebits Jul 17 '15 at 8:22
6

In zsh you can reverse the globbing order:

cat access.log.*(On)
~/test % ls
1  2  3
~/test % cat 1
1
~/test % cat 2
2
~/test % cat 3
3
~/test % cat *
1
2
3
~/test % cat *(On)
3
2
1
2
cat $( ls | tac )

or simply

cat $( ls -r )
  • 1
    ls -r does a reverse order by file name but it fails ordering numbers on names so it puts first access.log.9 than access.log.32 – jcbermu Jul 17 '15 at 8:36
  • If the number of digits is manageable, you could just do: cat $( ls -r access.log.?? ) $( ls -r access.log.? ) - but since it's logfiles ls -rt as @jcbermu used is applicable. – Fiximan Jul 17 '15 at 8:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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