Linux文档档名搜索

本文详细介绍了Linux系统中用于查找文件的三个重要命令:whereis、locate和find。whereis主要用于查找二进制文件、源代码和手册页;locate通过更新数据库快速定位文件,而find则提供了丰富的选项,如时间条件、文件模式、权限模式等,进行灵活的文件搜索。此外,还讲解了如何结合使用这些命令排除特定目录、查找特定类型的文件和指定大小的文件。
摘要由CSDN通过智能技术生成

学习目标:

Linux速成五、


学习内容:

几个文件搜索语令:
1、whereis
2、locate/updatedb
3、find


1、whereis:由一些特定的目录中寻找档案档名

  • whereis 主要是针对/bin /sbin 底下的执行档, 以及/usr/share/man 底下的man page 档案

1、-l:可以列出whereis会去查询的几个主要目录而已
2、-b:只找binary格式的档案
3、-m:只找在说明档manua1路径下的档案
4、-s:只找source来源档案
5、-u∶搜寻不在上述三个项目当中的其他特殊档案

[root@bogon ~]# whereis ifconfig
ifconfig: /usr/sbin/ifconfig /usr/share/man/man8/ifconfig.8.gz
[root@bogon ~]# whereis -b a*
anaconda-ks:
[root@bogon ~]# whereis -m passwd
passwd: /usr/share/man/man1/passwd.1.gz /usr/share/man/man5/passwd.5.gz

2、locate/updatedb

  • updatedb:根据/etc/updatedb.conf 的设定去搜寻系统硬碟内的档名,并更新/var/lib/mlocate内的资料库档案
  • locate:依据/var/lib/mlocate 内的资料库记载,找出使用者输入的关键字档名
    1、-i :忽略大小写的差异
    2、-c :不输出档名,仅计算找到的档案数量
    3、-l : 仅输出几行的意思,eg:输出五行则是-l 5
    4、-S:输出locate.所使用的资料库档案的相关资讯,包括该资料库纪录的档案/目录数量等
    5、-r :后面可接正规表示法的显示方式
locate第一次运行,需要先输入updatedb以更新库

[root@bogon ~]# updatedb
[root@bogon ~]# locate mkdir
/usr/bin/mkdir
/usr/lib/python3.6/site-packages/pip/_vendor/lockfile/mkdirlockfile.py
/usr/lib/python3.6/site-packages/pip/_vendor/lockfile/__pycache__/mkdirlockfile.cpython-36.opt-1.pyc
/usr/lib/python3.6/site-packages/pip/_vendor/lockfile/__pycache__/mkdirlockfile.cpython-36.pyc
/usr/share/man/man1/mkdir.1.gz
/usr/share/man/man1p/mkdir.1p.gz
/usr/share/man/man2/mkdir.2.gz
/usr/share/man/man2/mkdirat.2.gz
/usr/share/man/man3p/mkdir.3p.gz
[root@bogon ~]# locate -l 3 passwd
/etc/passwd
/etc/passwd-
/etc/pam.d/passwd
[root@bogon ~]# locate -S
数据库 /var/lib/mlocate/mlocate.db:
 6,644 文件夹
 56,010 文件
 2,685,259 文件名中的字节数
 1,347,511 字节用于存储数据库

3、find

1、与时间有关的选项,共有-atime, -ctime与-mtime,以-mtime 说明:

  • -mtime n : n为数字,意义为在n天之前的(一天之内)被更动过内容的档案
  • -mtime +n :列出在n天之前(不含n天本身)被更动过内容的档案档名
  • -mtime n :列出在n天之内(含n天本身)被更动过内容的档案档名
  • -newer file : file 为一个存在的档案, 列出比file还要新的档案档名

2、利用某种文件命名模式来寻找文件,文件模式要用引号引起来

find 目录位置 -name “文件模式” -print

-寻找以.log结尾的文件
 find ~ -name "*.log" -print
