10

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
  • 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, 2015 at 2:03

3 Answers 3

12

Try this:

ls -rt access.log* | xargs cat 

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

1
  • Great answer, solved my problem. I should learn to use xargs :)
    – insanebits
    Jul 17, 2015 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 )
2
  • 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, 2015 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.
    – FelixJN
    Jul 17, 2015 at 8:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .