Linux必备命令:find/locate/grep/which/whereis

1、find:查找文件或者目录

find 指令将从指定的目录向下递归地遍历其各个子目录,将满足条件的文件显示在终端。

1)基本语法

find [path...] [选项]

2)选项说明

选项说明
-name 文件名称按文件名查找文件
-user 用户名查找属于指定用户名的所有文件
-size [+-]n[bcwkMG]按照指定多文件大小查找文件
n:数字,表示文件大小,n 后面可以跟单位
单位为(区分大小写):
b:块(512 字节)
c:字节
w:字(2 字节)
k:千字节
M:兆字节
G:吉字节

3)案例

案例说明
find -name 1.txt在当前目录及其所有子目录中查询 1.txt 文件
find / -name 1.txt在/目录及其所有子目录中递归查找 1.txt 文件
find / -name *.txt在/目录及其所有子目录中递归查找.txt 结尾的文件
find /opt/ /home/ -name *.txt在[/opt,/home]两个目录中递归查找.txt 结尾的文件
find / -size 10M在/目录递归查找文件大小为 10M 的文件
find / -size +10M在/目录递归查找大于 10M 的文件
find / -size +10M -size -100M在/目录递归查找大于 10M 且小于 100M 的文件
find / -name *.log -size +50M -size -100M在/opt 目录中查找名称以.log 结尾的且大于 50M 小于 100M 的文件

2、locate:快速定位文件路径

locate 命令和find命令一样都是用来在系统下查找文件或目录。但 locate 命令要比 find -name 快得多,原因在于locate命令在查找文件时并不扫描具体目录,而是搜索一个已经创建好的数据库/var/lib/mlocate/mlocate.db 。这个数据库中含有本地几乎所有文件信息(一些被排除在外的目录或刚创建的目录可能不会包含在数据库中。Linux 系统会自动创建这个数据库,并且通过定时任务每天自动更新一次,因此,我们在用 whereis 和 locate 查找文件时,有时会找到已经被删除的数据,或者刚刚建立文件,却无法查找到,原因就是因为数据库文件没有被更新。为了避免这种情况,可以在使用locate之前,先使用updatedb命令,手动更新数据库,但这也需要一定的时间,时间长短和空间大小文件数量有关。整个 locate 工作其实是由四部分组成的:

  1. /usr/bin/updatedb 主要用来更新数据库,通过 crontab 自动完成的

  2. /usr/bin/locate 查询文件位置

  3. /etc/updatedb.conf updatedb 的配置文件

  4. /var/lib/mlocate/mlocate.db 存放文件索引信息的数据文件

1)语法

locate [OPTION]... [PATTERN]...

2)选项

  -b, --basename         match only the base name of path names
  -c, --count            只输出找到的数量
  -d, --database DBPATH  使用DBPATH指定的数据库,而不是默认数据库 /var/lib/mlocate/mlocate.db
  -e, --existing         only print entries for currently existing files
  -L, --follow           follow trailing symbolic links when checking file existence (default)
  -h, --help             显示帮助
  -i, --ignore-case      忽略大小写
  -l, --limit, -n LIMIT  limit output (or counting) to LIMIT entries
  -m, --mmap             ignored, for backward compatibility
  -P, --nofollow, -H     don't follow trailing symbolic links when checking file existence
  -0, --null             separate entries with NUL on output
  -S, --statistics       don't search for entries, print statistics about eachused database
  -q, --quiet            安静模式,不会显示任何错误讯息
  -r, --regexp REGEXP    使用基本正则表达式
      --regex            使用扩展正则表达式
  -s, --stdio            ignored, for backward compatibility
  -V, --version          显示版本信息
  -w, --wholename        match whole path name (default)

3)经验技巧

由于 locate 指令基于数据库进行查询, 所以第一次运行前, 必须使用 updatedb 指令创建 locate 数据库。

4)查找文件

[root@testx ~]# updatedb
[root@testx ~]# locate 1.txt
/etc/brltty/brl-ts-pb65_pb81.txt
/etc/pki/nssdb/pkcs11.txt
/home/jack/1.txt
/home/jack/b/1.txt
/home/tom/1.txt

3、grep:过滤查找即“|”管道符

管道符,“|”,表示将前一个命令的处理结果输出通过管道传递给后面的命令进行处理。

1)基本语法

grep 选项 查找内容 源文件

2)选项说明

选项功能
-n显示匹配行及行号
-v反向匹配

3)案例

查找 1.txt 中 ready 在哪些行

下面先使用 cat 命令查看 1.txt 的内容。

然后后面有使用了 2 种方式检索 ready 位于 1.txt 中所在行号及内容。

第 1 种:grep -n ready 1.txt

第 2 种:cat 1.txt | grep -n ready,这里使用到了管道命令,将 cat 1.txt 的结果通过管道命令传递给 grep,grep 将传递过来的内容作为查找目标。

[root@testx ~]# cat 1.txt
hello
不错哦 ready
ok
ready
[root@testx ~]# grep -n ready 1.txt
2:不错哦 ready
4:ready
[root@testx ~]# cat 1.txt | grep -n ready
2:不错哦 ready
4:ready
[root@test001 ~]# grep -v ready 1.txt
hello
ok

4、which:在 PATH 下查找命令位置

这条命令主要是用来查找系统**PATH 目录下**的可执行文件,说白了就是查找那些我们已经安装好的可以直接执行的命令,比如

[root@testx ~]# which useradd
/usr/sbin/useradd
[root@testx ~]# which ls
alias ls='ls --color=auto'
 /usr/bin/ls

which 查找的可执行文件,必须是要在 PATH 下的可执行文件,而不能是没有加入 PATH 的可执行文件,即使他就是可执行文件,但是没有加入到系统搜索路径,他仍然无法被 which 发现(好吧,有点啰嗦了)。

5、whereis:查找可执行文件路径

whichwhereis命令都是 Linux 操作系统下查找可执行文件路径的命令。

whereis这个命令可以用来查找二进制(命令)、源文件、man 文件。与which不同的是这条命令可以是通过文件索引数据库而非 PATH 来查找的,所以查找的面比which要广。例如:

[root@testx ~]# whereis ls
ls: /usr/bin/ls /usr/share/man/man1/ls.1.gz /usr/share/man/man1p/ls.1p.gz
[root@testx ~]# whereis useradd
useradd: /usr/sbin/useradd /usr/share/man/man8/useradd.8.gz

可以看到,whereis不仅找到了 ls 可执行文件的位置,还找到了其 man 帮助文件,可见其搜索范围比较广,不局限于 PATH。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值