linux 基础命令(二)

基础命令及其用法 (二)

文本去重命令 uniq

Usage: uniq [OPTION]… [INPUT [OUTPUT]]

  • c:显示文件中行重复的次数
  • d:只显示重复的行
  • u:只显示未重复的行
[root@wangaifei ~]# uniq -c 9
      1 3
      1 7
      1 9
      1 
      1 5
      1 4
      1 3
      1 6
      1 7
      1 3
      1 7
      1 4
      1 2
      1 4

基础,命令之cut

[root@wangaifei ~]# cut -d: -f1 passwd|head -3
root
bin
daemon
[root@wangaifei ~]# cut -d: -f1-3 passwd|head -3
root:x:0
bin:x:1
daemon:x:2
[root@wangaifei ~]# cut -d: -f1,3 passwd|head -3
root:0
bin:1
daemon:2

高级命令 awk

[root@wangaifei ~]# awk -F: '{print $1}' passwd | head -3
root
bin
daemon
[root@wangaifei ~]# awk -F: '{print $1,$2,$3}' passwd | head -3
root x 0
bin x 1
daemon x 2
[root@wangaifei ~]# awk -F: '{print $1,$3}' passwd | head -3
root 0
bin 1
daemon 2
[root@wangaifei ~]# awk -F: '{print $NF}' passwd | head -3
/bin/bash
/sbin/nologin
/sbin/nologin
[root@wangaifei ~]# awk -F: '{print $(NF-1)}' passwd | head -3
/root
/bin
/sbin
[root@wangaifei ~]# df
文件系统                 1K-块    已用     可用 已用% 挂载点
/dev/mapper/rhel-root 17811456  976968 16834488    6% /
devtmpfs                922456       0   922456    0% /dev
tmpfs                   933524       0   933524    0% /dev/shm
tmpfs                   933524    8824   924700    1% /run
tmpfs                   933524       0   933524    0% /sys/fs/cgroup
/dev/sda1              1038336  145856   892480   15% /boot
tmpfs                   186708       0   186708    0% /run/user/0
/dev/sr0               3963760 3963760        0  100% /mnt
[root@wangaifei ~]# df |awk '{print $3}'
已用
976968
0
0
8824
0
145856
0
3963760
[root@wangaifei ~]# head -3 passwd 
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
[root@wangaifei ~]# head -1 passwd |cut -d: -f7
/bin/bash
[root@wangaifei ~]# awk -F: 'NR==1{print $7}' passwd 
/bin/bash
[root@wangaifei ~]# awk -F: 'NR==2{print $7}' passwd 
/sbin/nologin
[root@wangaifei ~]# awk -F: 'NR==2{print $5}' passwd 
bin
[root@wangaifei ~]# awk -F: 'NR==3{print $5}' passwd 
daemon

高级命令之sed

sed替换内容

[root@wangaifei ~]# echo 'hello world runtime' |sed -r 's/hello (.*) (.*) /hello \2 \1/g'
hello world runtime

打印第一行内容

[root@wangaifei ~]# sed -n '1p' passwd 
root:x:0:0:root:/root:/bin/bash

把第一行的第二个root换为haha

[root@wangaifei ~]# sed '1s/root/haha/2' passwd |head -1
root:x:0:0:haha:/root:/bin/bash

把全部root换为haha

[root@wangaifei ~]# sed '1s/root/haha/g' passwd |head -1
haha:x:0:0:haha:/haha:/bin/bash

把第三个 root换为haha

[root@wangaifei ~]# sed '1s/root/haha/3' passwd |head -1
root:x:0:0:root:/haha:

查找以root开头的行,并将所有的root替换为haha

