Operations 3 min read

Shell Scripts for Batch Renaming .sh Files and Monitoring Port 80 Requests

This article provides two Bash scripts: one that recursively renames all ".sh" files to ".shell" and deletes their second line, and another that repeatedly checks the top‑20 IPs on port 80, reports activity when the smallest request count exceeds 500, or retries after 600 seconds.

Practical DevOps Architecture
Practical DevOps Architecture
Practical DevOps Architecture
Shell Scripts for Batch Renaming .sh Files and Monitoring Port 80 Requests

This guide presents two practical Bash scripts for Linux system administrators.

Task 1 – Rename and modify .sh files: The script searches the current directory (including sub‑directories) for files ending with .sh, renames each to .shell, and removes the second line of the renamed file.

#!/bin/bash
ALL_SH_FILE=$(find . -type f -name "*.sh")
for file in ${ALL_SH_FILE[*]}
do
    filename=$(echo $file | awk -F'.sh' '{print $1}')
    new_filename="${filename}.shell"
    mv "$file" "$new_filename"
    sed -i '2d' "$new_filename"
done

Task 2 – Monitor port 80 request volume: The script repeatedly obtains the request counts of the top‑20 IP addresses accessing port 80, checks whether the smallest count among them exceeds 500, and if so writes a system activity report to alert.txt; otherwise it waits 600 seconds before retrying.

#!/bin/bash
state="true"
while $state
do
    SMALL_REQUESTS=$(netstat -ant | awk -F'[ :]+' '/:22/{count[$4]++} END {for(ip in count) print count[ip]}' | sort -n | head -20 | head -1)
    if [ "$SMALL_REQUESTS" -gt 500 ]; then
        sar -A > alert.txt
        state="false"
    else
        sleep 600
        continue
    fi
done
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

LinuxBashNetwork Monitoringfile-rename
Practical DevOps Architecture
Written by

Practical DevOps Architecture

Hands‑on DevOps operations using Docker, K8s, Jenkins, and Ansible—empowering ops professionals to grow together through sharing, discussion, knowledge consolidation, and continuous improvement.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.