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
  • What is your final purpose? – quanta Feb 20 '13 at 3:40

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}
    cat huali-access.log.${i} >> merged-huali-access.log
cat huali-access.log >> merged-huali-access.log
  • Why do you have the 'echo -n ""' bit there? That is superfluous. The concatenation ">>" operation will create an empty file just as done by ">". – Daniel Mar 5 '15 at 21:53
  • 1
    @Daniel If the file is already there, it won't empty the file. – ianweller Mar 5 '15 at 22:45
  • @ianweller ... ack ... too true. – Daniel Mar 6 '15 at 0:54

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.

  • 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 9 '19 at 9:10

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

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

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

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
ls -1t *.access.log* | xargs zcat >  access.all.log

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.