[root@wangaifei ~]# head -1 passwd 
root:x:0:0:root:/root:/bin/bash
[root@wangaifei ~]# sed -i '/^root/s/root/haha/g' passwd 
[root@wangaifei ~]# head -1 passwd 
haha:x:0:0:haha:/haha:/bin/bash
[root@wangaifei ~]# echo 'hello world' > abc
[root@wangaifei ~]# ls
1   13  17        20     4  8    anaconda-ks.cfg  d      hh          passwd
10  14  18        2.bz2  5  9    bb               dd     hw          world
11  15  19        3      6  a    bbb              e      kong
12  16  1.tar.gz  3.bz2  7  abc  c                hello  number.zip
[root@wangaifei ~]# cat abc
hello world
[root@wangaifei ~]# echo 'runtime' >> abc
[root@wangaifei ~]# echo 'runtime' >> abc
[root@wangaifei ~]# echo 'runtime' >> abc
[root@wangaifei ~]# echo 'runtime' >> abc
[root@wangaifei ~]# cat abc
hello world
runtime
runtime
runtime
runtime
[root@wangaifei ~]# cat > abc <<EFO
>
> wangaifei
> wangaifei
> wangaifei
> EFO
[root@wangaifei ~]# cat abc

wangaifei
wangaifei
wangaifei

把每行的第1个wang替换成W

[root@wangaifei ~]# sed 's/wang/W/1' abc

Waifei
Waifei
Waifei

把每行的第一个ai替换成A

[root@wangaifei ~]# sed 's/wang/W/1' abc

Waifei
Waifei
Waifei
[root@wangaifei ~]# sed 's/ai/A/1' abc

wangAfei
wangAfei
wangAfei

-$p表示最后一行

[root@wangaifei ~]# sed -n '$p' passwd 
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
[root@wangaifei ~]# tail -3 passwd 
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
chrony:x:998:996::/var/lib/chrony:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin

文件命名规范

  • 长度不能超过255字符
  • 不能使用/当文件名
  • 严格区分大小写

正则表达式

  1. 正则表达式分类
    正则表达式:REGEXP,REGular EXPression。
    正则表达式分为两类:

Basic REGEXP(基本正则表达式)
Extended REGEXP(扩展正则表达式)
2. 基本正则表达式

//元字符
    .           //任意单个字符
    []          //匹配指定范围内的任意单个字符
    [^]         //匹配指定范围外的任意单个字符
//匹配次数(贪婪模式)
    *           //匹配其前面的任意单个字符任意次
    .*          //任意长度的任意字符
    \?          //匹配其前面的任意单个字符1次或0次
    \+          //匹配其前面的任意单个字符至少1次
    \{m,n\}     //匹配其前面的任意单个字符至少m次,至多n次
//位置锚定
    ^           //锚定行首,此字符后面的任意单个字符必须出现在行首
    $           //锚定行尾,此字符前面的任意单个字符必须出现在行尾
    ^$          //空白行
    \<或\b       //锚定词首,其后面的任意单个字符必须作为单词首部出现
    \>或\b       //锚定词尾,其前面的任意单个字符必须作为单词尾部出现
/分组
    \(\)
    例:\(ab\)*
    //后向引用
        \1      //引用第一个左括号以及与之对应的右括号所包括的所有内容
        \2      //引用第二个左括号以及与之对应的右括号所包括的所有内容
3. 扩展正则表达式
//字符匹配
    .       //匹配任意单个字符
    []      //匹配指定范围内的任意单个字符
    [^]     //匹配指定范围外的任意单个字符
//次数匹配
    *       //匹配其前面的任意单个字符任意次
    ?       //匹配其前面的任意单个字符1次或0次
    +       //匹配其前面的任意单个字符至少1次
    {m,n}   //匹配其前面的任意单个字符至少m次,至多n次

//位置锚定
    ^       //锚定行首,此字符后面的任意单个字符必须出现在行首
    $       //锚定行尾,此字符前面的任意单个字符必须出现在行尾
    ^$      //空白行
    \<或\b       //锚定词首,其后面的任意单个字符必须作为单词首部出现
    \>或\b       //锚定词尾,其前面的任意单个字符必须作为单词尾部出现
