微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Linux_文件查找

# **Linux基础命令五**

* * *

## **文件命名规则**

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

## **一.文本查找(grep egrep fgrep)**

### **Pattern(模式)** //文本字符和正则表达式定义的模式来过滤文本的命令。

### **grep(文本三剑客之一)**

* 根据模式搜索文本,并将符合模式的文本行**显示出来。**
* 使用基本正则表达式定义的模式来**过滤文本**的命令。

1. **grep abc** //过滤并取出含有abc(小写abc)关键字的内容

```bash
[root@lc ~]# ls|grep abc
abc
abc-214625
[root@lc ~]
```

* * *

### **-i** //忽略大小写。

2. **grep -i** //过滤取出含abc(忽略大小写)关键字的内容

```bash
[root@lc ~]# ls|grep -i abc
abc
aBc
AbC
ABc
ABC
abc-214625
[root@lc ~]
```

3. **grep -i abc$** //$代表以...结尾,过滤取出abc(忽略大小写)结尾的内容

```bash
[root@lc ~]# ls|grep -i abc$
abc
aBc
AbC
ABc
ABC
[root@lc ~]
```

* * *

### **-v** //取反,显示没有被模式匹配到的行。

4. **grep -v** //取反,过滤取出以abc(小写abc)关键字结尾以外的内容

```bash
[root@lc ~]# ls|grep -v abc$
201105
a
ab
aBc
AbC
ABc
ABC
abc-214625
anaconda-ks.cfg
test
[root@lc ~]
```

* * *

### **-o** //只显示被模式匹配到的字符串。

5. **grep -o** //只显示被模式匹配到的字符串。

