第七天 位置参数 变量运算if case || && find locate compress

export name=VALUE
环境变量赋值,声明
export name=VALUE
declare -x name=VALUE

变量引用
${name} $name 花括号保存了变量完整性

查看环境变量
env
printenv
export
declare -x

删除环境变量 unset name

这些都是位置变量
$1 脚本第一个参数
$2 脚本第二个参数
$* 脚本所有参数 但是这些参数看成1个整体
$@ 脚本所有参数,但这些参数分开排列
$0 脚本所在位置一般跟 basename $0 去做判断名字在改变
$# 列出所有参数个数

$?前一个命令执行结果 成功失败

echo $1
echo $2
echo $*
echo $?
echo $#
echo $@
echo $0

[root@centos7 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 "3st arg is $3"
echo "10st arg is ${10}"

[root@centos7 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

扫描二维码关注公众号,回复: 6084929 查看本文章

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

[root@centos7 script]#./rm
rm1
[root@centos7 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 ^ (乘方)

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 前边不能空

[root@centos7 script]#expr 4 \< 5
1
[root@centos7 script]#expr 4 \> 5
0

[root@centos7 script]#expr * 5
expr: syntax error

[root@centos7 script]#expr 4 \> 5
0
[root@centos7 script]#echo $?
1
[root@centos7 script]#expr 4 \< 5
1
[root@centos7 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

[root@centos7 script]#i=2
[root@centos7 script]#let j+=5*i
[root@centos7 script]#echo $j
10

先算乘法* 再算加等+=

[root@centos7 script]#i=1
[root@centos7 script]#j=1
[root@centos7 script]#let j=i+=1
[root@centos7 script]#echo $j
2
[root@centos7 script]#echo $i

先是i+=1 然后j=i +=优先级比=高
[root@centos7 script]#echo $j
8
[root@centos7 script]#echo $i
8
[root@centos7 script]#echo $p
2
[root@centos7 script]#p=2
[root@centos7 script]#let j=i+=p*=2
[root@centos7 script]#echo $j $i $p
12 12 4
[root@centos7 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++)

[root@centos7 script]#i=0 ;let j=++i ; echo $j
1
[root@centos7 script]#i=3 ;let j=++i ; echo $j
4
=++10i ????
[root@centos7 script]#i=3 ;let j=++10i ; echo $j
-bash: let: j=++10i: value too great for base (error token is "10i")
4

这个公式不成立

[root@centos7 script]#i=3 ;let j=++10i ; echo $j
30
j=++10
i
++10 = +10
[root@centos7 script]#i=1 ;let j=++10i ; echo $j
10
[root@centos7 script]#i=1 ;let j=10
i++ ; echo $j
10
j=10i +
[root@centos7 script]#i=1 ;let j=10++
i ; echo $j
-bash: let: j=10++i: syntax error: operand expected (error token is "i")
10
[root@centos7 script]#i=1 ;let j=10++i ; echo $j
10
[root@centos7 script]#i=1 ;let j=10
++i ; echo $j
20

先++i 在10* ++i

[root@centos7 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

[root@centos7 ~]# a=$[12&2]
[root@centos7 ~]#echo $a
0
[root@centos7 ~]# a=$[12&8]
[root@centos7 ~]#echo $a

| 二进制或
1100 12
1000 8

1100 12
[root@centos7 ~]# a=$[12|8] ;echo $a
12
! 非 取反

[root@centos7 ~]# a=$[!010] ;echo $a
0
[root@centos7 ~]# a=12 ;b=$[!a] ;echo $b
0
[root@centos7 ~]# a=3 ;b=$[!a] ;echo $b
0

[root@centos7 ~]# a=3 ;b=$[!0] ;echo $b
1
[root@centos7 ~]#b=$[!-1] ;echo $b
0
[root@centos7 ~]#b=$[!-111] ;echo $b
0
所有运算数字(除0)取反都是零,而0取反则是1

异或 ^ 相同为假 不同为真 二进制中真假 异或2个数字可以得出第三个数字 这3个数字中2个数字都能得出第三个

1100 12
1000 8
亦或^
0100 4
[root@centos7 ~]#a=$[1^2]
[root@centos7 ~]#echo a
a
[root@centos7 ~]#echo $a
3

[root@centos7 ~]#a=$[1^2] ;echo $a ;b=$[a^2] ;echo $b ;a=$[a^1];echo $a
3
1
2

三个数异或 怎么算

3个异或可以得到第四个 3个结合可以得到另一个数字
[root@centos7 ~]#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
[root@centos7 ~]#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 表达式

[root@centos7-6 ~]#ehco aa &>/dev/null && echo bb
[root@centos7-6 ~]#echo aa &>/dev/null && echo bb
bb

&&
假 && 第一个命令结果假 则 后边没有必要执行

真 && 假|真 第一个命令结果真则 则后边有必要在看如果是真则真 ,是假则是假

短路或

真 || 如果第一个表达式是真 则后边没有必要
假 || 真|假 第一个结果假 则 则后边有必要在看如果是真则真 ,是假则是假

COMMAND1 && COMMAND2

COMMAND1 || COMMAND2

-rw-r--r--. 1 root root 595 Mar 5 20:25 /etc/fstab
[root@centos7-6 ~]#[ -w /etc/fstab ] && echo /etc/fstab is writale 1对则执行2
/etc/fstab is writale

[root@centos7-6 ~]#[ -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

[root@centos7-6 ~]#[ -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

[root@centos7 script]#name=mage
[root@centos7 script]#name1=mage
[root@centos7 script]#test $name = $name1
[root@centos7 script]#echo $?
0
[root@centos7 script]#name1=mag
[root@centos7 script]#test $name = $name1
[root@centos7 script]#echo $?
1

OS=cat /etc/redhat-release |grep -oE "[0-9]+"|head -n1

= 前后要有东西才能比字符串

[ "$a" = "$b" ] 字符串比较

== =~ 都是模糊匹配比较 ==通配符格式 =~ 正则表达式

正则表达式和通配符格式 不用加双引号,但是前边要加引号,表示字符串不能为空
[root@centos7 script36]#a=f1.sh
[root@centos7 script36]#[[ "$a" =~ .sh$ ]]

[root@centos7 script36]#a=f1.sh
[root@centos7 script36]#[[ "$a" == .sh ]]
[root@centos7 script36]#echo $?
0
a=0
[root@centos7 script36]#[[ $a ==
.sh ]]
[root@centos7 script36]#echo $?
0
[root@centos7 script]#a=f1.sh ; [[ "$a" =~ ".sh$" ]];echo $? 正则表达式不用加引号
1
[root@centos7 script]#a=f1.sh ; [[ $a =~ .sh$ ]];echo $?
0
[root@centos7 script]#a=f1.sh ; [[ $a == .sh ]];echo $?
0
[root@centos7 script]#a=f1.sh ; [[ $a == "
.sh" ]];echo $? 通配符表达式也不用加引号
1

-z 判断字符串和$a为空

-n 判断字符串不为空

[ -n "$var" ];echo $?
[ ] = [ -n $var ]

[root@centos7 script36]#[ "" ];echo $?
1
[root@centos7 script36]#[ ];echo $?
1

皆为空

[ x"$var" = 'x' ];echo $? 这个方法简直太好了很好的除去表达式不成立,或者为空风险,只能判断字符串

[root@centos7 script]#[ "x""$vvv" = "x" ] ;echo $?
0
[root@centos7 script]#[ "x"$vvv = "x" ] ;echo $?
0
[root@centos7 script]#[ x$vvv = x ] ;echo $?
0
[root@centos7 script]#[ 'x'$vvv = 'x' ] ;echo $?
0

test = [ ] 但是[ ] 比较直观
[ ] 精确匹配
[[ ]] 模糊匹配 通配符 正则 [[ ]] 扩展正则表达式

arg1 OP arg2 Arithmetic tests. OP is one of -eq, -ne,
-lt, -le, -gt, or -ge.
[ ]判断变量是不是大小
[root@centos7 script]#[ 1 -gt 2 ] ;echo $?
1

n= ; [ "$num" ] 做比较应该先判断有内容是不是空

[root@centos7 script]#[ "$num" ] ; echo $?
1
[root@centos7 script]#num=
[root@centos7 script]#[ "$num" ] ; echo $?
1
[root@centos7 script]#num=""
[root@centos7 script]#[ "$num" ] ; echo $?
1
[root@centos7 script]#num=''
[root@centos7 script]#[ "$num" ] ; echo $?
1
[root@centos7 script]#

[root@centos7 script]#n=-7 ; [ "$n" ] && [[ "$n" =~ ^[0-9]+$ ]]; echo $?
1
纯正整数数字且不为空
n=111; [[ "$n" =~ ^[0-9]+$ ]];echo $? 纯正整数数字且不为空

VAR变量的意思
-v VAR 是否被设置值 也就是是否设置和字符串不为空
unset name
[ -v $name ];echo $?
[root@centos7 script]#unset nnn
[root@centos7 script]#[ -v $nnn ] ;echo $? 变量没有赋值则为正确
0
[root@centos7 script]#[ -v $nnn ] ;echo $?
0
[root@centos7 script]#nnn=111
[root@centos7 script]#[ -v $nnn ] ;echo $? 变量被赋值则为错误
1
[root@centos7 script]#nnn=
[root@centos7 script]#[ -v $nnn ] ;echo $?
0

""自动给字符串赋值让它不为空或者不赋值的状态 -v正确表示应该不加""
[root@centos7 script]#[ -v "" ] ;echo $?
1
[root@centos7 script]#set |grep nnn=
[root@centos7 script]#[ -v "$nnn" ] ;echo $?
1
[root@centos7 script]#[ -v $nnn ] ;echo $?
0
[root@centos7 script]#nnn=1
[root@centos7 script]#[ -v $nnn ] ;echo $?
1
[root@centos7 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命令执行结果

[root@centos7 script]#(lslsl ;exit 10)
bash: lslsl: command not found...
[root@centos7 script]#echo $?
10
[root@centos7 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 ) ]

[root@centos7 ~]#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
[root@centos7 ~]#[ -a /etc/login -a ! -a /etc/nologin ] ;echo $?
0
[root@centos7 ~]#\rm -f /etc/login
[root@centos7 ~]#[ ! -a /etc/login -a ! -a /etc/nologin ] ;echo $?
0
[root@centos7 ~]#touch /etc/nologin
[root@centos7 ~]#[ ! -a /etc/login -a -a /etc/nologin ] ;echo $?
0
[root@centos7 ~]#touch /etc/login
[root@centos7 ~]#[ -a /etc/login -a -a /etc/nologin ] ;echo $?
0
[root@centos7 ~]#[ ! ( -a /etc/login -o -a /etc/nologin ) ] ;echo $?
1
[root@centos7 ~]#\rm /etc/login /etc/nologin
[root@centos7 ~]#[ ! ( -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 $-
[root@centos7 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)
?索引构建过程需要遍历整个根文件系统,极消耗资源

[root@centos7 test]#locate 111 |grep "/data"
[root@centos7 test]#updatedb
[root@centos7 test]#locate 111 |grep "/data"
/data/test/111
[root@centos7 test]#ll /var/lib/mlocate/mlocate.db 不是开机立马更新过一会更新
updatedb 更新索引数据库命令

locate args.sh

locate 特点
速度快 因为搜索也有数据库的文件索引速度快
模糊查找 因为搜的是包含111所以模糊查找
[root@centos7 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
可能只搜取用户具备读取和执行的目录
[root@centos7 test]#chmod 000 ../test
[root@centos7 test]#ll .
[mage@centos7 ~]$locate -r "t/111$"
/root/111

[root@centos7 test]#chmod 777 ../test
[mage@centos7 ~]$locate -r "t/111$"
/data/test/111
/root/111

locate KEYWORD 关键词

-i 忽略大小写
-n 只列出n各项目
-r 基本正则表达式
'.conf$'
[root@centos7 test]#locate -r "t/1*$"
/data/test/111
/root/111

find 是遍历文件目标路径的,所以效率低 ,而且影响性能,而且也是看访问权限
他属于精确查找时时查找如window

查找条件 :文件名大小类型权限

find 执行效率低 ,影响系统性能 普通用户看访问权限 精确查找实时查找

find 支持通配符 -name *,? ,[] ,[^]

find 路径 查找条件 处理动作
默认当前目录 而且搜索递归
find -name "f*" 支持同配符 glob 但是要表示文件名全称
-regex 支持正则表达式但是要表示文件名全称

[root@centos7 data]#find -name a
./script/argsnum.sh
[root@centos7 test]#find -name 1

./111
可以指定深度 find -maxdepth 1 只是找1级子目录
[root@centos7 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
[root@centos7 data]#find -inum "67"
./script

[root@centos7 data]#find -maxdepth 5 -regex "..sh$"
./script/downloadscp.sh
主要是搜索最深和最短搜索会掐断和截取一部分 最大搜索深度就是最大到哪,最小搜索深度就是从最小深度搜索
[root@centos7 data]#find -mindepth 2 -name "1
"
./rm/123.link
./rm/123.link/11
./rm/11
./test/111

最大搜索度 -maxdepth 2
最小搜索深度 -mindepth 1
指定在 几层到几层搜索
-depth 先处理文件在处理目录 这样可以方便看 . 之后 . /
[root@centos7 test]#find -depth
./111
.
find 默认先处理目录在处理文件 默认先处理目录在处理文件 遍历

find -name ".sh" 支持通配符
find -inum 67 支持插inode号查找
find -regex ".
.sh$" find通配符匹配整个路径

*find -regex "." 必须是文件的全部路径****

[root@centos7 data]#find -maxdepth 5 -regex ".*.sh$"
./script/downloadscp.sh
./script/canshu.sh

find -user laowang -ls 用户为wang文件列出
[root@centos7 home]#find -user laowang -ls
202566453 0 drwx------ 2 laowang ccc 6 Mar 8 19:40 ./user

find -nouser 这个命令好可以查看没有用户的文件
[root@centos7 home]#find -nouser
./abb
./abb/.mozilla
./abb/.mozilla/extensions
./abb/.mozilla/plugins
./abb/.bash_logout
find -nogroup 找没有所有者所属组文件
[root@centos7 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

[root@centos7 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 不加权限则精准匹配

[root@centos7 test]#find -perm /777 -ls
33554688 0 drwxrwxrwx 2 root root 129 Mar 20 20:47 .
这个是最大权限只要在777里就列出

[root@centos7-6 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
[root@centos7-6 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

[root@centos7-6 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

[root@centos7-6 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[root@centos7 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 主流压缩工具

猜你喜欢

转载自blog.51cto.com/14230597/2388150