你必须非常努力,才能看起来毫不费力!
微信搜索公众号 "漫漫Coding路",一起From Zero To Hero !
前言
本篇文章主要讨论Linux中的文件搜索命令,大家一起来学习吧!
find
命令格式
find 路径 选项 参数 [ -exec -ok command {} \;]
命令描述
- 用来在指定目录下查找文件
- 不填写路径,默认在当前目录下查找
- 如果不使用选项参数,则find命令将在当前目录下查找子目录与文件,并且将查找到的子目录和文件全部进行显示
常用选项
- -name:指定文件名
- -iname:不区分大小写
- -size:文件大小,如
+ - 10M(大于小于文件大小) - -user:根据所有者查找
- -group:根据所属组查找
- -amin:(modify)文件的访问时间 ,使用
+ - 时间(大于小于多长时间内的) - -cmin:(change)文件属性被更改时间 ,使用
+ - 时间(大于小于多长时间内的) - -mmin:(modify)文件内容被修改时间 ,使用
+ - 时间(大于小于多长时间内的) - -type:文件类型(f 文件 d 目录 l 链接)
- -inum:根据i节点查找
- -a 连接多个条件 (and)
- -o:连接多个条件(or)
-exec command {} \;: 对执行结果执行提供的command操作直接执行,例如ls,后面的{} \;是固定写法-ok command {} \;: 对执行结果执行提供的command操作询问执行,例如ls,后面的{} \;是固定写法
示例
# 1. 文件准备
[root@VM-0-5-centos tmp]# cd /opt
[root@VM-0-5-centos tmp]# mkdir test
[root@VM-0-5-centos tmp]# cd test/
# 创建两个文件以及两个文件夹
[root@VM-0-5-centos test]# touch test
[root@VM-0-5-centos test]# touch TEST
[root@VM-0-5-centos test]# mkdir test1
[root@VM-0-5-centos test]# mkdir 1test
[root@VM-0-5-centos test]# ls -l
总用量 8
drwxr-xr-x 2 root root 4096 10月 9 20:45 1test
-rw-r--r-- 1 root root 0 10月 9 20:45 test
-rw-r--r-- 1 root root 0 10月 9 20:45 TEST
drwxr-xr-x 2 root root 4096 10月 9 20:45 test1
# 2. 使用-name查找
[root@VM-0-5-centos test]# find -name test
./test
# 支持使用通配符: ?匹配单个字符,*匹配多个字符
[root@VM-0-5-centos test]# find -name tes?
./test
[root@VM-0-5-centos test]# find -name '*tes*'
./test1
./1test
./test
# 3. 使用 -iname,不区分大小写
[root@VM-0-5-centos test]# find -iname test
./TEST
./test
# 4. 使用 -size,查找大于1M的文件
[root@VM-0-5-centos opt]# find -size +1M
./redis-6.2.4.tar.gz
./redis-6.2.4/src/redis-benchmark
# 5. 根据所有者查找
[root@VM-0-5-centos opt]# find /home -user lifelmy
/home/lifelmy
/home/lifelmy/.bashrc
# 6. 使用mmin,查找30min以内,内容被改变过的(cmin、amin类似)
[root@VM-0-5-centos opt]# find /tmp -mmin -30
/tmp
/tmp/stargate.lock
/tmp/test
/tmp/test/test1
/tmp/test/TEST
/tmp/test/1test
/tmp/test/test
# 7. 使用条件语句连接(30min以内,内容被改变过的文件,不包含目录)
[root@VM-0-5-centos opt]# find /tmp -mmin -30 -a -type d
/tmp
/tmp/test
/tmp/test/test1
/tmp/test/1test
# 8. 使用 -exec command {} \;
[root@VM-0-5-centos opt]# find /tmp -mmin -30 -a -type f -a -exec ls -l {} \;
-rw-r--r-- 1 root root 0 10月 9 20:46 /tmp/stargate.lock
-rw-r--r-- 1 root root 0 10月 9 20:45 /tmp/test/TEST
-rw-r--r-- 1 root root 0 10月 9 20:45 /tmp/test/test
# 8. 使用 -ok command {} \;
[root@VM-0-5-centos tmp]# find /tmp -mmin -50 -a -type f -ok ls -l {} \;
< ls ... /tmp/stargate.lock > ? n
< ls ... /tmp/test/TEST > ? y
-rw-r--r-- 1 root root 0 10月 9 20:45 /tmp/test/TEST
< ls ... /tmp/test/test > ? y
-rw-r--r-- 1 root root 0 10月 9 20:45 /tmp/test/test
# 9. 通过-inum查找到文件,然后删除(适用于那种文件名过长,或者不符合规范的文件名(有空格))
[root@VM-0-5-centos tmp]# ls -li
总用量 4
991 -rw-r--r-- 1 root root 0 10月 9 20:46 stargate.lock
264 drwxr-xr-x 4 root root 4096 10月 9 20:45 test
[root@VM-0-5-centos tmp]# find /tmp -inum 991 -ok rm {} \;
< rm ... /tmp/stargate.lock > ? y
[root@VM-0-5-centos tmp]# ls
test
locate
命令格式
locate [选项] [样式]
命令描述
- 与
find挨个查找不同,locate在本地维护了一个数据库,通过该命令查找不用挨个比对,比较节省资源。 - 但是
locate是定期维护的,新建立的文件有可能不能立即查找到,可以使用updatedb命令更新数据库,就可以查找到了。 locate不会维护/tmp目录下的文件
常用选项
- -c, --count # 只输出找到的数量
- -r, --regexp REGEXP # 使用基本正则表达式
- -i, --ignore-case # 忽略大小写
示例
[root@VM-0-5-centos tmp]# locate /opt/redis-6.2.4/redis.
/opt/redis-6.2.4/redis.conf
which
命令格式
which 命令
命令描述
搜索命令所在目录及别名信息
示例
[root@VM-0-5-centos tmp]# which ls
alias ls='ls --color=auto'
/usr/bin/ls
whereis
命令格式
whereis 命令
命令描述
搜索命令所在目录及帮助文档路径
示例
[root@VM-0-5-centos tmp]# whereis ls
ls: /usr/bin/ls /usr/share/man/man1/ls.1.gz
grep
命令格式
grep -iv [指定字符串] [文件]
命令描述
- 在文件中搜寻字符串匹配的行并输出
- 可以使用正则表达式或通配符来指定字符串
选项
- -i 不区分大小写
- -v 排除指定字符串
示例
[root@a698b274ec6d test]# grep love test.txt
i love you
[root@a698b274ec6d test]# grep -i love test.txt
i love you
i LOVE you
[root@a698b274ec6d test]# grep -v *love* test.txt
i hate you
i LOVE you
[root@a698b274ec6d test]# grep -i -v *love* test.txt
i hate you
[root@VM-0-5-centos tmp]# grep ^i test.txt
i love you
i LOVE you
i hate you
总结
本文介绍了Linux部分文件搜索命令:
- find:在指定目录下查找文件
- locate:使用本地维护的数据库进行查询,数据库会定时更新
- which:搜索命令所在目录及别名信息
- whereis:搜索命令所在目录及帮助文档路径
- grep:找文件里符合条件的字符串
更多
个人博客: lifelmy.github.io/
微信公众号:漫漫Coding路