linux下查找(find,grep,which,whereis,locate)命令使用方法

目录

0、通配符介绍

一、find命令:查找文件

二、grep命令:查找文件中的字符/字符串

三、which命令:查找系统可执行命令的路径

四、whereis命令:查找bin文件,源文件,帮助文件的位置

五、locate命令:与whereis类型,但是搜索出的东西更多

六、总结


注:
1、以下测试在ubuntu 9.10(32位)上进行,其他平台未测试
2、本文只是我个人的理解,不一定完全正确
3、版权所有,转载请注明作者和出处

0、通配符介绍

下面介绍几个linux常用的通配符
*    :星号,通配任意的单个字符或字符串
?    :问号,通配任意的单个字符
[]   :方括号,通配方括号内的任意一个字符

[a-zA-Z] :表示匹配一个字符,这个字符必须是a-z或A-Z这52个字母中的一个
[^123]   :匹配一个字符,这个字符是除了1、2、3以外的所有字符
[A-Za-z]      等价于 [[:alpha:]]
[0-9]         等价于 [[:digit:]]
[A-Za-z0-9]   等价于 [[:alnum:]]
tab,space 等空白字符 [[:space:]]
[A-Z]         等价于 [[:upper:]]
[a-z]         等价于 [[:lower:]]
标点符号             [[:punct:]]

一、find命令:查找文件

1、命令帮助

2、命令详解

语法:  find [路径path][选项][查找条件]

参数说明:
如果 path 是空字串则使用当前路径。

-mount, -xdev : 只检查和指定目录在同一个文件系统下的文件,避免列出其它文件系统中的文件
-amin n       : 在过去 n 分钟内被读取过的文件
-anewer file  : 比文件 file 更晚被读取过的文件
-atime n      : 在过去 n 天内被读取过的文件
-cmin n       : 在过去 n 分钟内被修改过的文件
-cnewer file  : 比文件 file 更新的文件
-ctime n      : 在过去 n 天内被修改过的文件
-empty        : 空的文件
-gid n        : gid 是 n 的文件
-group name   : group 名称是name的文件
-ipath p, -path p       : 路径名称符合 p 的文件,ipath 会忽略大小写
-name name, -iname name : 文件名称符合 name 的文件。iname 会忽略大小写
-size n : 文件大小n,n的单位有
          `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)
-type c : 文件类型是 c 的文件
          File is of type c:
          b      block (buffered) special
          c      character (unbuffered) special
          d      directory
          p      named pipe (FIFO)
          f      regular file
          l      symbolic link;
          s      socket
          D      door (Solaris)

3、实例

 

二、grep命令:查找文件中的字符/字符串

1、命令帮助

2、命令详解

命令选项:
Regexp selection and interpretation:
  -E, --extended-regexp     PATTERN is an extended regular expression (ERE)
  -F, --fixed-strings       PATTERN is a set of newline-separated fixed strings
  -G, --basic-regexp        PATTERN is a basic regular expression (BRE)        
  -P, --perl-regexp         PATTERN is a Perl regular expression               
  -e, --regexp=PATTERN      use PATTERN for matching                           
  -f, --file=FILE           obtain PATTERN from FILE                           
  -i, --ignore-case         ignore case distinctions                           
  -w, --word-regexp         force PATTERN to match only whole words            
  -x, --line-regexp         force PATTERN to match only whole lines            
  -z, --null-data           a data line ends in 0 byte, not newline            

Miscellaneous:
  -s, --no-messages         suppress error messages
  -v, --invert-match        select non-matching lines
  -V, --version             print version information and exit
      --help                display this help and exit        
      --mmap                use memory-mapped input if possible

Output control:
  -m, --max-count=NUM       stop after NUM matches
  -b, --byte-offset         print the byte offset with output lines
  -n, --line-number         print line number with output lines    
      --line-buffered       flush output on every line             
  -H, --with-filename       print the filename for each match      
  -h, --no-filename         suppress the prefixing filename on output
      --label=LABEL         print LABEL as filename for standard input
  -o, --only-matching       show only the part of a line matching PATTERN
  -q, --quiet, --silent     suppress all normal output                   
      --binary-files=TYPE   assume that binary files are TYPE;           
                            TYPE is `binary', `text', or `without-match' 
  -a, --text                equivalent to --binary-files=text            
  -I                        equivalent to --binary-files=without-match   
  -d, --directories=ACTION  how to handle directories;                   
                            ACTION is `read', `recurse', or `skip'       
  -D, --devices=ACTION      how to handle devices, FIFOs and sockets;    
                            ACTION is `read' or `skip'                   
  -R, -r, --recursive       equivalent to --directories=recurse          
      --include=FILE_PATTERN  search only files that match FILE_PATTERN  
      --exclude=FILE_PATTERN  skip files and directories matching FILE_PATTERN
      --exclude-from=FILE   skip files matching any file pattern from FILE    
      --exclude-dir=PATTERN directories that match PATTERN will be skipped.
  -L, --files-without-match print only names of FILEs containing no match
  -l, --files-with-matches  print only names of FILEs containing matches
  -c, --count               print only a count of matching lines per FILE
  -T, --initial-tab         make tabs line up (if needed)
  -Z, --null                print 0 byte after FILE name

Context control:
  -B, --before-context=NUM  print NUM lines of leading context
  -A, --after-context=NUM   print NUM lines of trailing context
  -C, --context=NUM         print NUM lines of output context
  -NUM                      same as --context=NUM
      --color[=WHEN],
      --colour[=WHEN]       use markers to highlight the matching strings;
                            WHEN is `always', `never', or `auto'
  -U, --binary              do not strip CR characters at EOL (MSDOS)
  -u, --unix-byte-offsets   report offsets as if CRs were not there (MSDOS)

`egrep' means `grep -E'.  `fgrep' means `grep -F'.

常用选项解释:
-G, --basic-regexp,BRE 模式,也是默认的模式
-P, --perl-regexp ,PRE 模式
-E :开启扩展(Extend)的正则表达式模式,即ERE模式。
-i :忽略大小写(ignore case)。
-v :反过来(invert)输出,即只打印没有匹配的,而匹配的反而不打印。
-n :显示行号。
-w :被匹配的文本只能是单词。
-c :显示总共有多少行被匹配到了,而不是显示被匹配到的内容。
-o :只显示被模式匹配到的字符串。
-A     n:显示匹配到的字符串所在的行及其后n行,after。
-B     n:显示匹配到的字符串所在的行及其前n行,before。
-C     n:显示匹配到的字符串所在的行及其前后各n行,context。
--color : 将匹配到的内容以颜色高亮显示。

3、实例

三、which命令:查找系统可执行命令的路径

1、命令帮助

2、命令详解

格式:which 系统可执行文件名称

一般用于确认系统中是否安装了指定的软件。
which指令会在环境变量$PATH变量指定的路径中搜索某个系统命令的位置,并且返回第一个搜索结果。

常用选项:
-a	列出所有匹配的路径
-n	指定文件名长度,指定的长度必须大于或等于所有文件中最长的文件名。
-p	与-n参数相同,但此处的文件名长度包括了文件的路径。
-V	显示版本信息

3、实例

四、whereis命令:查找bin文件,源文件,帮助文件的位置

1、命令帮助

2、命令详解

格式:whereis [选项] 文件名

常用选项:
-b	搜索bin文件
-m	搜索帮助文件
-s	搜索源代码文件
-u	搜索默认路径下除bin文件、源代码文件、帮助文件以外的其它文件
-B	指定搜索bin文件的路径
-M	指定搜索帮助文件的路径
-S	指定搜索源代码文件的路径

whereis命令搜索的是linux自动创建的包含所有文件信息的数据库,而不是磁盘上的真实文件数据,所以刚创建的文件可能搜不到,因为数据库每天才更新一次,这个要注意。

3、实例

五、locate命令:与whereis类型,但是搜索出的东西更多

1、命令帮助

 2、命令详解

格式:locate [选项] [搜索的字符串]

常用选项:
-q	 安静模式,不会显示任何错误信息
-l x	至多显示 x 个输出
-r	使用正则表达式做寻找的条件
-V	显示版本信息

locate命令搜索的位置和whereis相同,即搜索数据库,但是locate命令可以搜索除了whereis可以搜索的bin文件,源文件和帮助文件外的文件,搜索除的内容更丰富

3、实例

六、总结

linux下查找等命令功能十分的过于强大,再加上正则表达式的配合,几乎无所不能了,就是前期学习时要多多的练习,不然过些天就忘记了。对比Windows下的查找功能,比linux的弱很多很多啦。

2020-04-22 ~2020-04-23 晴

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值