[root@192 ~]# find / -name "*.log" -print
/var/log/sssd/sssd.log
/var/log/sssd/sssd_implicit_files.log
/var/log/sssd/sssd_nss.log
/var/log/tuned/tuned.log
...
-想要的当前目录及子目录中查找文件名以一个大写字母开头的文件
    find . -name "[A-Z]*" -print  
[root@192 /]# find . -name "B*" -print
./sys/firmware/acpi/tables/BOOT
./var/lib/rpm/Basenames
./var/cache/dnf/BaseOS-929b586ef1f72f69
./var/cache/dnf/BaseOS.solv
./var/cache/dnf/BaseOS-filenames.solvx
...
 - 想要在/etc目录中查找文件名以host开头的文件
  find /etc -name "host*" -print
[root@192 /]# find /etc -name "host*" -print
/etc/host.conf
/etc/hosts
/etc/hostname
 - 要想让系统高负荷运行,就从根目录开始查找所有的文件
find / -name "*" -print 
[root@192 /]# find / -name "*" -print
/proc/167/task/167/clear_refs
/proc/167/task/167/smaps
/proc/167/task/167/smaps_rollup
/proc/167/task/167/pagemap
...

3、用perm按文件权限模式来查找文件
在八进制数字前面要加一个横杠-,表示都匹配,如-007就相当于777,-005相当于555

find 目录位置 -perm 权限值 -print

在当前目录下查找文件权限位为755的文件
[root@192 /]# find . -perm 755 -print
./usr/share/locale/sc
./usr/share/locale/sc/LC_MESSAGES
./usr/share/locale/scn
./usr/share/locale/scn/LC_MESSAGES
./usr/share/locale/sco
./usr/share/locale/sco/LC_MESSAGES
...

4、在查找文件时希望忽略某个目录,那么可以使用-prune选项来指出需要忽略的目录

find 需要查找的目录 -path “忽略的文件夹” -prune -o -print
a 和 -o 都是短路求值,与 shell 的 && 和 || 类似

如果希望在test目录下查找文件,但不希望在test/test3目录下查找
[root@bogon hello]# ls -l
总用量 4
drwxr-xr-x. 2 root root 41 1024 08:39 test
drwxr-xr-x. 2 root root  6 1024 08:39 test1
drwxr-xr-x. 2 root root  6 1024 08:39 test2
-rw-r--r--. 1 root root 11 1023 23:16 yyt
[root@bogon hello]# find test -path "test/test3" -prune -o -print
test
test/yyt
test/yyt1
test/yyt2

 - 在test目录下查找不在test4子目录之内的所有文件

[root@bogon hello]# find test
test
test/yyt1
test/yyt2
test/test4
test/test4/yyt1
test/yyt
test/wy
test/test5
test/test5/wy
[root@bogon hello]# find test -path "test/test4" -prune -o -print
test
test/yyt1
test/yyt2
test/yyt
test/wy

 - 在test目录下查找不在test4和test5中的所有文件,“()”表示表达式的结合“\”表示引用

[root@bogon hello]# find test
test
test/yyt1
test/yyt2
test/test4
test/test4/yyt1
test/yyt
test/wy
test/test5
test/test5/wy
[root@bogon hello]# find test \( -path test/test4 -o -path test/test3 \) -prune -o -print
test
test/yyt1
test/yyt2
test/yyt
test/wy
test/test5
test/test5/wy

 - 查找某一确定文件,-name等选项加在-o之后 
 - 在test目录下查找不在test4和test5中的所有以wy开头的文件

[root@bogon hello]# find test
test
test/yyt1
test/yyt2
test/test4
test/test4/yyt1
test/yyt
test/wy
test/test5
test/test5/wy
test/wy1
test/wy2
test/wy3
test/wy4
[root@bogon hello]# find test \( -path test/test4 -o -path test/test5 \) -prune -o -name "wy*" -print
test/wy
test/wy1
test/wy2
test/wy3
test/wy4

5.使用user和nouser查找文件

  • 用user查找属主为某用户的文件
  • 用nouser查找没有属主的文件