```bash
[root@lc ~]# ls|grep -o abc
abc
abc
[root@lc ~]

6. **grep -o ^abc$** //^...$用来精确查找;只取abc(小写abc)。

- **^a表示查找以a开头的内容**
- **c$表示查找以c结尾的内容**

```bash
[root@lc ~]# ls|grep -o ^abc$
abc
[root@lc ~]
```

7. **grep -io ^abc$** //^...$用来精确查找;只取abc(不分abc)。

```bash
[root@lc ~]# ls|grep -io ^abc$
abc
aBc
AbC
ABc
ABC
[root@lc ~]#
```

* * *

### **-E** // 使用扩展正则表达式,grep -E相当于使用egrep。

8. **grep -E '.B.$'** //‘^.B.$’代表中间是B的内容;只取中间是B的内容

```bash
[root@lc ~]# ls|grep -E '.B.$'
aBc
ABc
ABC
[root@lc ~]#
```

9. **grep -E '^AB.$|^lB.$'** //以|做分隔符代表和;只取AB开头和lB开头的内容

```bash
[root@lc ~]# ls|grep -E '^AB.$|^lB.$'
ABc
ABC
lBv
[root@lc ~]#
```

* * *

### **-q** // 静模式,不输出任何信息。

10. **grep -Eq 'aBc|aBj'** //搜索aBc和aBj,不输出

- **echo $?** //**0**表示搜索到了;**1**表示没有搜索到。

```bash
[root@lc ~]# ls|grep -Eq 'aBc|aBj'
[root@lc ~]# echo $?
0
[root@lc ~]# ls|grep -Eq 'aBc|sdsds'
[root@lc ~]# echo $?
0
[root@lc ~]# ls|grep -Eq 'aBcdsdsd|sdsds'
[root@lc ~]# echo $?
1
[root@lc ~]#
```

* * *

* **-A 1** //后面一行

* **-B 1**//前面一行

* **-C 1**//前后各一行

11. **A1 B1 C1** // after;before;contest,数字代表行数;搜索结果如下:

```bash
[root@lc ~]# grep -A1 '^network' anaconda-ks.cfg
network --bootproto=dhcp --device=ens160 --ipv6=auto --activate
network --hostname=localhost.localdomain
repo --name="AppStream" --baseurl=file:///run/install/repo/AppStream
[root@lc ~]# grep -B1 '^network' anaconda-ks.cfg
# Network @R_229_4045@ion
network --bootproto=dhcp --device=ens160 --ipv6=auto --activate
network --hostname=localhost.localdomain
[root@lc ~]# grep -C1 '^network' anaconda-ks.cfg
# Network @R_229_4045@ion
network --bootproto=dhcp --device=ens160 --ipv6=auto --activate
network --hostname=localhost.localdomain
repo --name="AppStream" --baseurl=file:///run/install/repo/AppStream
[root@lc ~]#
```

* * *

## **文本查找(find)**

- text实时查找,精确性强,遍历指定目录中所有文件完成查找。
- 查找速度慢,支持众多查找标准。
- 语法:**find [OPTION...]** 查找路径 查找标准 查找到以后的处理动作。
- 查找路径 //认为当前目录。
- 查找标准 //认为指定路径下的所有文件

* * *

### **-name ‘filename’**// 对文件名作精确匹配.支持glob通配符机制。

### **-iname ‘filename’**// -i 文件匹配时不区分大小写。
1. **find / - name abc** //在根目录下查找以abc(小写abc)为名字的目录。

```bash
[root@lc ~]# find / -name abc
/opt/abc
/tmp/abc
[root@lc ~]#
```

#### 2. **find / - iname abc** //在根目录下查找以abc(忽略大小写)为名字的目录。

```bash
[root@lc ~]# find / -iname abc
/etc/abC
/root/aBc
/opt/abc
/opt/aBc
/opt/AbC
/opt/ABc
/opt/ABC
/tmp/abc
/ABC
[root@lc ~]#
```

* * *

### **-user username** //根据用户来查找。

3. **find / -user jerry** //查找和jerry用户有关的文件

```bash
[root@lc ~]# find / -user jerry
/home/jerry
/home/jerry/.bash_logout
/home/jerry/.bash_profile
/home/jerry/.bashrc
find: ‘/proc/5309/task/5309/fd/7’: No such file or directory
find: ‘/proc/5309/task/5309/fdinfo/7’: No such file or directory
find: ‘/proc/5309/fd/6’: No such file or directory
find: ‘/proc/5309/fdinfo/6’: No such file or directory
/var/spool/mail/jerry
```

* * *

### **-group groupname** //根据用户组来查找。

4. **find / -group jerry** //查找jerry用户组。

```bash
[root@lc ~]# find / -group jerry
/home/jerry
/home/jerry/.bash_logout
/home/jerry/.bash_profile
/home/jerry/.bashrc
find: ‘/proc/5316/task/5316/fd/7’: No such file or directory
find: ‘/proc/5316/task/5316/fdinfo/7’: No such file or directory
find: ‘/proc/5316/fd/6’: No such file or directory
find: ‘/proc/5316/fdinfo/6’: No such file or directory
[root@lc ~]#
```

* * *

### **-nouer** // 查找没有属主的文件.用户删除的情况下产生的文件,只有uid没有属主。

### **-nogrouP** // 查找没有属组的文件.组被删除的情况下产生的文件,只有gid没有属组。

5. **find -nouer/find -nogrouP** //结果如下:

```bash
[root@lc ~]# ll
total 4
-rw-r--r--. 1 1000 root 0 Nov 8 00:17 aBc
-rw-------. 1 root 1000 1188 Nov 7 23:59 anaconda-ks.cfg
[root@lc ~]# find -nouser
./aBc
[root@lc ~]# find -nogroup
./anaconda-ks.cfg
[root@lc ~]#
```

* * *

### **-type//根据文件类型来查(f,d,c,b,l,p,s)**

- f 普通文件
- d 目录文件
- c 字符设备文件
- b 块设备文件
- l 链接文件
- p 管道文件
- s 套接文件

1. **find -type f/find -type d** //查找普通文件和目录文件

```bash
[root@lc ~]# find -type f
./.bash_logout
./.bash_profile
./.bashrc
./.cshrc
./.tcshrc
./anaconda-ks.cfg
./abc/xixi
[root@lc ~]# find -type d
.
./abc
[root@lc ~]#
```

* * *

### **-size** //根据文件大小进行查找。如1k、1M,+10k、+10M,-1k、-1M。

2. **-size +2G /-2G** //+代表大于2G;-代表小于2G。

```bash
[root@lc ~]# find /opt -size +1G
/opt/abc
[root@lc ~]# find -size -2k
.
./.bash_logout
./.bash_profile
./.bashrc
./.cshrc
./.tcshrc
./hehe
./abc
./abc/xixi
```

* * *

- **-mtime** //修改时间
- **-ctime** //改变时间
- **-atime** //访问时间

3. **查找2天以前;查找2天以内**//+2是2天前,-2是2天以内。

```bash
[root@lc ~]# find -mtime -2
.
./anaconda-ks.cfg
./hehe
./abc
./abc/xixi
[root@lc ~]# find -mtime +2
./.bash_logout
./.bash_profile
./.bashrc
./.cshrc
./.tcshrc
[root@lc ~]#
```

* * *

- **-mmin** //多少分钟修改
- **-cmin** //多少分钟改变过
- **-amin** //多少分钟访问过
4. **查找5分钟以前;查找5分钟以内**//+2是2分钟前,-2是2分钟以内(60分钟是1小时)。

```bash
[root@lc ~]# find -mmin +5
.
./.bash_logout
./.bash_profile
./.bashrc
./.cshrc
./.tcshrc
./anaconda-ks.cfg
./hehe
./abc
./abc/xixi
[root@lc ~]# find -mmin -5
[root@lc ~]#
```

* * *

## **二.组合条件**

1. **-a** //认情况;查找名字是abc并且大于1G的文件

```bash
[root@lc ~]# find / -name abc -a -size +1G
/opt/abc
```

2. **-o** //-o是或者的意思;查找大于1G或者名字叫abc的文件(proc目录里的文件忽略)。

```bash
[root@lc ~]# find / -size +1G -o -name abc
/proc/kcore
find: ‘/proc/5569/task/5569/fd/6’: No such file or directory
find: ‘/proc/5569/task/5569/fdinfo/6’: No such file or directory
find: ‘/proc/5569/fd/5’: No such file or directory
find: ‘/proc/5569/fdinfo/5’: No such file or directory
/root/abc
/opt/abc
```

3. **-not** //取反;查找名字是abc但是大小不是大于1G的文件(在什么条件前面加not就是给那个条件取反)。

```bash
[root@lc ~]# find / -name abc -not -size +1G
/root/abc
[root@lc ~]# ll -h /opt/
total 2.0G
-rw-r--r--. 1 root root 2.0G Nov 8 00:58 abc
[root@lc ~]#
```

* * *

## **三.处理动作**

1. **-print** //认是打印。

```bash
[root@lc ~]# find -type l
./hehe
[root@lc ~]# find -type l -print
./hehe
[root@lc ~]# find -type l -ls
101232241 0 lrwxrwxrwx 1 root root 3 Nov 8 00:43 ./hehe -> aBc
[root@lc ~]#
```

2. **-ls** //需要打出ls显示详细信息。

```bash
[root@lc ~]# find -type l -ls
101232241 0 lrwxrwxrwx 1 root root 3 Nov 8 00:43 ./hehe -> aBc
[root@lc ~]#
```

3. **-delete**//删除文件(不可以删除目录)。

```bash
[root@lc ~]# find -type l -delete
[root@lc ~]# ls
abc anaconda-ks.cfg
[root@lc ~]#
```

4. **-fls** //把长格式信息文件写到至指定文件中。

```bash
[root@lc ~]# find -type d -fls hehe
[root@lc ~]# ls
abc anaconda-ks.cfg hehe
[root@lc ~]# cat hehe
100663425 0 dr-xr-x--- 3 root root 137 Nov 8 01:58 .
67533296 0 drwxr-xr-x 2 root root 18 Nov 8 00:48 ./abc
[root@lc ~]#
```

5. **-ok** //对查找到的每个文件执行COMMAND,每次操作都需要用户确认(这里是询问是否删除,输入y确定,n是拒绝)。

```bash
[root@lc ~]# find / -name hehe
/etc/hehe
/opt/hehe
/tmp/hehe
[root@lc ~]# find / -name hehe -ok rm -rf {} \;
< rm ... /etc/hehe > ? y
< rm ... /opt/hehe > ? y
< rm ... /tmp/hehe > ? y
[root@lc ~]#
```

6. **-exec** // 对查找到的每个文件执行COMMAND,操作不需要确认(这里是不询问直接删除)。

```bash
[root@lc ~]# find / -name hehe -exec rm {} \;
[root@lc ~]# ls
abc anaconda-ks.cfg
[root@lc ~]# ls /etc/hehe
ls: cannot access '/etc/hehe': No such file or directory
[root@lc ~]# ls /tmp/
ks-script-axebdlv6 vmware-root_967-4248221830
[root@lc ~]#
```

7. **xargs** //过管道将查找到的内容给xargs处理,xargs后面直接跟命令即可(一般用来删除文件)。

```bash
[root@lc ~]# ls
123 abc anaconda-ks.cfg
[root@lc ~]# find -name abc|xargs rm -rf
[root@lc ~]# ls
123 anaconda-ks.cfg
[root@lc ~]#
```

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