第七天 位置參數 變量運算if case || && find locate compress
環境變量賦值,聲明
export name=VALUE
declare -x name=VALUE
變量引用
${name} $name 花括號保存了變量完整性
查看環境變量
env
printenv
export
declare -x
刪除環境變量 unset name
這些都是位置變量
$1 腳本第一個參數
$2 腳本第二個參數
$* 腳本所有參數 但是這些參數看成1個整體
[email protected] 腳本所有參數,但這些參數分開排列
$0 腳本所在位置一般跟 basename $0
去做判斷名字在改變
$# 列出所有參數個數
$?前一個命令執行結果 成功失敗
echo $1
echo $2
echo $*
echo $?
echo $#
echo [email protected]
echo $0
[[email protected] script]#canshu.sh 11 22 33 44
11
22
11 22 33 44
0
4
11 22 33 44
/data/script/canshu.sh
set -- 清空所有位置變量
$1 $2 $3 換位置 shfit 把$1清掉$2變$1 每次只處理$1
第一個參數 什麽時候$1處理完成則處理完成,配合循環使用
echo "1st arg is $1"
echo "2st arg is $2"
echo "10st arg is ${10}"
[[email protected] script]#canshu.sh 11 22 33 44 55
11
22
33
44
55
11 22 33 44 55
0
5
11 22 33 44 55
/data/script/canshu.sh
22
33
44
55
22 33 44 55
0
4
22 33 44 55
shift
shift 2
刪除前2個參數
由於軟連接的文件名字不同對應 $0不同但是腳本相同 可以做不同工作 busybox就是這種設置
linkname=basename $0
if [ "$linkname" = "rm1" ] ; then
echo "rm1"
elif [ "$linkname" = "mv" ] ; then
echo "mv"
elif [ "$linkname" = "go1" ] ; then
echo "go1"
else echo "to"
fi
[[email protected] script]#./rm
rm1
[[email protected] script]#rm
mv: missing file operand
Try ‘mv --help‘ for more information.
alias別名優先級最高,其次PATH變量中最前邊腳本優先級最高
"rm" \rm ‘rm‘ 這些尋找的都是hash的路徑就是除了別名的腳本
可以用rm的絕對路徑刪除腳本變量
腳本一定要經過bash -x 測試查看完整過程分析執行哪裏出問題
軟連接$0值指向鏈接本身, 判斷$0是什麽用來執行不同命令
-z string 比較字符串為空
-n比較字符串不為空
[ "$1" ] 也可以這個樣比較
ln -s arg.sh cp
ln -s arg.sh rm
/usr/sbin/pidof
pidof 顯示進程編號
軟連接$0是什麽 去做什麽
readonly name 設置只讀變量
declare -r 設置只讀變量
readonly -p 查看
-i整數
-r只讀變量
-i to make NAMEs have the `integer‘ attribute
用狀態結果去判斷腳本執行成功與否 echo$?,狀態碼去判斷如網頁404
echo$? 0成功 非零失敗 1-255 前一個命令執行結果
ping -c1 -W1 172.22.0.1 > /dev/null
echo $?
終端echo $? 是 腳本最後一個命令執行結果是
exit 100 可以定義腳本執行結果
-
-
- % ** (乘方) / shell數字運算符
bc ^ (乘方)
- % ** (乘方) / shell數字運算符
-
i=20
j=10
let sum=$i+$j 數字運算
let sum=i+j
sum=$[i+j] 數字運算
sum=$((i+j))
expr 屬於命令運算 所以有些要加轉以符*
expr 2 + 8 命令數字運算
expr 2 * 2 認為通配符
expr i * 2 數字運算不識別字母
expr \ 2 前邊不能空
[[email protected] script]#expr 4 \< 5
1
[[email protected] script]#expr 4 \> 5
0
[[email protected] script]#expr * 5
expr: syntax error
[[email protected] script]#expr 4 \> 5
0
[[email protected] script]#echo $?
1
[[email protected] script]#expr 4 \< 5
1
[[email protected] script]#echo $?
0
expr 的結果和$?是相反的
declare 是整數 ,浮點數不行,字符串不行
-i to make NAMEs have the `integer‘ attribute是整數
declare -i var=i+j 聲明數值類型附加字符串就會出問題
比如i=haha
$[RANDOM%7+31]
i+=2 是i+2
i-= 是i-2
i/=2 i/2
i=2 i2
i++ i+1
[[email protected] script]#i=2
[[email protected] script]#let j+=5*i
[[email protected] script]#echo $j
10
先算乘法* 再算加等+=
[[email protected] script]#i=1
[[email protected] script]#j=1
[[email protected] script]#let j=i+=1
[[email protected] script]#echo $j
2
[[email protected] script]#echo $i
先是i+=1 然後j=i +=優先級比=高
[[email protected] script]#echo $j
8
[[email protected] script]#echo $i
8
[[email protected] script]#echo $p
2
[[email protected] script]#p=2
[[email protected] script]#let j=i+=p*=2
[[email protected] script]#echo $j $i $p
12 12 4
[[email protected] scri
右邊加等優先級最高
arg 變量的意思
let: let arg [arg ...]
let 命令支持 += -= /= ++ -- ++i
i+=10 i=i+10
i=10 i=i10
i=100
let j=i++;echo j=$j ,i=$i j=i i++
let j=++i;echo j=$j ,i=$i i++ j= (i++)
[[email protected] script]#i=0 ;let j=++i ; echo $j
1
[[email protected] script]#i=3 ;let j=++i ; echo $j
4
=++10i ????
[[email protected] script]#i=3 ;let j=++10i ; echo $j
-bash: let: j=++10i: value too great for base (error token is "10i")
4
這個公式不成立
[[email protected] script]#i=3 ;let j=++10i ; echo $j
30
j=++10 i
++10 = +10
[[email protected] script]#i=1 ;let j=++10i ; echo $j
10
[[email protected] script]#i=1 ;let j=10i++ ; echo $j
10
j=10i +
[[email protected] script]#i=1 ;let j=10++i ; echo $j
-bash: let: j=10++i: syntax error: operand expected (error token is "i")
10
[[email protected] script]#i=1 ;let j=10++i ; echo $j
10
[[email protected] script]#i=1 ;let j=10++i ; echo $j
20
先++i 在10* ++i
[[email protected] script]#
true false 兩個命令
1 0
echo$?
0 是true
1 是 false
1與1 1
1與0 0
0與0 0
0與1 0
1或1 1
1或0 1
0或1 1
0或0 0
& 二進制與
1100 12
1000 8
與
1000 8
[[email protected] ~]# a=$[12&2]
[[email protected] ~]#echo $a
0
[[email protected] ~]# a=$[12&8]
[[email protected] ~]#echo $a
| 二進制或
1100 12
1000 8
或
1100 12
[[email protected] ~]# a=$[12|8] ;echo $a
12
! 非 取反
[[email protected] ~]# a=$[!010] ;echo $a
0
[[email protected] ~]# a=12 ;b=$[!a] ;echo $b
0
[[email protected] ~]# a=3 ;b=$[!a] ;echo $b
0
[[email protected] ~]# a=3 ;b=$[!0] ;echo $b
1
[[email protected] ~]#b=$[!-1] ;echo $b
0
[[email protected] ~]#b=$[!-111] ;echo $b
0
所有運算數字(除0)取反都是零,而0取反則是1
異或 ^ 相同為假 不同為真 二進制中真假 異或2個數字可以得出第三個數字 這3個數字中2個數字都能得出第三個
1100 12
1000 8
亦或^
0100 4
[[email protected] ~]#a=$[1^2]
[[email protected] ~]#echo a
a
[[email protected] ~]#echo $a
3
[[email protected] ~]#a=$[1^2] ;echo $a ;b=$[a^2] ;echo $b ;a=$[a^1];echo $a
3
1
2
三個數異或 怎麽算
3個異或可以得到第四個 3個結合可以得到另一個數字
[[email protected] ~]#a=$[1^2^3];echo $a;b=$[0^2^3];echo $b;c=$[0^3^1];echo $c;d=$[0^2^1] ; echo $d
0
1
2
3
1=01 01
2=10
3=11 11
4=00 00
4 00 00
[[email protected] ~]#a=$[1^2^3^4];echo $a ;b=$[4^4^2^3];echo $b
4
1
同或 相同為真 不同為假 二進制數字運算不能交換字符串
01
10
11
a=20
b=3
tmp=$a
a=$b
b=$tmp
a=20;b=3 ;a=$[a^b];b=$[a^b];a=$[a^b]
echo $a , $b
短路與
cmd1|| cmd2
cmd1 && cmd2
10
test: test [expr]
expr 表達式
[[email protected] ~]#ehco aa &>/dev/null && echo bb
[[email protected] ~]#echo aa &>/dev/null && echo bb
bb
&&
假 && 第一個命令結果假 則 後邊沒有必要執行
真 && 假|真 第一個命令結果真則 則後邊有必要在看如果是真則真 ,是假則是假
短路或
真 || 如果第一個表達式是真 則後邊沒有必要
假 || 真|假 第一個結果假 則 則後邊有必要在看如果是真則真 ,是假則是假
COMMAND1 && COMMAND2
COMMAND1 || COMMAND2
-rw-r--r--. 1 root root 595 Mar 5 20:25 /etc/fstab
[[email protected] ~]#[ -w /etc/fstab ] && echo /etc/fstab is writale 1對則執行2
/etc/fstab is writale
[[email protected] ~]#[ -w /etc/fstab ] && echo /etc/fstab is writale || echo /etc/fstab is writale 1對則執行2 由於1,2都對則3不執行
/etc/fstab is writale
/etc/fstab is writale
[[email protected] ~]#[ -w /etc/fstab ] || echo /etc/fstab is writale && echo /etc/fstab is writale 1對則2不執行 由於1||2整體對則執行3
/etc/fstab is writale
echo f{1..100000} |xargs touch
如果通過管道加xarg在加命令格式 好處就是給命令傳輸多個參數, 並且可以超過命令本身限制參數
Usage: useradd [options] LOGIN
echo f{1..100000} |xargs useradd
useradd不支持後邊加多個參數
dick_used=df |grep "/dev/sd"| grep -oE "[0-9]+%" |tr -d % |sort -nr|head -n1
warn=
[ "$dick_used" -gt "$warn" ]
[ "$dick_used" -gt "$warn" -o "$inod_used" -gt "$warn" ]
test 判斷真假 等價 [ ]
help test
是0還是1取決於對表達式的評價,表達式通常檢查文件狀態
Exits with a status of 0 (true) or 1 (false) 取決於 echo $?
Exits with a status of 0 (true) or 1 (false) depending on
the evaluation of EXPR. Expressions may be unary or binary. Unary
expressions are often used to examine the status of a file. There
are string operators and numeric comparison operators as well.
變量寫法
i = 100 寫法不對
i=100
name2=mage
name=mage
test $name = $name
[[email protected] script]#name=mage
[[email protected] script]#name1=mage
[[email protected] script]#test $name = $name1
[[email protected] script]#echo $?
0
[[email protected] script]#name1=mag
[[email protected] script]#test $name = $name1
[[email protected] script]#echo $?
1
OS=cat /etc/redhat-release |grep -oE "[0-9]+"|head -n1
= 前後要有東西才能比字符串
[ "$a" = "$b" ] 字符串比較
== =~ 都是模糊匹配比較 ==通配符格式 =~ 正則表達式
正則表達式和通配符格式 不用加雙引號,但是前邊要加引號,表示字符串不能為空
[[email protected] script36]#a=f1.sh
[[email protected] script36]#[[ "$a" =~ .sh$ ]]
[[email protected] script36]#a=f1.sh
[[email protected] script36]#[[ "$a" == .sh ]]
[[email protected] script36]#echo $?
0
a=0
[[email protected] script36]#[[ $a == .sh ]]
[[email protected] script36]#echo $?
0
[[email protected] script]#a=f1.sh ; [[ "$a" =~ ".sh$" ]];echo $? 正則表達式不用加引號
1
[[email protected] script]#a=f1.sh ; [[ $a =~ .sh$ ]];echo $?
0
[[email protected] script]#a=f1.sh ; [[ $a == .sh ]];echo $?
0
[[email protected] script]#a=f1.sh ; [[ $a == ".sh" ]];echo $? 通配符表達式也不用加引號
1
-z 判斷字符串和$a為空
-n 判斷字符串不為空
[ -n "$var" ];echo $?
[ ] = [ -n $var ]
[[email protected] script36]#[ "" ];echo $?
1
[[email protected] script36]#[ ];echo $?
1
皆為空
[ x"$var" = ‘x‘ ];echo $? 這個方法簡直太好了很好的除去表達式不成立,或者為空風險,只能判斷字符串
[[email protected] script]#[ "x""$vvv" = "x" ] ;echo $?
0
[[email protected] script]#[ "x"$vvv = "x" ] ;echo $?
0
[[email protected] script]#[ x$vvv = x ] ;echo $?
0
[[email protected] script]#[ ‘x‘$vvv = ‘x‘ ] ;echo $?
0
test = [ ] 但是[ ] 比較直觀
[ ] 精確匹配
[[ ]] 模糊匹配 通配符 正則 [[ ]] 擴展正則表達式
arg1 OP arg2 Arithmetic tests. OP is one of -eq, -ne,
-lt, -le, -gt, or -ge.
[ ]判斷變量是不是大小
[[email protected] script]#[ 1 -gt 2 ] ;echo $?
1
n= ; [ "$num" ] 做比較應該先判斷有內容是不是空
[[email protected] script]#[ "$num" ] ; echo $?
1
[[email protected] script]#num=
[[email protected] script]#[ "$num" ] ; echo $?
1
[[email protected] script]#num=""
[[email protected] script]#[ "$num" ] ; echo $?
1
[[email protected] script]#num=‘‘
[[email protected] script]#[ "$num" ] ; echo $?
1
[[email protected] script]#
[[email protected] script]#n=-7 ; [ "$n" ] && [[ "$n" =~ ^[0-9]+$ ]]; echo $?
1
純正整數數字且不為空
n=111; [[ "$n" =~ ^[0-9]+$ ]];echo $? 純正整數數字且不為空
VAR變量的意思
-v VAR 是否被設置值 也就是是否設置和字符串不為空
unset name
[ -v $name ];echo $?
[[email protected] script]#unset nnn
[[email protected] script]#[ -v $nnn ] ;echo $? 變量沒有賦值則為正確
0
[[email protected] script]#[ -v $nnn ] ;echo $?
0
[[email protected] script]#nnn=111
[[email protected] script]#[ -v $nnn ] ;echo $? 變量被賦值則為錯誤
1
[[email protected] script]#nnn=
[[email protected] script]#[ -v $nnn ] ;echo $?
0
""自動給字符串賦值讓它不為空或者不賦值的狀態 -v正確表示應該不加""
[[email protected] script]#[ -v "" ] ;echo $?
1
[[email protected] script]#set |grep nnn=
[[email protected] script]#[ -v "$nnn" ] ;echo $?
1
[[email protected] script]#[ -v $nnn ] ;echo $?
0
[[email protected] script]#nnn=1
[[email protected] script]#[ -v $nnn ] ;echo $?
1
[[email protected] script]#[ -v "$nnn" ] ;echo $?
1
-a file -e file 判斷文件存在
-d file 目錄 指向結果是不是文件夾,首先應該判斷軟連接才知道
[ -L file ]
[ -r file ] 判斷所有者實際權限,或者所屬組權限 其他人權限
-f FILE True if file exists and is a regular file. 判斷普通文件,他指向軟連接結果
做文件處理加執行權限
chmod x $1 $2
[ "$#" -eq 0 ]
[ -L "$1" ]
[ -f "$1" ] && [[ "$1" =~ .sh$ ]] && chmod +x $1
短路與短路或最多3段 ,多了就復雜,自己都看不懂
grep -q no-such /
ping -W1 -c1
time ping -W1 -c1 命令查看命令執行時間
( exit 10 )小括號用 exit 10 在當前終端可以echo $?可以查到
echo $? 原理exit是最後一個子shell 或者當前shell命令執行結果
[[email protected] script]#(lslsl ;exit 10)
bash: lslsl: command not found...
[[email protected] script]#echo $?
10
[[email protected] script]#aaa ; ls ;echo $?
bash: aaa: command not found...
backup.sh ceshi1.sh downloadscp.sh f2.sh
canshu.sh dickcheck.sh f1.sh uploadingscp.sh
0
( umask 077 ; touch f3 )
touch f3
禁止普通用戶登錄 直接在etc下建 /etc/nologin 但是su可以切換
服務器維護網站維護用這個
2個文件就是雙目錄
file -ef file 硬軟連接
2個條件做與運算EXPR條件的意思
-a並且
-o或
[ -r file -a -w file ] ;echo $?
[ ! -r file -a -w file ] ;echo $?
判斷2個文件存在關系 [ ! -a file -a ! -a file ] = [ ! ( -a file -o -a file ) ]
[[email protected] ~]#ll /etc/login /etc/nologin
ls: cannot access /etc/nologin: No such file or directory
-rw-r--r-- 1 root root 0 Mar 20 12:39 /etc/login
[[email protected] ~]#[ -a /etc/login -a ! -a /etc/nologin ] ;echo $?
0
[[email protected] ~]#\rm -f /etc/login
[[email protected] ~]#[ ! -a /etc/login -a ! -a /etc/nologin ] ;echo $?
0
[[email protected] ~]#touch /etc/nologin
[[email protected] ~]#[ ! -a /etc/login -a -a /etc/nologin ] ;echo $?
0
[[email protected] ~]#touch /etc/login
[[email protected] ~]#[ -a /etc/login -a -a /etc/nologin ] ;echo $?
0
[[email protected] ~]#[ ! ( -a /etc/login -o -a /etc/nologin ) ] ;echo $?
1
[[email protected] ~]#\rm /etc/login /etc/nologin
[[email protected] ~]#[ ! ( -a /etc/login -o -a /etc/nologin ) ] ;echo $?
0
id $1 > /dev/null
-g file set-group-g-id sgid
-N 判斷 讀時間和修改時間誰久新否
help [
This is a synonym for the "test" builtin,
格式 [ "$name" = "$name" ]
version.sh
read 鍵盤讀入的內容
echo $REPLY變量存的 read付的內容
read NAME SEX
mage male
echo a b c | read x y z read可以做變量傳遞
echo a b c | { read x y z ; echo $x $y $z ; }
read x y 因為變量不匹配所以read只對一個變量賦值
1 2 3
echo $x
echo $y
read x y
123
read -p "請輸入:" a 提示輸入變量信息,比較直觀,read命令讓腳本具有交互功能
比如 rm -i color.txt
請你輸入yes/no
read -p "do you agree? (yes or no) : " a
[[ "$a" =~ ^[Nn][Oo]? ]] && { echo a111 ; exit ; }
-s 靜默輸入 比如passwd
-n 3 只能輸入3個字符
-d$ 指定$輸入結束符號
-t 3 3秒不輸入則退出
stty -echo 隱藏輸入操作
stty echo 鍵盤輸入顯示
比read更加通用
復雜條件判斷分支語句
真則執行 if中條件代碼不是則不執行 看$?
if可以在條件判斷中在嵌套if
單分支if ;then
條件代碼
fi
雙分支 if 條件判斷 ; then
條件代碼
elif 條件判斷 ; then
條件代碼
else 條件代碼
fi
read -p "成績" score
if [[ ! $score =~ 數字 ]] ; then
echo "輸入數字"
exit
elif [ $score -lt 60 ] ;then
echo "so so"
elif [ $score -lt 80 ] ;then
echo "so so"
elif [ $score -le 100 ] ;then
echo "so so"
else
echo " bug "
fi fi是if反寫
grep "stenm" .txt 維護狀態
否則意外宕機
雞兔同籠 35頭 94 腳
雞x
兔y
x+y
2x+4y
x+2y -(x+y) =y
read -p "" HEAD
read -p "" FOOT
RABBIT=$[FOOT/2-HEAD]
CHOOK=$[HEAD-RABBIT]
echo $RABBIT
echo $CHOOK
根據軟連接名稱去做什麽事情
fi --> rm 則 rm f1 -->cp 則cp
busybox繁忙盒子就是指向軟連接是什麽就做什麽功能
FILE=basename $0
if [ "$FILE" = "rm" ] ; then
echo rm
elif [ "$FILE" = "cp" ] ; then
echo cp
else exit
fi
PATH 別名
1 3 5 cmd1
2 4 6 cmd2
7 8 9 cmd 3
if []
]
case 散列值匹配
case esac
if fi 一起配合
help case
case WORD 關鍵字 in PATTREN通配符模式 ..多個 COMANDS ;;
case $num in
1|3|5)
cmd1
;;
2|4|6)
cmd2
;;
*)
cmd2
;;
esac
case 在多種場景下判斷簡單 因為匹配通配符模式,而且可以或
vim menu 1號菜單什麽菜2號什麽菜
cat << EOF
1:a
2:b
3:c
4:d
5:退出
EOF
table
read -p "number" menu
case $menu in
1|2)
echo 50元
;;
3)
echo 100元
;;
*)
exit
esac
如果用循環讓它一直點菜
1 2 3 可以寫通配符
id wang
echo $?
判斷id是否存在
所有字符判斷優先執行順序
最上邊優先級最高
bash
1展開1最優先是別名2是{ }括號三家目錄4命令5文件統配6重定向
1把命令拆成單詞
1別名替換
{ }替換
家目錄~替換
$( ) 和`` 替換
在展開
統配*,? [abc] 替換
< > 重定向替換
bash中
‘‘ 單引號防止所有擴展
""雙引號除
$ 變量
`` 命令
\ 轉以符
!歷史替換
vim
ls
hostname
history history命令在腳本不起作用
etc下影響所有
/etc/profile
/etc/profile.d/*.sh 推薦這個
/etc/bashrc
如果全局切換則 su - root 先是 /etc/profile /etc/profile.d/.sh
~/profile ./bashrc etc/bahsrc
如果局部切換則 su ckw 先是 /etc/profile.d/*.sh etc/bashrc
.bashrc
profile 配置文件,定義環境變量腳本命令 啟動程序
bashrc 別名函數 普通變量
生效方法 . 文件名 或者 source 文件 把腳本名讀一遍 ( source直接執行 不開啟子進程 )
bash 文件 則開啟子shell
.bash_logout 退出時執行操作的文件 刪除歷史信息
交互登錄輸入密碼
su - 用戶
cat .bash_profile
先調用bashrc
非交互登錄
su
執行腳本
bash實例
圖形界面
個人配置
~/bash_profile
~/.bashrc
$- 變量
echo $-
[[email protected] script36]#echo $-
himBH
h:hashall hash緩存功能
set +h 取消hash
i 判斷是不是交互shell
m 前後臺執行
B 支持花括號擴展
echo { a..z }
H 不能調用history 禁用歷史命令
help set
***建議這麽寫 基於安全策略
set -e
set -u
也可以set -ue
set -e 只要有命令出錯則後邊不執行
有錯不忘下走
-e 如果命令非0狀態則退出
set -u 沒有聲明(沒有賦值)就拿來用就是錯的,沒有賦值則認為錯誤,顯示信息
set -u
set -e
echo $var
echo continue
reset.sh
/etc/issue
/etc/motd
grep先吸收整個文件,在一個個查找
文件查找和壓縮
尋找文件 locate find
locate 中文名定位 數據庫搜索文件 ,基於文件索引數據庫搜索,
文件剛建立搜不到 locate 是模糊搜索 對系統性能影響極小
不能時使反應當前狀態
索引的構建在系統空閑時候進行
但是 索引構建過程遍歷整個文件系統極其消耗資源,
索引的構建是在系統較為空閑時自動進行(周期性任務),管理員手動更新數據庫
(updatedb)
?索引構建過程需要遍歷整個根文件系統,極消耗資源
[[email protected] test]#locate 111 |grep "/data"
[[email protected] test]#updatedb
[[email protected] test]#locate 111 |grep "/data"
/data/test/111
[[email protected] test]#ll /var/lib/mlocate/mlocate.db 不是開機立馬更新過一會更新
updatedb 更新索引數據庫命令
locate args.sh
locate 特點
速度快 因為搜索也有數據庫的文件索引速度快
模糊查找 因為搜的是包含111所以模糊查找
[[email protected] test]#locate -r "111"
/data/test/111
/home/1112aaa
/home/1112aaa/.bash_logout
/home/1112aaa/.bash_profile
/home/1112aaa/.bashrc
/home/1112aaa/.mozilla
/home/1112aaa/.mozilla/extensions
及時性不好 因為數據庫必須更新最新的,新建文件才能看到 (updatedb)
搜索文件全路徑
/data/test/111
可能只搜取用戶具備讀取和執行的目錄
[[email protected] test]#chmod 000 ../test
[[email protected] test]#ll .
[[email protected] ~]$locate -r "t/111$"
/root/111
[[email protected] test]#chmod 777 ../test
[[email protected] ~]$locate -r "t/111$"
/data/test/111
/root/111
locate KEYWORD 關鍵詞
-i 忽略大小寫
-n 只列出n各項目
-r 基本正則表達式
‘.conf$‘
[[email protected] test]#locate -r "t/1*$"
/data/test/111
/root/111
find 是遍歷文件目標路徑的,所以效率低 ,而且影響性能,而且也是看訪問權限
他屬於精確查找時時查找如window
查找條件 :文件名大小類型權限
find 執行效率低 ,影響系統性能 普通用戶看訪問權限 精確查找實時查找
find 支持通配符 -name *,? ,[] ,[^]
find 路徑 查找條件 處理動作
默認當前目錄 而且搜索遞歸
find -name "f*" 支持同配符 glob 但是要表示文件名全稱
-regex 支持正則表達式但是要表示文件名全稱
[[email protected] data]#find -name a
./script/argsnum.sh
[[email protected] test]#find -name 1
./111
可以指定深度 find -maxdepth 1 只是找1級子目錄
[[email protected] data]#find -mindepth 2 -name "1*"
./rm/123.link
./rm/123.link/11
./rm/11
./test/111
find -iname "1*" 不區分大小寫
find -inum 67 按i節點查找
67 drwxr-xr-x. 2 root root 4096 Mar 20 18:55 script
[[email protected] data]#find -inum "67"
./script
[[email protected] data]#find -maxdepth 5 -regex "..sh$"
./script/downloadscp.sh
主要是搜索最深和最短搜索會掐斷和截取一部分 最大搜索深度就是最大到哪,最小搜索深度就是從最小深度搜索
[[email protected] data]#find -mindepth 2 -name "1"
./rm/123.link
./rm/123.link/11
./rm/11
./test/111
最大搜索度 -maxdepth 2
最小搜索深度 -mindepth 1
指定在 幾層到幾層搜索
-depth 先處理文件在處理目錄 這樣可以方便看 . 之後 . /
[[email protected] test]#find -depth
./111
.
find 默認先處理目錄在處理文件 默認先處理目錄在處理文件 遍歷
find -name ".sh" 支持通配符
find -inum 67 支持插inode號查找
find -regex "..sh$" find通配符匹配整個路徑
*find -regex "." 必須是文件的全部路徑****
[[email protected] data]#find -maxdepth 5 -regex ".*.sh$"
./script/downloadscp.sh
./script/canshu.sh
find -user laowang -ls 用戶為wang文件列出
[[email protected] home]#find -user laowang -ls
202566453 0 drwx------ 2 laowang ccc 6 Mar 8 19:40 ./user
find -nouser 這個命令好可以查看沒有用戶的文件
[[email protected] home]#find -nouser
./abb
./abb/.mozilla
./abb/.mozilla/extensions
./abb/.mozilla/plugins
./abb/.bash_logout
find -nogroup 找沒有所有者所屬組文件
[[email protected] home]#find -nogroup
./1112aaa
./1112aaa/.mozilla
./1112aaa/.mozilla/extensions
./1112aaa/.mozilla/plugins
find -type TYPE 指定類型搜文件
find -type d 只搜文件夾
find -empty -ls
find -name "*.sh" -a -user root -ls
[[email protected] home]#find -name "*.sh" -a -user laowang
./laowang/bin/reset.sh
./laowang/reset.sh
find -name "*.sh" -o -user root -ls
非a 或 非b =非 (a 且 b)
非a 並且 非b = 非 (a 或者 b ) 則 除a或b
find -not ( -user root -a -name "f*" ) -ls
find ( -not -user root -o -not -name "f*" ) -ls
-prune 剪切
find /etc -path "/etc/sane.d" -a -prune -o -name "*.config"
過濾特定文件夾
"/etc/sane.d" -a -prune 就是找到這個文件夾並且剪切
查找/etc/下除/etc/sane.d目錄的其它所有.conf後綴的文件
-a -o (並且優先級高)
並且優先級比或者高
dd if=/dev/zero of=f1 bs=1 count=200 200b
dd if=/dev/zero of=f1 bs=1k count=200 200k
find -size 200k -size是模糊搜索 199k< 到<=200k文件
人默認 數字-1 1k 則是1k-1 0-1k
-size +1k
size -1
find -size -6k 0-6k
-atime +# [#, ) 等於#,或者大於#
-mmin -1 ( ,1]
-perm MODE 精準匹配權限
chmod 600 f*
find -perm 600 -ls -perm MODE 不加權限則精準匹配
[[email protected] test]#find -perm /777 -ls
33554688 0 drwxrwxrwx 2 root root 129 Mar 20 20:47 .
這個是最大權限只要在777裏就列出
[[email protected] f1]#ll
total 0
---x------ 1 root root 0 May 1 14:59 1
-rwx------ 1 root root 0 May 1 14:59 2
-rwxrwx--- 1 root root 0 May 1 14:59 3
---x--x--x 1 root root 0 May 1 14:59 4
[[email protected] f1]#find -perm /777 -ls 只要文件有rwxrwxrwx任意一個就列出
206738282 0 drwxr-xr-x 2 root root 42 May 1 14:59 .
206738283 0 ---x------ 1 root root 0 May 1 14:59 ./1
206738284 0 -rwx------ 1 root root 0 May 1 14:59 ./2
206738285 0 -rwxrwx--- 1 root root 0 May 1 14:59 ./3
206738286 0 ---x--x--x 1 root root 0 May 1 14:59 ./4
[[email protected] f1]#find -perm /222 -ls 只要文件有www 其中任意個w就列出
206738282 0 drwxr-xr-x 2 root root 42 May 1 14:59 .
206738284 0 -rwx------ 1 root root 0 May 1 14:59 ./2
206738285 0 -rwxrwx--- 1 root root 0 May 1 14:59 ./3
[[email protected] f1]#find -perm -111 -ls 3個都有xxx 才會列出
206738282 0 drwxr-xr-x 2 root root 42 May 1 14:59 .
206738286 0 ---x--x--x 1 root root 0 May 1 14:59 ./4
模糊匹配 0代表不關心
find -perm -222 -ls 3個人都有寫權限
find -perm -220 -ls 所有者所屬組必須是寫權限
/ centos 7 +是centos6
find -perm /222 -ls 只要有一個人有寫權限就可以
find -perm /222 -fls /root/find.log 搜索內容ls重訂向文件
find -perm /222 -type f -ok chmod a-w {} \; 取消所有寫權限 \;表示命令結束
-ok詢問
-exec 不用詢問改文件
3[[email protected] test]#find /var -mtime -7 -not ( -user root -o -user p
( -a ) 空格要有空隙
超級有用命令
echo f{1..5000000} |xargs touch xargs 把前邊的標準輸出作為這個命令的參數,且一個個傳過去
一切命令不支持標準輸入 ,但是可以用參數
echo user{1..10} |xargs useradd
ls | xargs rm 解決參數處理
/700 且有rwx???測試
只要文件在 rwx00 中顯示
查找當前目錄下.txt結尾文件通過xargs刪除 -0 和 -print0 相互對應代表分隔符
find -name "*.txt" -print0 |xargs -0 rm
壓縮
www.kernel.org
tar.sz
tar.gz
cd /boot
file-roller 文件 圖形化壓縮工具
compress 壓縮文件後綴加.Z 壓縮比高
壓縮是通過cpu運算做到的,會降低cpu性能,
某些場景是犧牲磁盤空間提示cpu 性能
壓縮算法
f1 .txt
magedu 100000
用m替換 少寫agedu 10000次
文本適合壓縮
dd if=/dev/zero of=f2 bs=1M count=1000
壓縮適合沒有被壓縮 或者文本 或者bmg圖片文件
uncompress 解壓縮 compress默認刪文件
-c 壓縮結果標準輸出,不刪除源文件
-v 詳情
-d 解壓縮
zcat m.z 接壓縮結果標準輸出,不刪除源文件
unmcompress mm
unmcompress mm.Z compress對文件後綴有要求
gz bz2 主流壓縮工具
第七天 位置參數 變量運算if case || && find locate compress