Linux 文件查找及命令查找

1.locate

它是根据系统本地数据库查找文件,因为数据库每日更新一次,新建文件无法查询到,需要手动更新数据库

使用格式:

locate [OPTION]... PATTERN...

选项:

-i #忽略大小写
-r #支持一些简单的正则表达式,使用锚定行首(^)时需要填写觉得路径

案例:

[root@hebin ~]# yum install mlocate -y      #系统默认没此命令,手动安装

[root@hebin ~]# locate hostname 
/etc/hostname
/etc/dbus-1/system.d/org.freedesktop.hostname1.conf
/etc/selinux/targeted/active/modules/100/hostname
/etc/selinux/targeted/active/modules/100/hostname/cil

[root@hebin ~]# updatedb        #更新数据库
[root@hebin ~]# locate hostname
/etc/hostname
/etc/dbus-1/system.d/org.freedesktop.hostname1.conf
/etc/selinux/targeted/active/modules/100/hostname
/etc/selinux/targeted/active/modules/100/hostname/cil
/etc/selinux/targeted/active/modules/100/hostname/hll
/etc/selinux/targeted/active/modules/100/hostname/lang_ext
/root/hostname
/usr/bin/hostname

[root@hebin ~]# locate -i hostname      #-i  忽略大小写  
/etc/hostname
/etc/dbus-1/system.d/org.freedesktop.hostname1.conf
/etc/selinux/targeted/active/modules/100/hostname
/etc/selinux/targeted/active/modules/100/hostname/cil
/etc/selinux/targeted/active/modules/100/hostname/hll
/etc/selinux/targeted/active/modules/100/hostname/lang_ext
/root/HOSTNAME
/root/hostname

[root@hebin ~]# locate -r hostname$     #支持简单的正则
/etc/hostname
/etc/selinux/targeted/active/modules/100/hostname
/root/hostname
/usr/bin/hostname
/usr/bin/nmtui-hostname
/usr/lib64/gettext/hostname
[root@hebin ~]# locate -r ^hostname     #错误的写法 
[root@hebin ~]# locate  -r  ^/usr/share/doc/hostname
/usr/share/doc/hostname-3.13
/usr/share/doc/hostname-3.13/COPYRIGHT

2.whereis

根据文件类型查找不同类型的文件,比如二进制文件、man帮主文件、源代码文件。

选项:

-b #查找二进制文件
-m #查找man帮助文件
-s #查找源代码文件

案例:

[root@hebin test]# whereis hostname         #显示所有类型的文件
hostname: /usr/bin/hostname /etc/hostname /usr/share/man/man1/hostname.1.gz /usr/share/man/man5/hostname.5.gz

[root@hebin test]# whereis -b hostname      #显示二进制文件
hostname: /usr/bin/hostname /etc/hostname

[root@hebin test]# whereis -m hostname      #显示帮助文件
hostname: /usr/share/man/man1/hostname.1.gz /usr/share/man/man5/hostname.5.gz

[root@hebin test]# whereis -s hostname      #显示源代码文件
hostname:[root@hebin test]# 

3.which

查找命令的绝对路径以及别名

案例:

[root@hebin ~]# which ls
alias ls='ls --color=auto'
    /usr/bin/ls
[root@hebin ~]# which cd
/usr/bin/cd

4.type

显示其他命令的类型,比如可以显示其他命令是否为内置命令

选项:

-a #显示所有命令的绝对路径
-p #只显示外置名的绝对路径,并不显示其他内容

案例:

root@hebin ~]# type cd 
cd is a shell builtin
[root@hebin ~]# type -a cd
cd is a shell builtin
cd is /usr/bin/cd
[root@hebin ~]# type -p ifconfig
/usr/sbin/ifconfig

5.find

查找文件命令

选项:

-name #根据文件名查找
-iname #不区分大小写
-type #根据文件类型查找
d #目录
f #文件,find默认查找文件
-maxdepth #根据层级查找匹配的文件

案例:

[root@hebin ~]# find  /  -name  "hostname"      #根据名称查找
/proc/sys/kernel/hostname
/etc/selinux/targeted/active/modules/100/hostname
/etc/hostname
/root/hostname
/usr/bin/hostname
/usr/lib64/gettext/hostname
[root@hebin ~]# find  /  -iname  "hostname"     #忽略大小写
/proc/sys/kernel/hostname
/etc/selinux/targeted/active/modules/100/hostname
/etc/hostname
/root/hostname
/root/HOSTNAME
/usr/bin/hostname
/usr/lib64/gettext/hostname
/usr/lib64/perl5/auto/Sys/Hostname
[root@hebin ~]# find  /  -iname  "hostname*"    #以hostname开头的文件
/proc/sys/kernel/hostname
/etc/selinux/targeted/active/modules/100/hostname
/etc/hostname
/root/hostname
/root/HOSTNAME
/usr/bin/hostname
/usr/share/man/man1/hostnamectl.1.gz
/usr/share/man/man5/hostname.5.gz
/usr/share/bash-completion/completions/hostnamectl
[root@hebin ~]# find  /  -iname  "*hostname"    #以hostname为结尾的文件
/proc/sys/kernel/hostname
/sys/kernel/debug/tracing/events/syscalls/sys_enter_sethostname
/sys/kernel/debug/tracing/events/syscalls/sys_exit_sethostname
/etc/selinux/targeted/active/modules/100/hostname
/etc/hostname
/usr/lib64/gettext/hostname
/usr/lib64/perl5/auto/Sys/Hostname
[root@hebin ~]# find  /  -iname  "*hostname*"  #文件名中包含hostname的文件
/proc/sys/kernel/hostname
/sys/kernel/debug/tracing/events/syscalls/sys_enter_sethostname
/sys/kernel/debug/tracing/events/syscalls/sys_exit_sethostname
/etc/dbus-1/system.d/org.freedesktop.hostname1.conf
/etc/selinux/targeted/active/modules/100/hostname
/etc/hostname
/root/hostname
/root/HOSTNAME

[root@hebin ~]# find  /etc  -iname  "hostname"   -print
/etc/selinux/targeted/active/modules/100/hostname
/etc/hostname
[root@hebin ~]# find  /etc -maxdepth 2  -iname  "hostname"   -print  #目录层级
/etc/hostname
[root@hebin ~]# find  /etc -maxdepth 1  -iname  "hostname"   -print
/etc/hostname
点赞
  1. 我超凶的哦说道:
    Google Chrome Windows 10
    不错 ,踩踩

发表回复