find - 掌握Linux搜索文件的正确姿势

  • 原创
  • Madman
  • /
  • /
  • 0
  • 7853 次阅读

Linux查找文件-min.png

Synopsis: which命令在PATH系统变量所指定的路径中搜索第一个匹配的结果,即查询某命令是否存在,它的具体执行路径是什么。whereis命令在/usr/bin或/usr/sbin等特定目录中查找文件。which和whereis都只是在指定目录中而非整个文件系统中查找文件,所以速度很快。locate则是在系统自动构建的索引数据库中模糊查找,当你记不清完整文件名时,使用locate查找很方便,缺点是索引并不是实时更新的。find命令可以指定精确的查询条件,比如模糊的文件名、文件类型、文件大小、文件权限、文件的时间戳、文件属主或属组、甚至还支持正则表达式,可以说是非常强大的搜索工具,缺点是速度较慢

1. which

which命令在PATH系统变量所指定的路径中搜索第一个匹配的结果,即查询某命令是否存在,它的具体执行路径是什么

# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

# which passwd
/usr/bin/passwd

2. whereis

whereis命令在/usr/bin/usr/sbin等特定目录中查找文件

# whereis passwd
passwd: /usr/bin/passwd /etc/passwd /usr/share/man/man1/passwd.1.gz /usr/share/man/man5/passwd.5.gz

# whereis -m passwd
passwd: /usr/share/man/man1/passwd.1.gz /usr/share/man/man5/passwd.5.gz

从哪些目录中查找呢?
# whereis -l
bin: /usr/bin
bin: /usr/sbin
bin: /usr/lib
bin: /usr/lib64
bin: /etc
...

3. locate

如果当前系统存在大量的文件,更新全文件系统的索引数据库非常消耗系统资源,而更新是由cron周期性执行updatedb命令。如果你想查找刚添加不久的文件可能没有查询结果,需要手动执行一次该命令,它会根据配置文件/etc/updatedb.conf去更新默认的索引数据库/var/lib/mlocate/mlocate.db

# touch newfile.data
# locate newfile
此时搜索不到结果

# updatedb
# locate *new?ile*
/root/newfile.data

常用选项与参数:

  • -i 忽略大小写的差异
  • -c 不输出文件名,仅计算找到的文件数量
  • -l 仅输出几行的意思,例如输出五行则是 -l 5
  • -S 输出 locate 所使用的数据库相关信息,包括文件和目录的数量等
  • -r 后面可接正则表达式的模式
1. 列出 locate 当前使用的信息库
# locate -S
Database /var/lib/mlocate/mlocate.db:
    10,472 directories
    99,482 files
    5,524,776 bytes in file names
    2,263,811 bytes used to store database

2. 查找文件名中包含 passwd 字符的文件,只显示5条记录
# locate -l 5 passwd
/etc/passwd
/etc/passwd-
/etc/pam.d/passwd
/etc/security/opasswd
/root/Python-3.5.2/Lib/test/keycert.passwd.pem

4. find

基本格式为 find [指定路径] [指定条件] [指定动作],默认会查找当前目录及子目录,默认动作是 -print 把结果输出到屏幕上

4.1 查询条件

(1) -name PATTERN 根据文件名查找

要查找的 PATTERN 基于bash的 file globing 的匹配字符

  • ? 任意单个字符
  • * 任意长度、任意字符
  • [set] 范围内任意字符
  • [^ set] [! set] 范围外任意字符
  • brace expansion 大括号展开,比如/tmp/{a,b,c}对应/tmp/a/tmp/b/tmp/c

-iname PATTERN 忽略字符大小写; -inode n 按 inode 号查找; -links n: 链接数为 n 的文件

查找当前目录及子目录下,所有以 .txt 结尾的文件
# find . -name '*.txt'

(2) -type [bcdpfls] 根据文件类型查找

  • b 块设备
  • c 字符设备
  • d 目录文件
  • p 命名管道
  • f 普通文件
  • l 符号链接
  • s 套接字文件
查找 /etc 目录及子目录下,名字包含 grub 的符号链接文件
# find /etc -type l -name '*grub*'

(3) -size N[bcwkMG] 根据文件大小查找

File uses n units of space. The following suffixes can be used:

  • b for 512-byte blocks (this is the default if no suffix is used)
  • c for bytes
  • w for two-byte words
  • k for Kilobytes (units of 1024 bytes)
  • M for Megabytes (units of 1048576 bytes)
  • G for Gigabytes (units of 1073741824 bytes)
默认单位是字节,这里的文件大小有一个单位的浮动,因为元数据的存在
-size -N : 查找大小为 N 个字节以下的文件[0, N-1]
-size N : 查找大小刚好等于 N 个字节的文件(N-1, N]
-size +N : 查找大小为 N 个字节以上的文件(N, +∞)

比如-size -5M代表[0, 4M], -size 5M代表(4M, 5M], -size +5M代表(5M, +∞)
分类: Linux
标签: find locate whereis which
  • 13373413
  • rafela
  • nabiha
  • chritina
  • ramayah
  • corrion
  • kamisha
  • everlene
  • taneyah
  • rommie
  • tukker
  • fianna
  • kiersa
  • julica
  • johnattan
  • ryunosuke
  • ruban
  • gavril
  • romona
  • bernadetta
  • favion
  • masiela
  • thema
  • corinda
未经允许不得转载: LIFE & SHARE - 王颜公子 » find - 掌握Linux搜索文件的正确姿势

分享

作者

作者头像

Madman

如需 Linux / Python 相关问题付费解答,请按如下方式联系我

0 条评论

暂时还没有评论.

专题系列