149 Essential Shell Scripts for Sysadmins – Keep Them Handy
This article provides a curated collection of 149 practical Bash scripts for Linux system administrators, covering common tasks such as locating zombie processes, removing empty files, calculating sums and ranges, formatting dates, retrieving MAC addresses, checking leap years, sorting numbers, and more, with each example presented as ready‑to‑run code.
The author shares a selection of useful Bash scripts that can help Linux system administrators automate repetitive tasks and improve efficiency. The full set contains 149 scripts across 68 pages, searchable by keyword; the article shows a representative subset.
1. Find zombie processes
#!/bin/bash
# 查找 Linux 系统中的僵尸进程
# awk 判断 ps 命令输出的第 8 列为 Z 是,显示该进程的 PID 和进程命令
ps aux | awk '{if($8 == "Z"){print $2,$11}}'2. Delete zero‑size files in a directory
#!/bin/bash
# 删除某个目录下大小为 0 的文件
#/var/www/html 为测试目录,脚本会清空该目录下所有 0 字节的文件
dir="/var/www/html"
find $dir -type f -size 0 -exec rm -rf {} \;3. Read five numbers (<100) and compute sum, min, max
#!/bin/bash
COUNT=1
SUM=0
MIN=0
MAX=100
while [ $COUNT -le 5 ]; do
read -p "请输入1-10个整数:" INT
if [[ ! $INT =~ ^[0-9]+$ ]]; then
echo "输入必须是整数!"
exit 1
elif [[ $INT -gt 100 ]]; then
echo "输入必须是100以内!"
exit 1
fi
SUM=$((SUM+INT))
[ $MIN -lt $INT ] && MIN=$INT
[ $MAX -gt $INT ] && MAX=$INT
let COUNT++
done
echo "SUM: $SUM"
echo "MIN: $MIN"
echo "MAX: $MAX"4. Print various date/time formats
#!/bin/bash
# 打印各种时间格式
echo "显示星期简称(如:Sun)"
date +%a
echo "显示星期全称(如:Sunday)"
date +%A
echo "显示月份简称(如:Jan)"
date +%b
echo "显示月份全称(如:January)"
date +%B
echo "显示数字月份(如:12)"
date +%m
echo "显示数字日期(如:01)"
date +%d
echo "显示数字年(如:2023)"
date +%Y
echo "显示年‑月‑日"
date +%F
echo "显示小时(24 小时制)"
date +%H
echo "显示分钟(00..59)"
date +%M
echo "显示秒"
date +%S
echo "显示纳秒"
date +%N
echo "组合显示"
date +"%Y%m%d %H:%M:%S"5. Check if a file or directory exists
#!/bin/bash
# 判断文件或目录是否存在
if [ $# -eq 0 ]; then
echo "未输入任何参数,请输入参数"
echo "用法: $0 [文件名|目录名]"
fi
if [ -f $1 ]; then
echo "该文件,存在"
ls -l $1
else
echo "没有该文件"
fi
if [ -d $2 ]; then
echo "该目录,存在"
ls -ld $2
else
echo "没有该目录"
fi6. Sort three input integers
#!/bin/bash
# 依次提示用户输入 3 个整数,脚本根据数字大小依次排序输出 3 个数字
read -p " 请输⼊⼀个整数:" num1
read -p " 请输⼊⼀个整数:" num2
read -p " 请输⼊⼀个整数: " num3
tmp=0
if [ $num1 -gt $num2 ]; then
tmp=$num1
num1=$num2
num2=$tmp
fi
if [ $num1 -gt $num3 ]; then
tmp=$num1
num1=$num3
num3=$tmp
fi
if [ $num2 -gt $num3 ]; then
tmp=$num2
num2=$num3
num3=$tmp
fi
echo "排序后数据(从⼩到⼤)为: $num1, $num2, $num3"7. Get local MAC address
#!/bin/bash
# 获取本机 MAC 地址
ip a s | awk 'BEGIN{print " 本 机 MAC 地 址 信 息 如 下 :"}/^[0-9]/{print $2;getline;if($0~/link\/ether/){print $2}}' | grep -v lo:8. Determine if a year is a leap year
#!/bin/bash
# 提示用户输入年份后判断该年是否为闰年
# 能被4整除并且不能被100整除的年份是闰年
# 能被400整除的年份也是闰年
read -p "请输入一个年份:" year
if [ "$year" = "" ]; then
echo "没有输入年份"
exit 1
fi
if [[ "$year" =~ [a‑Z] ]]; then
echo "你输入的不是数字"
exit 1
fi
if [ $((year % 4)) -eq 0 ] && [ $((year % 100)) -ne 0 ]; then
echo "$year 年是闰年"
elif [ $((year % 400)) -eq 0 ]; then
echo "$year 年是闰年"
else
echo "$year 年不是闰年"
fiThese examples illustrate typical sysadmin tasks and can be copied directly into a Bash environment. The full collection includes many more scripts covering file manipulation, system monitoring, network queries, and automation patterns useful for daily operations.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Golang Shines
We share daily the latest Golang technical articles, practical resources, language news, tutorials, and real-world projects to help everyone learn and improve.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
