Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> Linux 系統教學 >> Linux 教學

Linux 運維必備:高頻命令與實戰技巧大彙總

編輯:Linux 教學

1. 查找並移動文件

find . -name "*.tar" -exec mv {} ./backup \;
# 延伸:刪除 30 天前、超過 100MB 的 .log 文件
find . -name "*.log" -mtime +30 -type f -size +100M | xargs rm -rf

2. 批量解壓縮

for i in $(find . -name "*.zip" -type f); do
    unzip -d "$i" /data/www/img/
done

3. sed 常用單行腳本

# 刪除行首字符
sed -i 's/^.//g' test.txt
# 行首插入 a
sed 's/^/a/g' test.txt
# 行尾插入 a
sed 's/$/a/g' test.txt
# 匹配行後追加 c
sed '/pattern/ac' test.txt
# 匹配行前插入 c
sed '/pattern/ic' test.txt
# 修改包含 SELINUX 的行:enforcing -> disabled
sed -i '/SELINUX/s/enforcing/disabled/' /etc/selinux/config

4. 目錄存在性判斷並創建

if [ ! -d /data/backup/ ]; then
    mkdir -p /data/backup/
else
    echo "The Directory already exists,please exit"
fi

5. 磁盤監控告警腳本(根分區 ≥90% 發郵件)

while sleep 5m; do
    for i in $(df -h | sed -n '/\/$/p' | awk '{print $5}' | sed 's/%//g'); do
        if [ $i -ge 90 ]; then
            echo "More than 90% Linux disk space, Please Check!" | \
            mail -s "Warn Linux / Parts is $i%" barlow@toxingwang.com
        fi
    done
done

6. Nginx 訪問日志 Top 20 IP

cat access.log | awk '{print $1}' | sort | uniq -c | sort -nr | head -20

7. 文件最大/最小值

# 正確做法:去除多余空格後排序,取首尾行
sed 's/  */ /g' a.txt | sort -nr | sed -n '1p;$p'

8. SNMP 抓取 Cacti 數據

snmpwalk -v2c -c public 192.168.0.241

9. 替換以 jk 結尾的行為 yz

sed -e 's/jk$/yz/g' b.txt

10. tcpdump 抓包範例

# 抓取 192.168.56.7 經 80 端口的包
tcpdump -nn host 192.168.56.7 and port 80
# 排除 192.168.0.22 的 80 端口
tcpdump -nn host 192.168.56.7 or ! host 192.168.0.22 and port 80

TCP/IP 七層模型:物理層 → 數據鏈路層 → 網絡層 → 傳輸層 → 會話層 → 表示層 → 應用層。

Copyright © Windows教程網 All Rights Reserved