BASH

Filter Web Server Access Logs by IP or Status Code

A Bash script to efficiently filter large web server access logs by specific IP addresses or HTTP status codes, useful for debugging and analysis.

#!/bin/bash

LOG_FILE="/var/log/nginx/access.log"
FILTER_IP="192.168.1.1"
FILTER_STATUS="404"

echo "Filtering logs for IP: $FILTER_IP and Status: $FILTER_STATUS"

grep "$FILTER_IP" "$LOG_FILE" | grep "HTTP/1.[01]" $FILTER_STATUS" | head -n 10
How it works: This script demonstrates how to filter web server access logs using `grep`. It first filters by a specified IP address and then pipes the output to another `grep` command to filter by a specific HTTP status code (e.g., 404 Not Found). The `head -n 10` limits the output to the first 10 matching lines for a quick preview. This is highly useful for identifying requests from specific sources or errors in large log files during web application debugging.

Need help integrating this into your project?

Our team of expert developers can help you build your custom application from scratch.

Hire DigitalCodeLabs