linux环境下常用的查找命令find、which、grep
文章目录
- 前言
- find
- 命令格式
- 具体示例
- locate
- 命令格式
- 具体示例
- whereis
- 命令格式
- 具体示例
- which
- 命令格式
- 具体示例
- grep
- 命令格式
- 具体示例
- 总结
前言
查找是运维工作的很重要的一部分,不管是文件查找,还是内容查找,在日常开发维护过程中都常常用到,本文把一些日常用到的查找命令总结到一起,通过对比来学习异同点,进而达到 增强记忆的目的。
本文只是想对常用命令进行一个罗列,并不会对每个命令进行详细的解释,如果想看更详细的用法,直接查询 man
手册是一个不错的选择,我们接下来会说到通用文件查找的 find
命令,快速定位文件的 locate
命令,仅用于搜索程序和文档的 whereis
命令,用于查找系统命令的 which
命令,最后是用于文件内容查找的 grep
命令。
find
命令格式
find [指定目录] 搜索条件 [指定动作]
具体示例
- 全局查找tendis文件所在目录
[root@VM-0-3-centos ~]# find / -name tendis
/root/tendis
- 当前目录按指定名找到tendis并打印文件信息
[root@VM-0-3-centos ~]# find . -name tendis -ls
918146 4 drwxr-xr-x 4 root root 4096 May 1 2021 ./tendis
- 全局查找test开头的文件
[root@VM-0-3-centos ~]# find / -name 'test*'
/boot/grub2/i386-pc/testspeed.mod
/boot/grub2/i386-pc/test.mod
/boot/grub2/i386-pc/test_blockarg.mod
/boot/grub2/i386-pc/testload.mod
/usr/lib/modules/3.10.0-1127.19.1.el7.x86_64/kernel/drivers/ntb/test
/usr/lib/python2.7/site-packages/jinja2/tests.pyc
/usr/lib/python2.7/site-packages/jinja2/tests.py
/usr/lib/python2.7/site-packages/jinja2/testsuite
...
- 当前目录下查找所有的目录
[root@VM-0-3-centos ~]# find . -type d
.
./tendis
./tendis/scripts
./tendis/bin
./tendis/bin/deps
./extundelete-0.2.4
...
- 查找大于1M的文件
[root@VM-0-3-centos ~]# find . -size +1M -ls
918152 164712 -rwxr-xr-x 1 root root 168663910 Dec 17 2020 ./tendis/bin/tendisplus_static
918151 18036 -rwxr-xr-x 1 root root 18464898 Dec 17 2020 ./tendis/bin/binlog_tool
918148 2576 -rwxr-xr-x 1 root root 2635759 Dec 17 2020 ./tendis/bin/redis-cli
918150 10896 -rwxr-xr-x 1 root root 11154937 Dec 17 2020 ./tendis/bin/deps/libstdc++.so.6
918145 165076 -rwxr-xr-x 1 root root 169036319 Dec 17 2020 ./tendis/bin/tendisplus
1311915 1860 -rw-r--r-- 1 root root 1904320 Nov 28 2021 ./extundelete-0.2.4/src/extundelete-extundelete.o
1311926 1296 -rwxr-xr-x 1 root root 1323360 Nov 28 2021 ./extundelete-0.2.4/src/extundelete
...
- 查找10分钟内修改的普通文件
[root@VM-0-3-centos ~]# find . -type f -mmin -10
./b.txt
./.bash_history
locate
locate 也是用来查找文件的,只不过它不是通过文件系统来找,而是通过自己的数据库来找,默认在 /var/lib/mlocate/mlocate.db
,每天自动更新一次,所以查不到最新变动的文件,可以手动通过 updatedb
来更新数据库(我查了一下才2M很小的)。
命令格式
locate [选项] [匹配串]
具体示例
- 查找家目录下包含te的文件
[root@VM-0-3-centos ~]# locate ~/te
/root/tendis
/root/test.iso
/root/tendis/bin
/root/tendis/file.xml
/root/tendis/scripts
/root/tendis/bin/binlog_tool
/root/tendis/bin/deps
/root/tendis/bin/redis-cli
/root/tendis/bin/tendisplus
/root/tendis/bin/tendisplus_static
/root/tendis/bin/deps/libstdc++.so.6
/root/tendis/scripts/start.sh
/root/tendis/scripts/stop.sh
/root/tendis/scripts/tendisplus.conf
- 不区分大小写查找
[root@VM-0-3-centos ~]# locate -i ~/tE
/root/TE.txt
/root/tendis
/root/test.iso
/root/tendis/bin
/root/tendis/file.xml
/root/tendis/scripts
/root/tendis/bin/binlog_tool
/root/tendis/bin/deps
/root/tendis/bin/redis-cli
/root/tendis/bin/tendisplus
/root/tendis/bin/tendisplus_static
/root/tendis/bin/deps/libstdc++.so.6
/root/tendis/scripts/start.sh
/root/tendis/scripts/stop.sh
/root/tendis/scripts/tendisplus.conf
whereis
whereis 只能用于二进制文件、man手册和源代码文件的搜索,默认返回所有信息。
命令格式
whereis [-bmsBMS] 匹配串
具体示例
- 查找二进制程序
ls
[root@VM-0-3-centos ~]# whereis -b ls
ls: /usr/bin/ls
- 查找
grep
所有信息
[root@VM-0-3-centos ~]# whereis grep
grep: /usr/bin/grep /usr/share/man/man1/grep.1.gz
which
which 是在 PATH 变量中找到第一个匹配的命令并返回,这能帮助我们确认多个相同命令时用的是哪一个。
命令格式
which [选项] 匹配串
具体示例
- 打印当前使用的gcc程序,打印所有可加
-a
参数
[root@VM-0-3-centos ~]# which gcc
/usr/bin/gcc
[root@VM-0-3-centos ~]# which gcc -a
/usr/bin/gcc
grep
grep 不算是单纯查找文件的命令,更多的是用于从文件中过滤指定内容。
命令格式
grep [选项] 匹配串 [指定文件]
具体示例
- 过滤包含指定字符串的行
[root@VM-0-3-centos ~]# grep "which" w.txt
which - shows the full path of (shell) commands.
which [options] [--] programname [...]
This man page is generated from the file which.texinfo.
- 显示匹配行之后的2行
[root@VM-0-3-centos ~]# grep "which" w.txt -A 2
which - shows the full path of (shell) commands.
SYNOPSIS
which [options] [--] programname [...]
DESCRIPTION
--
This man page is generated from the file which.texinfo.
OPTIONS
--
- 当前目录下查找包含
wonderful
的文件
[root@VM-0-3-centos ~]# grep -r "wonderful" .
./.rediscli_history:hset life family wonderful
./.bash_history:grep -r "wonderful" . | head
总结
find
命令查找文件最全面find . -name tendis -ls
locate
命令查找最快,locate -i /etc/redis
,可用updatedb
命令更新数据库whereis
命令可以查找二进制、man手册、源码,whereis -b grep
which
可以从PATH路径下找到第一个匹配的二进制程序grep
一个强大的过滤命令,也可用于找文件grep -r "wonderful" .
幸福感从比较中诞生,亦从比较中消亡,并且与比较双方的关系紧密程度高度相关。我有一块糖,而你没有,我就很幸福,转身发现他有10块糖,然后嘴里的糖瞬间就不甜了~