Linux查找命令

Linux下主要有以下五个查找命令:

  • find
  • locate
  • grep
  • which
  • whereis

find

find命令在某个目录下查找。
一般使用格式:

find <pathname> <-option> <filetype> <action> 

-pathname:所要查找的目录及其所有子目录(默认递归查找)。默认为当前目录。
-option:指定参数。
-filetype:想要查找的文件类型。
-action:对查找结果进行的处理。

常用参数

-name: 按照文件名查找文件。
-size n:[c] 查找文件长度为n块的文件,带有c时表示文件长度以字节计。
-depth:在查找文件时,首先查找当前目录中的文件,然后再在其子目录中查找。
-ctime n:查找系统中最后n*24小时被改变文件状态的文件。
-atime n:查找系统中最后n*24小时访问的文件
-type: 查找某一类型的文件,如:

-b : 块设备文件。
-c : 字符设备文件。
-d : 目录。
-f : 普通文件。
-l : 符号链接文件。
-p : 管道文件。

例子:

find . -name a* -ls

功能
在当前目录中查找所有以a开头的文件,并显示它们的详细信息。

输出

slot@slot-ubt:~/test$ find . -name a* -ls
   397656      4 -rw-rw-r--   2 slot     slot           13 12月  3 16:21 ./aa

locate

locate命令在系统的全局范围内查找。
一般使用格式:

locate <-option> <filetype>

-option:指定参数。
-filetype:想要查找的文件类型。

常用参数:

-d:指定要查找的数据库路径
-h: 显示辅助讯息
-i:忽略大小写
-q:安静模式,不会显示任何错误讯息。
-n:至多显示 n个输出。
-r: 使用正规运算式 做寻找的条件。
-o: 指定资料库存的名称。
-V: 显示程式的版本讯息

例子:

slot@slot-ubt:~/test$ locate hello.txt
/usr/share/doc/syslinux-common/asciidoc/hello.txt

注意:
locate是从数据库中读取数据,而不是从文件系统中读取。从数据库中读取时是读取updatedb命令返回的结果:

《Linux查找命令》

而updatedb命令默认是一天(24小时)才自动运行一次,这就意味着如果是最新创建的文件,使用locate命令可能查找不到。

解决方法:
在使用locate命令前,先手动运行updatedb命令(需要root权限):

sudo updatedb

例子:
首先新建一个名为haha.ha的文件,然后用locate命令查找该文件,发现查找不到结果:

slot@slot-ubt:~/test$ touch haha.ha
slot@slot-ubt:~/test$ 
slot@slot-ubt:~/test$ locate haha.ha
slot@slot-ubt:~/test$ 

使用updatedb命令更新后再查找,可以找到:

slot@slot-ubt:~/test$ sudo updatedb
[sudo] password for slot: 
slot@slot-ubt:~/test$ 
slot@slot-ubt:~/test$ locate haha.ha
/home/slot/test/haha.ha
slot@slot-ubt:~/test$ 

grep

和find及locate命令不同的是,grep命令是在指定文件中搜索特定的内容,然后将包含有这些匹配内容的行输出到标准输出。如果不指定文件名,则从标准输入读取内容。grep命令经常和find等命令结合使用,其中grep常充当“过滤器”的角色。

常用参数:
-c: 只输出匹配行的行数。
-h: 查询多文件时不显示文件名。
-I: 不区分大小写(只适用于单字符)。
-n: 显示匹配的行和行号。
-s: 不显示 不存在或无匹配文本 的错误信息。
-v: 反向匹配,即显示不包含匹配文本的所有行。
-R: 递归查询,即连同子目录中的文件一起查询。

例子:在目录~/test下递归查找包含字符串”hello”的所有文件,并显示匹配行的行号

slot@slot-ubt:~/test$ grep -Rn "hello" ~/test 
/home/slot/test/aa:1:hello world!
/home/slot/test/cc:1:hello world!
/home/slot/test/bb:1:hello world!

注意,这里文件bb是文件aa的符号链接:

slot@slot-ubt:~/test$ ls -l
total 8
-rw-rw-r-- 2 slot slot 13 12月  3 16:21 aa
lrwxrwxrwx 1 slot slot  2 12月  3 16:04 bb -> aa
-rw-rw-r-- 2 slot slot 13 12月  3 16:21 cc

如果用-r参数则不显示符号链接:

slot@slot-ubt:~/test$ grep -rn "hello" ~/test
/home/slot/test/aa:1:hello world!
/home/slot/test/cc:1:hello world!

which

which命令将在PATH变量指定的路径中 查找某个系统命令的位置,并且返回第一个搜索结果。也就是说,使用which命令,就可以看到某个系统命令是否存在,以及执行的到底是哪一个位置的命令。

例如,查看当前环境下使用的是哪个bash:

slot@slot-ubt:~/test$ which bash
/bin/bash

whereis

whereis命令用来定位二进制文件(参数-b)、源代码文件(参数-s)和帮助手册文件(即man文件,参数-m)。如果省略参数,则返回所有信息。

例子:

slot@slot-ubt:~/test$ whereis bash
bash: /bin/bash /etc/bash.bashrc /usr/share/man/man1/bash.1.gz
slot@slot-ubt:~/test$ 
slot@slot-ubt:~/test$ whereis -b bash
bash: /bin/bash /etc/bash.bashrc
slot@slot-ubt:~/test$ 
slot@slot-ubt:~/test$ whereis -s bash
bash:
slot@slot-ubt:~/test$ 
slot@slot-ubt:~/test$ whereis -m bash
bash: /usr/share/man/man1/bash.1.gz
slot@slot-ubt:~/test$ 

    原文作者:echoworlding
    原文地址: https://www.jianshu.com/p/72c579528337
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