Created
December 15, 2011 09:34
-
-
Save andrewharvey/1480519 to your computer and use it in GitHub Desktop.
cat's a bunch of log rotate log files in correct order
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
for the sort command -V option is not present, In that case what needs to be done?