查找/目录下属主为root的文件
[root@bogon /]# find . -user yyt -print
./var/spool/mail/yyt
./home/yyt
./home/yyt/.bash_logout
./home/yyt/.bash_profile
./home/yyt/.bashrc
./yyt
查找/目录下没有属主的文件
我把刚才创建的yyt用户删除掉,然后查找残留的属主为yyt的残留文件
[root@bogon /]# userdel yyt
[root@bogon /]# find . -nouser -print
./var/spool/mail/yyt
./home/yyt
./home/yyt/.bash_logout
./home/yyt/.bash_profile
./home/yyt/.bashrc
./yyt

6、使用group和nogroup

  • 用group查找属于为某用户组的文件
  • 用nogroup查找没有属于用户组的文件
查找/目录下属于wy用户组的文件
[root@bogon /]# groupadd wy
[root@bogon /]# touch wy
[root@bogon /]# chgrp wy wy
[root@bogon /]# ls -l wy
-rw-r--r--. 1 root wy 0 1024 09:34 wy
[root@bogon /]# find . -group wy -print
./wy
查找/目录下没有属于用户组的文件
把刚才创建的用户组wy删掉,然后查找残留的属于用户组wy的残留文件
[root@bogon /]# groupdel wy
[root@bogon /]# find . -nogroup -print
./wy

8、查找比某个文件新或旧的文件
查找更改时间比某个文件新但比另一个文件旧的所有文件, “ !”是逻辑非符号

比某个文件新但是比某个文件旧:newest_file_name ! oldest_file_name
比某个文件新:find . -newer 被比较文件 -print

  • 在新建的test目录下,按时间顺序新建yyt、yyt1、yyt2、yyt3
在test下查找比yyt新,比yyt3旧的文件
[root@bogon test]# find -newer yyt ! -newer yyt3
.
./yyt1
./yyt2
./yyt3

在test下查找比yyt2旧的文件
[root@bogon test]# find ! -newer yyt2
./yyt
./yyt1
./yyt2

9.使用type查找某种类型的文件

find 目录位置 -type d -print:查找某目录下的所有目录文件
find 目录位置 -type l -print:查找某目录下的所有的符号链接文件

  • 切换到dev目录
查找dev下的所有的目录文件
[root@bogon dev]# find . -type d -print
.
./dri
./dri/by-path
./snd
./snd/by-path
./vfio
./net
./hugepages
...
查找dev目录下所有的符号链接文件
[root@bogon dev]# find . -type l -print
./dri/by-path/pci-0000:00:0f.0-render
./dri/by-path/pci-0000:00:0f.0-card
./cdrom
./snd/by-path/pci-0000:02:02.0
./initctl
./cl/swap
...

10.使用size查找一定大小的文件

find 目录位置 -size 一定的大小 -print:查找一定大小的文件

在dev目录下查找大于1M的文件
[root@bogon dev]# find . -size +1000c -print
.
./char
在dev下查找小于小于1块的文件(一块等于512字节)
[root@bogon dev]# find . -size -1 -print
./vcsa6
./vcs6
./vcsa5
./vcs5
./vcsa4
./vcs4
...

11.使用depth先匹配所有的文件,再在子目录中查找

find 目录位置 -name “文件名” -depth -print:查找某个文件

find命令从文件系统的根目录开始,查找一个名为yyt.txt的文件
[root@bogon ~]# find / -name "yyt.txt" -depth -print
/root/hello/yyt.txt  

12.使用mount在当前的文件系统中查找文件(不进入其他文件系统)

find 目录位置 -name “文件名” -mount -print

从当前目录开始查找位于本文件系统中文件名以.log结尾的文件
[root@bogon /]# find . -name "*.log" -mount -print
./var/log/sssd/sssd.log
./var/log/sssd/sssd_implicit_files.log
./var/log/sssd/sssd_nss.log
./var/log/tuned/tuned.log
./var/log/audit/audit.log
./var/log/anaconda/anaconda.log
./var/log/anaconda/X.log
...
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值