//分组
    ()      //分组
    \1,\2,\3,....
   例:(ab)*
    //后向引用
        \1      //引用第一个左括号以及与之对应的右括号所包括的所有内容
        \2      //引用第二个左括号以及与之对应的右括号所包括的所有内容
//或者
    |       //or 默认匹配|的整个左侧或者整个右侧的内容
    //例:C|cat表示C或者cat,要想表示Cat或者cat则需要使用分组,如(C|c)at

文本过滤命令grep

- grep命令根据正则表达式搜索文本,并将符合正则表达式的文本显示出来。
默认使用基本正则表达式来过滤文本。

- Usage: grep [OPTION]... PATTERN [FILE]...

- 常用的OPTION:
	-i:忽略大小写
	--color:匹配到的内容高亮显示
    -v:显示没有被正则表达式匹配到的内容
	-o:只显示被正则表达式匹配到的内容
	-E:使用扩展正则表达式
	-q:静默模式,不输出任何信息
	-A #:此处的#必须是数字。被正则匹配到的内容以及其后面#行的内容都显示出来
	-B #:此处的#必须是数字。被正则匹配到的内容以及其前面#行的内容都显示出来
	-C #:此处的#必须是数字。被正则匹配到的内容及其前后各#行的内容都显示出来

[root@wangaifei ~]# which grep
alias grep='grep --color=auto'
	/usr/bin/grep
[root@wangaifei ~]# ls | grep Bc
[root@wangaifei ~]# ls | grep abc
abc
[root@wangaifei ~]# ls | grep Abc
Abc
[root@wangaifei ~]# ls | grep ABC
ABC
[root@wangaifei ~]# ls | grep '[Aa]bc'
abc
Abc
[root@wangaifei ~]# 
-i:  过滤时忽略大小写
[root@wangaifei ~]# ls | grep -i abc
abc
Abc
ABC
-v:显示没有被正则表达式匹配到的内容
[root@wangaifei ~]# ls |grep -v '^abc$'
1.tar.gz
2.bz2
3.bz2
a
Abc
ABC
anaconda-ks.cfg
c
d
dd
- :过滤出以a开头c结尾的文件,'^'表示行首,此字符后面的任意单个字符必须出现在行首。'$'锁定行尾,此字  前面的任意单个字符必须出现在行尾
[root@wangaifei ~]# ls
a  abc  Abc  ABC  abcde  anaconda-ks.cfg  hw  passwd
[root@wangaifei ~]# ls |grep -v '^abc$'
a
Abc
ABC
abcde
anaconda-ks.cfg
hw
passwd
-o:只显示被正则表达式匹配到的内容
[root@wangaifei ~]# ls
a  abc  Abc  ABC  abcde  anaconda-ks.cfg  hw  passwd
[root@wangaifei ~]# ls |grep -o 'abc'
abc
abc
[root@wangaifei ~]# ls |grep  'abc'
abc
abcde
-E:使用扩展正则表达式,不必转义
[root@wangaifei ~]# ls
a  abc  Abc  ABC  abcde  anaconda-ks.cfg  hw  passwd
[root@wangaifei ~]# ls | grep -E '(a|A)bc'
abc
Abc
abcde
[root@wangaifei ~]# ls | grep -E '^(a|A)bc$'
abc
Abc

基本正则表达式

[root@wangaifei ~]# ls | grep '\(a\|A\)bc'
abc
Abc
abcde
-q 静默模式,一般应用于写脚本;$?表示锁定前一条命令执行成功与否,0 表示执行成功,1 表示未成功
[root@wangaifei ~]# ls | grep -q 'abc'
[root@wangaifei ~]# echo $?
0
[root@wangaifei ~]# echo $?
0

