Skip to content

Instantly share code, notes, and snippets.

@floriandejonckheere
Last active August 29, 2015 14:02
Show Gist options
  • Save floriandejonckheere/a519476c116e115ffd39 to your computer and use it in GitHub Desktop.
Save floriandejonckheere/a519476c116e115ffd39 to your computer and use it in GitHub Desktop.
Grep both compressed and uncompressed logs
#!/usr/bin/bash
#
# grep-logs - grep both compressed and uncompressed log files
#
if [[ "$1" == "-h" || ! $2 ]]; then
echo "Usage: $0 PATTERN LOG_FILE_BASE"
echo -e "\tPATTERN\t\tsee grep(1)"
echo -e "\tLOG_FILE_BASE\tuncompressed log file relative to /var/log"
echo -e "\n\tExample:\t$0 'denied access' nginx_access.log"
exit
fi
PATTERN=$1
shift
LOG_FILE_BASE=$1
shift
grep $@ "${PATTERN}" "/var/log/${LOG_FILE_BASE}" "/var/log/${LOG_FILE_BASE}.1"
# For some reason ls turns double quotes into single while still expanding variables but not globs
for LOG in $(ls /var/log/${LOG_FILE_BASE}.*.gz); do
zgrep $@ "${PATTERN}" "${LOG}"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment