Skip to content

Instantly share code, notes, and snippets.

@andrewharvey
Created December 15, 2011 09:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrewharvey/1480519 to your computer and use it in GitHub Desktop.
Save andrewharvey/1480519 to your computer and use it in GitHub Desktop.
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
Copy link

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