首页 > 技术文章 > Linux常用命令——文件搜索命令

Simon212 2019-06-16 11:52 原文

1、文件搜索命令:locate

locate 文件名
--在后台数据库中按文件名搜索,搜索速度更快,只能根据文件名搜索,功能单一

/var/lib/mlocate
--locate命令所搜索的后台数据库

updatedb
--更新数据库
[root@localhost ~]# ls
anaconda-ks.cfg  binaries  binaries.tar.gz  initial-setup-ks.cfg  test123
[root@localhost ~]# locate anaconda-ks.cfg 
/root/anaconda-ks.cfg
[root@localhost ~]# touch test3
[root@localhost ~]# locate test3  --后台数据库不是实时更新,更新频率为一天一次
[root@localhost ~]# updatedb      --更新数据库
[root@localhost ~]# locate test3
/root/test3

  locate文件遵守/etc/update.conf配置文件。https://www.cnblogs.com/Simon212/p/11031075.html

 2、命令搜索命令:whereis和which

whereis 命令名
    搜索命令所在路径及帮助文档所在位置
        选项:
            -b:  只查找可执行文件
            -m: 只查找帮助文件

  

which 文件名
    搜索命令所在路径及别名

  

[root@localhost ~]# whereis ls
ls: /usr/bin/ls /usr/share/man/man1/ls.1.gz /usr/share/man/man1p/ls.1p.gz
[root@localhost ~]# which ls
alias ls='ls --color=auto'
	/usr/bin/ls

3、文件搜索命令:find 

find [搜索范围] [搜索条件]
    搜索文件
    避免大范围搜索,会非常消耗系统资源
    find是在系统中搜索符合条件的文件名。如果需要匹配,使用通配符匹配,通配符是完全匹配

  Linux中的通配符:

  *  匹配任意内容

  ?  匹配任意一个字符

  []  匹配任意一个中括号内的字符

[root@localhost ~]# ls
anaconda-ks.cfg  binaries  binaries.tar.gz  initial-setup-ks.cfg  test123  test3
[root@localhost ~]# find / -name binaries
/root/binaries
[root@localhost ~]# find /root -name "binaries*"
/root/binaries.tar.gz
/root/binaries
[root@localhost ~]# find /root -name "test[123]"
/root/test123/test2
/root/test3

 find支持的其他命令:

[root@localhost ~]# find /root -iname binaries   --不区分大小写

  

[root@localhost ~]# find /root -user root  --按照所有者搜索
/root
/root/.bash_logout
/root/.bash_profile
/root/.bashrc
/root/.cshrc
/root/.tcshrc
/root/anaconda-ks.cfg
/root/.cache
/root/.cache/dconf
/root/.cache/dconf/user
/root/.cache/abrt
/root/.cache/abrt/lastnotification
/root/.dbus
/root/.dbus/session-bus
/root/.dbus/session-bus/9621f505534747eb82894e790c8120e1-9
/root/initial-setup-ks.cfg
/root/.config
/root/.config/abrt
/root/.bash_history
/root/.bashrc_test
/root/.xauth91pRTQ
/root/binaries.tar.gz
/root/.xauthsXfZnT
/root/.viminfo
/root/test123
/root/test123/test2
/root/.Xauthority
/root/.anaconda-ks.cfg.swp
/root/test3

  

[root@localhost ~]# find /root -nouser    --查找没有所有者的文件
    Linux没有所有者的文件基本上是垃圾文件,但有两个例外
    1.由内核产生的文件不经过系统用户,有可能没有所有者
    2.外来文件有可能没有所有者

  

find /var/log/ -mtime +10
    查找10天前修改的文件
    
    
    -10        10天内修改的文件
    10         10天当天修改的文件
    +10        10天前修改的文件

    atime        文件访问时间
    ctime        改变文件属性
    mtime        修改文件内容

  

find . -size 25k
    查找文件大小是25KB的文件

    -25k    小于25KB的文件
    25k     等于25KB的文件
    +25k    大于25KB的文件
  搜索千字节需要小写,搜索兆字节需要大写
  默认的单位是按照扇区划分的数据块,建议最好加入单位

  

find . inum 262422
    查找i节点是262422的文件

  

find /etc -size +20k -a -size -50k -exec ls -lh {} \;
    查找/etc/目录下,大于20KB并且小于50KB的文件,并显示详细信息
        -a    and    逻辑与,两个条件都满足
        -o    or      逻辑或,两个条件满足一个即可
        -exec/-ok 命令 {} \;     对搜索结果执行操作

4、字符串搜索命令grep

grep [选项] 字符串 文件名
    在文件当中匹配符合条件的字符串
    选项:
        -i    忽略大小写
        -v   排除指定字符串

5、find命令和grep命令的区别

find命令:在系统当中搜索符合条件的文件名,如果需要匹配,使用通配符匹配,通配符是完全匹配

grep命令:在文件当中搜索符合条件的字符串,如果需要匹配,使用正则表达式进行匹配,正则表达式是包含匹配

推荐阅读