5

How to concatenate the rotated logs back together to make the original file?

huali-access.log     huali-access.log.15  huali-access.log.21  huali-access.log.28  huali-access.log.34  huali-access.log.40  huali-access.log.47  huali-access.log.6
huali-access.log.1   huali-access.log.16  huali-access.log.22  huali-access.log.29  huali-access.log.35  huali-access.log.41  huali-access.log.48  huali-access.log.7
huali-access.log.10  huali-access.log.17  huali-access.log.23  huali-access.log.3   huali-access.log.36  huali-access.log.42  huali-access.log.49  huali-access.log.8
huali-access.log.11  huali-access.log.18  huali-access.log.24  huali-access.log.30  huali-access.log.37  huali-access.log.43  huali-access.log.5   huali-access.log.9
huali-access.log.12  huali-access.log.19  huali-access.log.25  huali-access.log.31  huali-access.log.38  huali-access.log.44  huali-access.log.50
huali-access.log.13  huali-access.log.2   huali-access.log.26  huali-access.log.32  huali-access.log.39  huali-access.log.45  huali-access.log.51
huali-access.log.14  huali-access.log.20  huali-access.log.27  huali-access.log.33  huali-access.log.4   huali-access.log.46  huali-access.log.52
Ryan
  • 125
  • 7
steveyang
  • 673
  • 4
  • 10
  • 16

5 Answers5

9

If the files have the correct modification times set (e.g. you did not copy them around without taking care of preserving the modification times), you can use

 cat $(ls -t huali-access.log*) > output.log

The -t option in ls will sort it by modification time.

Felix
  • 119
  • 5
Daniel t.
  • 9,291
  • 1
  • 33
  • 36
  • cat $(ls -t -r huali-access.log*) > output.log to reverse the file order. Oldest file first so that the chronological order is maintained on the output.log. – teroi Aug 09 '19 at 09:10
6

like this:

cat huali-access.log* > merged-huali-access.log

or to be sure its chronologically in order:

echo -n "" > merged-huali-access.log # creating new file and making sure its empty
for i in {1..52}
do
    cat huali-access.log.${i} >> merged-huali-access.log
done
cat huali-access.log >> merged-huali-access.log
replay
  • 3,240
  • 14
  • 17
0

From @mauro.stettler, Fixed issue with order of files, also made it generic:

for LOG in *.log; do 
    ( for i in {100..1}; do 
       F=${LOG}.${i}; 
       [ -e $F ] && cat $F; 
    done ; cat ${LOG} ) > aggregated_${LOG};
done

Or you can use on accesslogs, not so generic as the other that you can apply to any logs, and will not work with among different months:

cat accesslog.log* | sort -nk 4 > aggregated_accesslog.log
PMG
  • 23
  • 1
  • 5
0

This will concatenate all log* files (including gzipped) into log.all Just replace "log" to use it

l='log'; test -f ${l}.all && rm ${l}.all; ls -1tr ${l}* | xargs zcat -f >> $l.all
Whitehat
  • 331
  • 2
  • 2
-1
ls -1t *.access.log* | xargs zcat >  access.all.log
user3721740
  • 107
  • 1
  • 4