[root@wangaifei ~]# head -5 passwd  //显示文件的前五行
haha:x:0:0:haha:/haha:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
[root@wangaifei ~]# 
[root@wangaifei ~]# grep 'daemon' passwd   //过滤出有 daemon的行
daemon:x:2:2:daemon:/sbin:/sbin/nologin
[root@wangaifei ~]# 
[root@wangaifei ~]# 
[root@wangaifei ~]# grep -A2 'daemon' passwd //-A#:此处的#必须是数字。被正则匹配到的内容以及其后面#行的内容都显示出来
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
[root@wangaifei ~]# 
[root@wangaifei ~]# 
[root@wangaifei ~]# grep -B2 'daemon' passwd 
//-B #:此处的#必须是数字。被正则匹配到的内容以及其前面#行的内容都显示出来
haha:x:0:0:haha:/haha:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
[root@wangaifei ~]# 
[root@wangaifei ~]# 
[root@wangaifei ~]# 
[root@wangaifei ~]# grep -C1 'daemon' passwd 
//-C #:此处的#必须是数字。被正则匹配到的内容及其前后各#行的内容都显示出来
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
[root@wangaifei ~]# ls
a  aac  abbcc  abc  Abc  ABC  abcde  acc  accc  accccc  anaconda-ks.cfg  hw  passwd
[root@wangaifei ~]# ls | grep 'a.c'// '.'表示匹配任意单个字符
aac
abc
abcde
acc
accc
accccc
[root@wangaifei ~]# ls | grep '^a.c$'//匹配以a开头c结尾的任意单个字符, '^'表示行首,此字符后面的任意单个字符必须出现在行首。'$'锁定行尾,此字符前面的任意单个字符必须出现在行尾
aac
abc
acc
[root@wangaifei ~]# ls
1    18  27  36  45  54  63  72  81  90  a      accccc           f   J  O       t  y
10   19  28  37  46  55  64  73  82  91  A      anaconda-ks.cfg  F   k  p       T  Y
100  2   29  38  47  56  65  74  83  92  aac    b                g   K  P       u  z
11   20  3   39  48  57  66  75  84  93  abbcc  B                G   l  passwd  U  Z
12   21  30  4   49  58  67  76  85  94  abc    c                h   L  q       v
13   22  31  40  5   59  68  77  86  95  Abc    C                H   m  Q       V
14   23  32  41  50  6   69  78  87  96  ABC    d                hw  M  r       w
15   24  33  42  51  60  7   79  88  97  abcde  D                i   n  R       W
16   25  34  43  52  61  70  8   89  98  acc    e                I   N  s       x
17   26  35  44  53  62  71  80  9   99  accc   E                j   o  S       X
[root@wangaifei ~]# ls | grep '^[b-d]$'//匹配b到d范围内的任意单个字符
b
c
d
[root@wangaifei ~]# ls | grep '^[abehi]$'//匹配abehi内的任意单个字符
a
b
e
h
i
[root@wangaifei ~]# ls | grep '^[0-6]$'//匹配0-6范围内的任意单个字符
1
2
3
4
5
6
[root@wangaifei ~]# ls | grep '^[0-9][0-9]$'//匹配10到99内的任意单个字符,两个'[]'表示两位数
10
11
12
13
14
15
[root@wangaifei ~]# ls | grep '^[0-9][0-9]$'|head -4//匹配两位数且只显示前四位
01
02
10
11
[root@wangaifei ~]# ls |grep '^[^0-9]$'//'^'放在[]的'['后表示取反
a
A
b
B
c
C
d
D
e
E
//剩余不一一展示
[root@wangaifei ~]# ls |grep '^[0-5^]$'//'^'放在']'前表示其本身
^
1
2
3
4
5
[root@wangaifei ~]# echo '-123456789'|grep '[7-9]'
-123456789
[root@wangaifei ~]# echo '-123456789'|grep -o '[7-9]'//'-'放在两者之间表示范围
7
8
9
[root@wangaifei ~]# touch -
[root@wangaifei ~]# echo '-123456789'|grep -o '[-7-9]'//放在两着前或两者后表示其本身
-
7
8
9
[root@wangaifei ~]# echo '-123456789'|grep -o '[7-9-]'
-
7
8
9
[root@wangaifei ~]# touch 11 1111 11111 111111 11111111
[root@wangaifei ~]# ls |grep '^1*$'//匹配'*'前面的任意字符任意次
1
11
1111
11111
111111
11111111
[root@wangaifei ~]# ls |grep '^1.*$'//匹配以1开头的任意字符的任意次
1
10
100
11
1111
11111
111111
11111111
12
13
14
15
16
17
18
19
[root@wangaifei ~]# ls|grep '^1?$'
[root@wangaifei ~]# ls|grep '^1\?$'//'\?'表示匹配其前面的任意单个字符1次或0次
1
[root@wangaifei ~]# ls
^         12  22  32  42  52  62  72  82  92   abbcc            c  H   M       R  x
01        13  23  33  43  53  63  73  83  93   abc              C  hw  n       s  X
02        14  24  34  44  54  64  74  84  94   Abc              d  i   N       S  y
1         15  25  35  45  55  65  75  85  95   ABC              D  I   o       t  Y
10        16  26  36  46  56  66  76  86  96   abcde            e  j   O       T  z
100       17  27  37  47  57  67  77  87  97   acc              E  J   p       u  Z
11        18  28  38  48  58  68  78  88  98   accc             f  k   P       U
1111      19  29  39  49  59  69  79  89  99   accccc           F  K   passwd  v
11111     2   3   4   5   6   7   8   9   a    anaconda-ks.cfg  g  l   q       V
111111    20  30  40  50  60  70  80  90  A    b                G  L   Q       w
11111111  21  31  41  51  61  71  81  91  aac  B                h  m   r       W
[root@wangaifei ~]# ls|grep '^1\+$'// '\+'匹配其前面的任意字符至少一次
1
11
1111
11111
111111
11111111
[root@wangaifei ~]# ls |grep '^1\{1,\}$'//'\{m,n\}',匹配其前面的任意字符至少m次,至多n次
1
11
1111
11111
111111
11111111
[root@wangaifei ~]# ls |grep '^1\{1,3}$'
grep: 不匹配的 \{
[root@wangaifei ~]# ls |grep '^1\{1,3\}$'
1
11
[root@wangaifei ~]# ls |grep '^1\{1,4\}$'
1
11
1111
[root@wangaifei ~]# cat > 0 <<EOF
> hello
> 
> world
> 
> tom
> 
> sam
> hi
> EOF
[root@wangaifei ~]# cat 0
hello

world

tom

sam
hi
[root@wangaifei ~]# grep -v '^$' 0 //-v:显示没有被正则表达式匹配到的内容
hello                               //'^$':空白行
world
tom
sam
hi
[root@wangaifei ~]# echo 'hello world runtime'|sed -r 's/hello (.*) (.*)/hello \2 \1/g'
//扩展正则表达式
hello runtime world
[root@wangaifei ~]# echo 'hello world runtime'|sed  's/hello \(.*\) \(.*\)/hello \2 \1/g'
//基本正则表达式小括号需要转义
hello runtime world

文件查找命令fine

语法:find [OPTION]... 查找路径  查找标准  查找到以后的处理动作

查找路径:默认为当前目录
查找标准:
	-name filename:对文件名精确匹配,支持通配符
	-iname filename:文件名匹配时不区分大小写
	-regex pattern:基于正则表达式进行文件名匹配
	-user:查找某用户的所有文件
	-group:查找某组的所有文件
	-uid:根据UID进行查找
	-gid:根据GID进行查找
	-nouser:查找没有拥有者的文件
	-nogroup:查找没有属组的文件
	-type:根据文件类型进行查找
语法:find [OPTION]... 查找路径  查找标准  查找到以后的处理动作

多条件组合使用:
	-a
	-o
	-not
	!

例如:
	!A –a !B 与 !(A –o B) 相等
	!A –o !B 与 !(A –a B) 相等
语法:find [OPTION]... 查找路径  查找标准  查找到以后的处理动作

处理动作:默认动作是显示到屏幕上
	-print:打印到屏幕上
	-ls:类似ls –l的形式显示每一个文件的详细信息
	-delete:删除查找到的文件
	-fls /path/to/somefile:将查找到的所有文件的长格式信息保存至指定文件中
	-ok COMMAND {} \;:对查找到的所有文件执行COMMAND,每次操作都需要用户确认
	-exec COMMAND {} \;:对查找到的所有文件执行COMMAND,操作不需要确认
注意:find传递查找到的文件至后面指定的命令时,查找到所有符合条件的文件一次性传递给后面的命令,而有些命令不能接受过多的参数,此时命令执行可能会失败,而xargs可规避此问题。
	xargs:通过管道将查找到的内容给xargs处理,xargs后面直接跟命令即可

[root@wangaifei ~]# ls
^       11111111  21  31  41  51  61  71  81  91  aac              B  h   m       r  W
0       12        22  32  42  52  62  72  82  92  abbcc            c  H   M       R  x
01      13        23  33  43  53  63  73  83  93  abc              C  hw  n       s  X
02      14        24  34  44  54  64  74  84  94  Abc              d  i   N       S  y
1       15        25  35  45  55  65  75  85  95  ABC              D  I   o       t  Y
10      16        26  36  46  56  66  76  86  96  abcde            e  j   O       T  z
100     17        27  37  47  57  67  77  87  97  acc              E  J   p       u  Z
11      18        28  38  48  58  68  78  88  98  accc             f  k   P       U
1111    19        29  39  49  59  69  79  89  99  accccc           F  K   passwd  v
11111   2         3   4   5   6   7   8   9   a   anaconda-ks.cfg  g  l   q       V
111111  20        30  40  50  60  70  80  90  A   b                G  L   Q       w
[root@wangaifei ~]# find -name 1  //-name filename:对文件名精确匹配,支持通配符
./1
[root@wangaifei ~]# find /root -name 1
/root/1
[root@wangaifei ~]# pwd
/root
[root@wangaifei ~]# find / -name passwd 
/sys/fs/selinux/class/passwd
/sys/fs/selinux/class/passwd/perms/passwd
/etc/passwd
/etc/pam.d/passwd
/root/passwd
/usr/bin/passwd
[root@wangaifei ~]# find /etc -name passwd 
/etc/passwd
/etc/pam.d/passwd
[root@wangaifei ~]# find /etc/pam.d/ -name passwd 
/etc/pam.d/passwd

[root@wangaifei ~]# find -iname a  //-iname filename:文件名匹配时不区分大小写
./a
./a/b/a
./a/a
./A

[root@wangaifei ~]# find -name "*7"  //匹配7前面的任意单个字符任意次,
./57
./17
./37
./67
./7
./27
./47
./77
./87
./97
[root@wangaifei ~]# ls /home/  //显示/home目录的全部内容
lisi  tom  zhangshan
[root@wangaifei ~]# ls -l /home/ //以长格式显示
总用量 0
drwx------. 2 1002 1002 62 11月  3 14:37 lisi
drwx------. 2 tom  tom  62 11月  3 14:26 tom
drwx------. 2 1001 1001 62 11月  3 14:37 zhangshan
[root@wangaifei ~]# find /home/ -user tom  //-user:查找某用户的所有文件
/home/tom
/home/tom/.bash_logout
/home/tom/.bash_profile
/home/tom/.bashrc
[root@wangaifei ~]# ls /home/ -l
总用量 0
drwx------. 2 1002 1002 62 11月  3 14:37 lisi
drwx------. 2 tom  tom  62 11月  3 14:26 tom
drwx------. 2 1001 1001 62 11月  3 14:37 zhangshan
[root@wangaifei ~]# find /home/ -group tom  //-group:查找某组的所有文件
/home/tom
/home/tom/.bash_logout
/home/tom/.bash_profile
/home/tom/.bashrc
[root@wangaifei ~]# ll /home/tom
总用量 0
[root@wangaifei ~]# touch /home/tom/123
[root@wangaifei ~]# ll /home/tom
总用量 0
-rw-r--r--. 1 root root 0 11月  7 19:48 123
[root@wangaifei ~]# userdel tom
[root@wangaifei ~]# ll /home/
总用量 0
drwx------. 2 1002 1002 62 11月  3 14:37 lisi
drwx------. 2 1000 1000 73 11月  7 19:48 tom
drwx------. 2 1001 1001 62 11月  3 14:37 zhangshan
[root@wangaifei ~]# find /home/ -nouser  //-nouser:查找没有拥有者的文件
/home/tom
/home/tom/.bash_logout
/home/tom/.bash_profile
/home/tom/.bashrc
/home/zhangshan
/home/zhangshan/.bash_logout
/home/zhangshan/.bash_profile
/home/zhangshan/.bashrc
/home/lisi
/home/lisi/.bash_logout
/home/lisi/.bash_profile
/home/lisi/.bashrc
[root@wangaifei ~]# find /home/ -nouser -exec rm -rf {} \;  
//-exec COMMAND {} \;:对查找到的所有文件执行COMMAND,操作不需要确认
find: ‘/home/tom’: 没有那个文件或目录
find: ‘/home/zhangshan’: 没有那个文件或目录
find: ‘/home/lisi’: 没有那个文件或目录
[root@wangaifei ~]# ls /home/
[root@wangaifei ~]# 
[root@wangaifei ~]# useradd tom  //创建新用户
正在创建信箱文件: 文件已存在
[root@wangaifei ~]# ls /home/
tom
[root@wangaifei ~]# ls /home/ -l
总用量 0
drwx------. 2 tom tom 62 11月  7 19:56 tom
[root@wangaifei ~]# id tom
uid=1000(tom) gid=1000(tom) 组=1000(tom)
[root@wangaifei ~]# find /home/ -uid 1000  //-uid:根据UID进行查找
/home/tom
/home/tom/.bash_logout
/home/tom/.bash_profile
/home/tom/.bashrc
[root@wangaifei ~]# ln -s abc def  //软连接;硬链接只能链接文件,不能连接目录,改变一个另一个也受影响,删除一个另一个不受影响
[root@wangaifei ~]# ls -li|grep 'abc'
33579982 -rw-r--r--. 1 root root   31 11月  5 18:59 abc
33579979 -rw-r--r--. 1 root root    0 11月  5 16:46 abcde
33575378 lrwxrwxrwx. 1 root root    3 11月  7 20:00 def -> abc
-  -type: 根据文件类型进行查找
-   f:普通文件
-   d:目录
-   l:套接字(ip+port)
-   b:块设备block
-   c:字符设备character
[root@wangaifei abc]# find /root/abc -type f  //-type:根据文件类型进行查找
/root/abc/1
/root/abc/2
/root/abc/3
/root/abc/4
/root/abc/5
/root/abc/6
/root/abc/7
[root@wangaifei abc]# find /root/abc -type d
/root/abc
[root@wangaifei ~]# find -size +1k  
//-size:根据文件大小进行查找。如1k、1M、+10k、+10M、-1k、-10M
		+表示大于,-表示小于
./.bash_history
./.anaconda-ks.cfg.swp
[root@wangaifei ~]# find -size -1k
./1
./2
./3
[root@localhost ~]# find -mmin -10  //根据时间查找修改的文件,-10表示十分钟以内,+10表示10分钟以前
.
./anaconda-ks.cfg
[root@localhost abc]# find -perm 644  //-perm –mode:文件权限能完全包含此mode时才符合条件
./1
./2
./3
./4
./5
./6
[root@localhost abc]# find -perm 644 -type f
./1
./2
./3
./4
./5
./6
[root@localhost abc]# ll
总用量 0
-rw-r--r--. 1 root root 0 11月  8 19:33 1
--w-------. 1 root root 0 11月  8 19:33 2
-rw-r--r--. 1 root root 0 11月  8 19:33 3
-rw-r--r--. 1 root root 0 11月  8 19:33 4
-rw-r--r--. 1 root root 0 11月  8 19:33 5
-rw-r--r--. 1 root root 0 11月  8 19:33 6
[root@localhost abc]# find -type f -perm 200 -print
./2
[root@localhost abc]# find -type f -a -perm 200 
./2
[root@localhost abc]# find -type f  -perm 200 
./2
[root@localhost abc]# find -type f  -perm 200 -ls
50849636    0 --w-------   1 root     root            0 11月  8 19:33 ./2
[root@localhost abc]# ls
1  2  3  4  5  6
[root@localhost abc]# find -type f -perm 200 -delete
[root@localhost abc]# ls
1  3  4  5  6
-rw-r--r--. 1 root root 0 11月  8 19:33 1
-rw-r--r--. 1 root root 0 11月  8 19:33 3
-rw-r--r--. 1 root root 0 11月  8 19:33 4
-rw-r--r--. 1 root root 0 11月  8 19:33 5
-rw-r--r--. 1 root root 0 11月  8 19:33 6
[root@localhost abc]# ls
1  3  4  5  6
[root@localhost abc]# find -type f -perm 644 -delete
[root@localhost abc]# ls


[root@localhost abc]# ls
1  10  2  3  4  5  6  7  8  9
[root@localhost abc]# find -perm 644 -fls hehe   
//-fls /path/to/somefile:将查找到的所有文件的长格式信息保存至指定文件中
[root@localhost abc]# ls
1  10  2  3  4  5  6  7  8  9  hehe
[root@localhost abc]# cat hehe
50849635    0 -rw-r--r--   1 root     root            0 11月  8 19:50 ./1
50849636    0 -rw-r--r--   1 root     root            0 11月  8 19:50 ./2
50849637    0 -rw-r--r--   1 root     root            0 11月  8 19:50 ./3
50849638    0 -rw-r--r--   1 root     root            0 11月  8 19:50 ./4
50849639    0 -rw-r--r--   1 root     root            0 11月  8 19:50 ./5
50849640    0 -rw-r--r--   1 root     root            0 11月  8 19:50 ./6
50849642    0 -rw-r--r--   1 root     root            0 11月  8 19:50 ./7
50849643    0 -rw-r--r--   1 root     root            0 11月  8 19:50 ./8
50849644    0 -rw-r--r--   1 root     root            0 11月  8 19:50 ./9
50849645    0 -rw-r--r--   1 root     root            0 11月  8 19:50 ./10
50849646    0 -rw-r--r--   1 root     root            0 11月  8 19:52 ./hehe
[root@localhost abc]# find -perm 644 -ok rm -rf {} \;
//ok COMMAND {} \;:对查找到的所有文件执行COMMAND,每次操作都需要用户确认
< rm ... ./1 > ? y
< rm ... ./2 > ? y
< rm ... ./3 > ? y
< rm ... ./4 > ? y
< rm ... ./5 > ? y
< rm ... ./6 > ? y
< rm ... ./7 > ? y
< rm ... ./8 > ? y
< rm ... ./9 > ? y
< rm ... ./10 > ? y
< rm ... ./hehe > ? y
[root@localhost abc]# y
-bash: y: 未找到命令
[root@localhost abc]# ls
[root@localhost abc]# ll
总用量 0
-rw-r--r--. 1 root root 0 11月  8 19:56 1
-rw-r--r--. 1 root root 0 11月  8 19:56 2
-rw-r--r--. 1 root root 0 11月  8 19:56 3
-rw-r--r--. 1 root root 0 11月  8 19:56 4
-rw-r--r--. 1 root root 0 11月  8 19:56 5
drwxr-xr-x. 2 root root 6 11月  8 19:55 abc
[root@localhost abc]# find -perm 644 -exec rm -rf {} \;
//-exec COMMAND {} \;:对查找到的所有文件执行COMMAND,操作不需要确认
[root@localhost abc]# ls
abc

xargs:通过管道将查找到的内容给xargs处理,xargs后面直接跟命令即可

[root@localhost abc]# find -type f | xargs rm -rf
[root@localhost abc]# ls
abc
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值