Operations 19 min read

18 Essential Shell Scripts for Everyday Linux Operations

This article compiles 18 practical Bash scripts covering tasks such as verifying file consistency across servers, scheduled log cleaning, network traffic monitoring, numeric analysis in files, FTP operations, Nginx error handling, process monitoring, bulk user management, iptables protection, and IP validation, each accompanied by clear code examples.

Linux Tech Enthusiast
Linux Tech Enthusiast
Linux Tech Enthusiast
18 Essential Shell Scripts for Everyday Linux Operations

1. Detect file consistency between two servers

The script computes MD5 checksums of all files under /data/web on the local machine and a remote host (IP 192.168.88.10), transfers the remote checksum file, and compares each entry to report changed, deleted, or missing files.

#!/bin/bash
#####################################
#检测两台服务器指定目录下的文件一致性
#####################################
#通过对比两台服务器上文件的md5值,达到检测一致性的目的
dir=/data/web
b_ip=192.168.88.10
#将指定目录下的文件全部遍历出来并作为md5sum命令的参数,进而得到所有文件的md5值,并写入到指定文件中
find $dir -type f|xargs md5sum > /tmp/md5_a.txt
ssh $b_ip "find $dir -type f|xargs md5sum > /tmp/md5_b.txt"
scp $b_ip:/tmp/md5_b.txt /tmp
#将文件名作为遍历对象进行一一比对
for f in `awk '{print $2}' /tmp/md5_a.txt`
do
    #以a机器为标准,当b机器不存在遍历对象中的文件时直接输出不存在的结果
    if grep -qw "$f" /tmp/md5_b.txt
    then
        md5_a=`grep -w "$f" /tmp/md5_a.txt|awk '{print $1}'`
        md5_b=`grep -w "$f" /tmp/md5_b.txt|awk '{print $1}'`
        #当文件存在时,如果md5值不一致则输出文件改变的结果
        if [ $md5_a != $md5_b ]
        then
            echo "$f changed."
        fi
    else
        echo "$f deleted."
    fi
done

2. Hourly log cleanup and size recording

Runs every hour; at midnight and noon it empties all files under /data/log without deleting them, otherwise it records each file’s size into a timestamped log file.

#!/bin/bash
################################################################
#每小时执行一次脚本(任务计划),当时间为0点或12点时,将目标目录下的所有文件内
#容清空,但不删除文件,其他时间则只统计各个文件的大小,一个文件一行,输出到以时#间和日期命名的文件中,需要考虑目标目录下二级、三级等子目录的文件
################################################################
logfile=/tmp/`date +%H-%F`.log
n=`date +%H`
if [ $n -eq 00 ] || [ $n -eq 12 ]
then
    #通过for循环,以find命令作为遍历条件,将目标目录下的所有文件进行遍历并做相应操作
    for i in `find /data/log/ -type f`
    do
        true > $i
    done
else
    for i in `find /data/log/ -type f`
    do
        du -sh $i >> $logfile
    done
fi

3. Monitor network interface traffic and log it

Continuously records inbound and outbound traffic of ens33 every minute, converting sar output from kB/s to bps.

#!/bin/bash
#######################################################
#检测网卡流量,并按规定格式记录在日志中
#######################################################
while :
 do
    LANG=en
    logfile=/tmp/`date +%d`.log
    #将下面执行的命令结果输出重定向到logfile日志中
    exec >> $logfile
    date +"%F %H:%M"
    #sar命令统计的流量单位为kb/s,日志格式为bps,因此要*1000*8
    sar -n DEV 1 59|grep Average|grep ens33|awk '{print $2,"\t","input:\t",$5*1000*8,"bps","
",$2,"\t","output:\t",$6*1000*8,"bps
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.

Linux Tech Enthusiast
Written by

Linux Tech Enthusiast

Focused on sharing practical Linux technology content, covering Linux fundamentals, applications, tools, as well as databases, operating systems, network security, and other technical knowledge.

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.