diff
比較兩個文件或目錄的不同
diff test1 test2
diff test1 test2
test2 文件 test 後面有一個空格
-b
不檢查空格字符不同
diff -b test1 test2
-B
不檢查空白行
diff test1 test2
diff -B test1 test2
test2 文件必比 test1 文件多了一行空行
-c
顯示全部內容,並標出不同之處
diff -c test1 test2
test1 與 test2 時間戳不同
test2 多了 一行內容:123
-i
不檢查大小寫的不同
diff test1 test2
test1 內容 test
test2 內容第一行:tEst
第二行:123
diff -i test1 test2
只顯示 test2 不同,多了一行 123
忽略掉檢查大小寫
-q
僅顯示有無差異,不顯示詳細的信息
diff test1 test2
diff -q test1 test2
提示文件 test1 與 test2 有不同之處,不顯示不同之處
-r
比較子目錄中的文件
ls /d1 /d2
/d1/test1
/d2/test1
diff -r /d1/test1 /d2/test1
/d2/test1 比 /d1/test1 多了一行內容:123
/d2/test1 是大寫 S
/d1/test1 是小寫 s
-u
以合併的方式來顯示文件內容的不同
diff -u test1 test2
diff -u test1 test2 > test.path
test1 與 test2 不同之處,導入到 test.path (補丁)文件中
patch
用於不同文件之間打補丁
patch -b
yum install patch -y
patch -b test1 test.path
給 test1 文件打 test.path 補丁 使 test1 文件與 test2 文件相同
cat test1 test2
cut
多用於字符的截取
cut -d
指定分隔符
cut -f
指定截取的列
cut -c
指定截取的字符位置
實驗
截取 ifconfig eth0 命令顯示中的 ip :“172.25.254.330”
ifconfig eth0
ifconfig eth0 | head -n 2 | tail -n 1 | cut -c 14-28
頭部2行 | 尾部一行 | 截取第 14 至 28 字符
172.25.254.230
截取 /etc/passwd 文件中第二行的 “nologin”
cat passwd | head -n 2 | tail -n 1 | cut -d : -f 7 | cut -d / -f 3
頭部 2 行 | 尾部 1 行 | 截取以:分隔的第七列中的以 / 分隔的第三列
head -n 2 | tail -n 1 | cut -d : -f 1,3,5
頭部 2 行 | 尾部 1行 | 分別截取以 : 分隔的第一、三、五列
sort
用於字符排序
-n
純數字排序
sort -n test
-r
倒序
sort -r test
sort -rn test
-u
去掉重複數字
sort -u test
sort -nru test
-o
輸出到指定文件中
-t
指定分隔符
-k
指定要排序的列
sort -nr test | uniq -c | sort -t " " -k 2
指定每行顯示一次並且按照分隔符空格排序兩列
sort -nr test | uniq -c | sort -t " " -k 2 -r
指定每行顯示一次並且按照分隔符空格排序兩列並且倒序排列
sort -nr test | uniq -c | sort -t " " -k 2 -r | head -1
指定每行顯示一次並且按照分隔符空格排序兩列並且倒序排列抓取第一行
sort -nr test | uniq -c | sort -t " " -k 2 -r -o test1
指定每行顯示一次並且按照分隔符空格排序兩列並且倒序排列輸出到 test1 文件內
cat test1
uniq
對重複字符做相應處理
-u
顯示唯一的行
uniq -u test
-d
顯示重複的行
uniq -d test
-c
每行顯示一次並統計重複次數
uniq -c test
&& 和 ||
&&
用來執行條件成立後執行的命令
||
用來執行條件不成立後執行的命令
實驗
vim /ping.sh
ping -w1 -c1 $1 > /dev/null && echo "$1 is up" || echo "$1 is down"
sh /ping.sh 172.25.254.30
網絡可達:172.25.254.30 is up
sh /ping.sh 192.168.0.30
網絡不可達:192.168.0.30 is down
test
test
和[]
等同
test "$A" == "$B"
等同於[ "$A" == "$B" ]
[ "$A" = "$B" ]
A等於B
[ "$A" -eq "$B" ]
A等於B
[ "$A" != "$B" ]
A不等於B
[ "$A" -ne "$B" ]
A不等於B
[ "$A" -ge "$B" ]
A大於等於B
[ "$A" -gt "$B" ]
A大於B
[ "$A" -le "$B" ]
A小於等於B
[ "$A" -lt "$B" ]
A小於B
[ -z "$A" ]
字符串的長度爲空
[ -n "$A" ]
字符串的長度非空
test File1 –ef File2
兩個文件是否爲同一個文件,可用於硬連接,主要判斷兩個文件是否指向同一個inod
test File1 –nt File2
判斷文件1是否比文件2新
test File1 –ot File2
判斷文件1比是否文件2舊
Tr
轉換大小寫
實驗
查看 / cpu% 使用率
超過 30% 報警
報警信息導入到日誌
df | head -n 2 | tsil -n 1
vim logger.sh
sh logger.sh
cat /var/log/messages | tail -n 1
每分鐘監視一次 / cpu% 使用率
vim logger.sh
cat logger.sh
sh logger.sh
執行 job 任務
cat /var/log/messages | tail -n 1
實驗
vim /root/check_file.sh
[ -z “$1” ] && {
echo “Please input your filename”
exit
}
[ -e “$1” ] || {
echo “$1 is not exist”
exit
}
[ -f “$1” ] && {
echo “$1 is file”
exit
}
[ -d “$1” ] && {
echo “$1 is directory”
exit
}
[ -L"$1" ] && {
echo “$1 is softlinks”
exit
}
[ -S “$1” ] && {
echo “$1 is socket”
exit
}
[ -b “$1” ] && {
echo “$1 is block dev”
exit
}
[-c “$1” ] && {
echo “$1 is character dev”
exit
}
cat /root/check_file.sh
測試