Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
cat's a bunch of log rotate log files in correct order
#!/bin/bash
# using,
#
# ./logrotatecat access.log
#
# inside a directory of log rotate logs in a form like
#
# access.log
# access.log.1
# access.log.2.gz
# ...
#
# do a cat on all the files in order from oldest at the top
# to newest at the bottom
# To the extent possible under law, the person who associated CC0
# with this work has waived all copyright and related or neighboring
# rights to this work.
# http://creativecommons.org/publicdomain/zero/1.0/
type="$1"
# glob access files in the current directory
for f in `ls ${type}* | sort -V` ; do
log_files=("${log_files[@]}" "$f")
done
# reverse the order
for i in $(seq ${#log_files[@]} -1 0) ; do
if [[ $i != ${#log_files[@]} ]] ; then
f=${log_files[$i]}
# cat the file
if [[ $f == ${type}.*.gz ]] ; then
zcat $f
elif [[ $f == ${type}.*.bz2 ]] ; then
bzcat $f
else
cat $f
fi
fi
done
@arunkvnss

This comment has been minimized.

Copy link

@arunkvnss arunkvnss commented Aug 17, 2017

for the sort command -V option is not present, In that case what needs to be done?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment