Linux文件查找

find, grep, sgrep, rgrep, locate, whereis

whereis 

命令用来定位指令的二进制程序、源代码文件和man手册页等相关文件的路径。

whereis命令只能用于程序名的搜索,而且只搜索二进制文件(参数-b)、man说明文件(参数-m)和源代码文件(参数-s)。如果省略参数,则返回所有信息。

和find相比,whereis查找的速度非常快,这是因为linux系统会将 系统内的所有文件都记录在一个数据库文件中,当使用whereis和下面即将介绍的locate时,会从数据库中查找数据,而不是像find命令那样,通过遍历硬盘来查找,效率自然会很高。 但是该数据库文件并不是实时更新,默认情况下时一星期更新一次,因此,我们在用whereis和locate 查找文件时,有时会找到已经被删除的数据,或者刚刚建立文件,却无法查找到,原因就是因为数据库文件没有被更新。


whereis [ -sbmu ] [ -SBM dir ... -f ] name...

-b:只查找二进制文件;
-B<目录>:只在设置的目录下查找二进制文件;
-f:不显示文件名前的路径名称;
-m:只查找说明文件;
-M<目录>:只在设置的目录下查找说明文件;
-s:只查找原始代码文件;
-S<目录>只在设置的目录下查找原始代码文件;
-u:查找不包含指定类型的文件。

[root@cent6 ~]# whereis tomcat
tomcat: /usr/local/tomcat


locate

https://www.cnblogs.com/xqzt/p/5426666.html

1、命令简介

        locate(locate) 命令用来查找文件或目录。 locate命令要比find -name快得多,原因在于它不搜索具体目录,而是搜索一个数据库/var/lib/mlocate/mlocate.db 。这个数据库中含有本地所有文件信息。Linux系统自动创建这个数据库,并且每天自动更新一次,因此,我们在用whereis和locate 查找文件时,有时会找到已经被删除的数据,或者刚刚建立文件,却无法查找到,原因就是因为数据库文件没有被更新。为了避免这种情况,可以在使用locate之前,先使用updatedb命令,手动更新数据库。整个locate工作其实是由四部分组成的:

  1. /usr/bin/updatedb   主要用来更新数据库,通过crontab自动完成的

  2. /usr/bin/locate         查询文件位置

  3. /etc/updatedb.conf   updatedb的配置文件

  4. /var/lib/mlocate/mlocate.db  存放文件信息的文件

2、用法

locate [OPTION]... [PATTERN]...

3、选项

复制代码
  -b, --basename         match only the base name of path names
  -c, --count            只输出找到的数量
  -d, --database DBPATH  使用DBPATH指定的数据库,而不是默认数据库 /var/lib/mlocate/mlocate.db
  -e, --existing         only print entries for currently existing files
  -L, --follow           follow trailing symbolic links when checking file existence (default)
  -h, --help             显示帮助
  -i, --ignore-case      忽略大小写
  -l, --limit, -n LIMIT  limit output (or counting) to LIMIT entries
  -m, --mmap             ignored, for backward compatibility
  -P, --nofollow, -H     don't follow trailing symbolic links when checking file existence
  -0, --null             separate entries with NUL on output
  -S, --statistics       don't search for entries, print statistics about eachused database
  -q, --quiet            安静模式,不会显示任何错误讯息
  -r, --regexp REGEXP    使用基本正则表达式
      --regex            使用扩展正则表达式
  -s, --stdio            ignored, for backward compatibility
  -V, --version          显示版本信息
  -w, --wholename        match whole path name (default)
复制代码

4、示例

示例1: 搜索etc目录下所有以my开头的文件
[root@cent6 lib]# locate /etc/my
/etc/my.cnf
示例2:新增的文件无法locate,使用updatedb
[root@cent6 ~]# touch new.txt
[root@cent6 ~]# locate new.txt
[root@cent6 ~]# updatedb
[root@cent6 ~]# locate new.txt
/root/new.txt
示例3:updatedb的配置文件/etc/updatedb.conf
复制代码
[root@cent6 ~]# cat /etc/updatedb.conf 
PRUNE_BIND_MOUNTS = "yes"
PRUNEFS = "9p afs anon_inodefs auto autofs bdev binfmt_misc cgroup cifs coda configfs cpuset debugfs devpts ecryptfs exofs fuse fusectl gfs gfs2 hugetlbfs inotifyfs iso9660 jffs2 lustre mqueue ncpfs nfs nfs4 nfsd pipefs proc ramfs rootfs rpc_pipefs securityfs selinuxfs sfs sockfs sysfs tmpfs ubifs udf usbfs"
PRUNENAMES = ".git .hg .svn"
PRUNEPATHS = "/afs /media /net /sfs /tmp /udev /var/cache/ccache /var/spool/cups /var/spool/squid /var/tmp"
复制代码

第一行PRUNE_BIND_MOUNTS="yes"的意思是:是否进行限制搜索。

第二行是排除检索的文件系统类型,即列出的文件系统类型不进行检索。

第二行表示对哪些后缀的文件排除检索,也就是列在这里面的后缀的文件跳过不进行检索。不同后缀之间用空格隔开。

第四行是排除检索的路径,即列出的路径下的文件和子文件夹均跳过不进行检索。updatedb之后使用locate仍然找不到想要文件

可以检查挂载的目录是否被忽略了


find

、命令简介

        find(find) 命令用来在指定目录下查找文件。任何位于参数之前的字符串都将被视为欲查找的目录名。如果使用该命令时,不设置任何参数,则find命令将在当前目录下查找子目录与文件。并且将查找到的子目录和文件全部进行显示。

 

2、用法

image

3、选项

'-H'表示只跟随命令行中指定的符号连接,
'-L'表示跟随所有的符号连接,
'-P'是默认的选项,表示不跟随符号连接。
-D debugoptions. 打印诊断信息
-Olevel  Enables  query  optimisation.   允许查询优化

EXPRESSIONS:表达式可能由下列成份组成:操作符、选项、测试表达式以及动作:

1、操作符为and、or、!的组合。

复制代码
! expr True if expr is false.        
-not expr   Same as ! expr, but not POSIX compliant.
expr1 expr2   implied "and"; expr2 is not evaluated if expr1 is false.
expr1 -a expr2  Same as expr1 expr2.
expr1 -and expr2 Same as expr1 expr2, but not POSIX compliant.
expr1 -o expr2  Or; expr2 is not evaluated if expr1 is true.
expr1 -or expr2  Same as expr1 -o expr2, but not POSIX compliant.
expr1 , expr2   List; both expr1 and expr2 are always evaluated.  The  value  of expr1 is discarded; the value of the list is the value of expr2.The comma operator can be useful for searching for several  different  types  of thing, but traversing the filesystem hierarchy only once.  The -fprintf action can be used to list the  various matched items into several different output files.
复制代码

2、设置项针对这次查找任务,而不是仅仅针对某一个文件,设置项总是返回true;

位置选项 (总是真): -daystart -follow -regextype
普通选项 (总是真,在其它表达式前指定):
-depth --help -maxdepth LEVELS -mindepth LEVELS -mount -noleaf
--version -xdev -ignore_readdir_race -noignore_readdir_race

3、测试项(test)则不同,它针对具体的一个文件进行匹配测试,如-name,-user等,返回true或者false;

测试项

-name  -type  -ok  -newer -perm  -atime, -ctime, -depth, -group,  -links,  -mtime,-nogroup,  -nouser,  -print,  -prune,  -size,  -user  and  -xdev -atime,  -ctime,  -depth,  -group,  -links,  -mtime,-nogroup,  -nouser,  -perm,  -print,  -prune, -size,-user and –xdev

测试项说明

复制代码
-name   filename             #查找名为filename的文件
-perm                        #按执行权限来查找
-user    username             #按文件属主来查找
-group groupname            #按组来查找
-mtime   -n +n                #按文件更改时间来查找文件,-n指n天以内,+n指n天以前
-atime    -n +n               #按文件访问时间来查GIN: 0px">
-ctime    -n +n              #按文件创建时间来查找文件,-n指n天以内,+n指n天以前
-nogroup                     #查无有效属组的文件,即文件的属组在/etc/groups中不存在
-nouser                     #查无有效属主的文件,即文件的属主在/etc/passwd中不存
-newer   f1 !f2              找文件,-n指n天以内,+n指n天以前 
-ctime    -n +n               #按文件创建时间来查找文件,-n指n天以内,+n指n天以前 
-nogroup                     #查无有效属组的文件,即文件的属组在/etc/groups中不存在
-nouser                      #查无有效属主的文件,即文件的属主在/etc/passwd中不存
-newer   f1 !f2               #查更改时间比f1新但比f2旧的文件
-type    b/d/c/p/l/f         #查是块设备、目录、字符设备、管道、符号链接、普通文件
-size      n[c]               #查长度为n块[或n字节]的文件
-depth                       #使查找在进入子目录前先行查找完本目录
-fstype                     #查更改时间比f1新但比f2旧的文件
-type    b/d/c/p/l/f         #查是块设备、目录、字符设备、管道、符号链接、普通文件
-size      n[c]               #查长度为n块[或n字节]的文件
-depth                       #使查找在进入子目录前先行查找完本目录
-fstype                      #查位于某一类型文件系统中的文件,这些文件系统类型通常可 在/etc/fstab中找到
-mount                       #查文件时不跨越文件系统mount点
-follow                      #如果遇到符号链接文件,就跟踪链接所指的文件
-cpio                %;      #查位于某一类型文件系统中的文件,这些文件系统类型通常可 在/etc/fstab中找到
-mount                       #查文件时不跨越文件系统mount点
-follow                      #如果遇到符号链接文件,就跟踪链接所指的文件
-cpio                        #对匹配的文件使用cpio命令,将他们备份到磁带设备中
-prune                       #忽略某个目录
复制代码

4、动作项(action)则是对某一个文件进行某种动作(最常见的如-print)

actions: -delete -print0 -printf FORMAT -fprintf FILE FORMAT -print 
      -fprint0 FILE -fprint FILE -ls -fls FILE -prune – -quit
      -exec COMMAND ; -exec COMMAND {} + -ok COMMAND ;
      -execdir COMMAND ; -execdir COMMAND {} + -okdir COMMAND ;
  1. -print: find命令将匹配的文件输出到标准输出。
  2. -ls:使用ls -dils 格式将匹配的文件输出到标准输出。
  3. -exec: find命令对匹配的文件执行该参数所给出的shell命令。
  4. -ok: 和-exec的作用相同,只不过以一种更为安全的模式来执行该参数所给出的shell命令,在执行每一个命令之前,都会给出提示,让用户来确定是否执行。

4、示例

实例1:-H –L –P指定对符号连接的处理方式

复制代码
[root@oracledb dir1]# ln -s /bin  rootbin
[root@oracledb dir1]# find -P . -name pwd
[root@oracledb dir1]# find -L . -name pwd
./rootbin/pwd
[root@oracledb dir1]# find -H rootbin  . -name pwd
rootbin/pwd
复制代码

示例2:根据文件名或者正则表达式进行匹配

列出当前目录及子目录下所有文件和文件夹

复制代码
[root@zabbix alertscripts]# find
.
./sendim.py
./sendmail.py
./sendsms.sh
./sendwechat.py
复制代码

在当前目录下查找以.py结尾的文件名

[root@zabbix alertscripts]# find . -name "*.py"
./sendim.py
./sendmail.py
./sendwechat.py

在当前目录下查找以.py结尾的文件名 ,但忽略大小写

[root@zabbix alertscripts]# find . -iname "*.PY"
./sendim.py
./sendmail.py
./sendwechat.py

当前目录及子目录下查找所有以.py和.sh结尾的文件

[root@zabbix alertscripts]#  find . -name "*.py" -o -name "*.sh"
./sendim.py
./sendmail.py
./sendsms.sh
./sendwechat.py

找出当前目录下不是以.sh结尾的文件

[root@zabbix alertscripts]# find . ! -name "*.sh"
.
./sendim.py
./sendmail.py
./sendwechat.py

示例3:匹配文件路径或者文件

find /usr/ -path "*local*"

示例4:基于正则表达式匹配文件路径

[root@zabbix alertscripts]#  find . -regex ".*(.sh|.py)$"
./sendim.py
./sendmail.py
./sendsms.sh
./sendwechat.py

同上,但忽略大小写

[root@zabbix alertscripts]#  find . -iregex ".*(.SH|.PY)$"
./sendim.py
./sendmail.py
./sendsms.sh
./sendwechat.py

示例5:搜索但跳出指定的目录

查找当前目录或者子目录下所有文件,但是跳过子目录alertscripts

复制代码
[root@zabbix zabbix]# find .  -path "./alertscripts" -prune  -o -name "*.*"
.
./web/maintenance.inc.php
./web/zabbix.conf.php
./zabbix_java_gateway.conf
./zabbix_agentd.d
./zabbix_agentd.d/userparameter_mysql.conf
./zabbix_agentd.conf
./alertscripts
./zabbix_java_gateway_logback.xml
./zabbix_server.conf.rpmnew_bak
./zabbix_server.conf_bak
./zabbix_server.conf
复制代码

示例6:根据文件类型进行搜索

(f 普通文件 l 符号连接 d 目录 c 字符设备 b 块设备 s 套接字 p Fifo )

[root@zabbix alertscripts]# find . -type d
.

示例7:基于目录深度搜索 向下最大深度限制为1

复制代码
[root@zabbix zabbix]# find . -maxdepth 1
.
./web
./zabbix_java_gateway.conf
./zabbix_agentd.d
./zabbix_agentd.conf
./alertscripts
./zabbix_java_gateway_logback.xml
./zabbix_server.conf.rpmnew_bak
./zabbix_server.conf_bak
./zabbix_server.conf
复制代码

搜索出深度距离当前目录至少2个子目录的所有文件

find . -mindepth 2 

示例8:根据文件时间戳进行搜索 

find . -type f 时间戳

UNIX/Linux文件系统每个文件都有三种时间戳:

访问时间(-atime/天,-amin/分钟):用户最近一次访问时间。

修改时间(-mtime/天,-mmin/分钟):文件最后一次修改时间。

变化时间(-ctime/天,-cmin/分钟):文件数据元(例如权限等)最后一次修改时间。

搜索最近七天内被访问过的所有文件

find . -type f -atime –7

搜索恰好在七天前被访问过的所有文件

find . -type f -atime 7

搜索超过七天内被访问过的所有文件

find . -type f -atime +7

搜索访问时间超过10分钟的所有文件

find . -type f -amin +10

找出比file.log修改时间更长的所有文件

find . -type f -newer file.log

示例9:根据文件大小进行匹配 

搜索大于10KB的文件

find . -type f -size +10k

搜索小于10KB的文件

find . -type f -size –10k

搜索等于10KB的文件

find . -type f -size 10k

示例10:根据文件权限/所有权进行匹配

当前目录下搜索出权限为777的文件

find . -type f -perm 777

找出当前目录下权限不是644的php文件

find . -type f -name "*.php" ! -perm 644

示例11:根据用户/用户组进行匹配

找出当前目录用户zabbix拥有的所有文件

find . -type f -user zabbix

找出当前目录用户组zabbix拥有的所有文件

find . -type f -group zabbix

示例12:–delete 删除匹配文件

删除当前目录下所有.txt文件

find . -type f -name "*.txt" –delete

示例13:列出匹配文件 :

find . -type f -name "*.txt" –ls

5、借助-exec选项与其他命令结合使用

find是我们很常用的一个Linux命令,但是我们一般查找出来的并不仅仅是看看而已,还会有进一步的操作,这个时候exec的作用就显现出来了。exec选项后面跟随着所要执行的命令或脚本,然后是一对儿{ },一个空格和一个,最后是一个分号。

-exec  参数后面跟的是command命令,它的终止是以;为结束标志的,所以这句命令后面的分号是不可缺少的,考虑到各个系统中分号会有不同的意义,所以前面加反斜杠。{}   花括号代表前面find查找出来的文件名。

找出当前目录下所有root的文件,并把所有权更改为用户oracle

find .-type f -user root -exec chown oracle {} ;

查找当前目录下所有.txt文件并把他们拼接起来写入到all.txt文件中

find . -type f -name "*.txt" -exec cat {} ;> all.txt

将30天前的.log文件移动到old目录中

find . -type f -mtime +30 -name "*.log" -exec cp {} old ;

找出当前目录下所有.txt文件并以“File:文件名”的形式打印出来

find . -type f -name "*.txt" -exec printf "File: %s
" {} ;

因为单行命令中-exec参数中无法使用多个命令,以下方法可以实现在-exec之后接受多条命令

-exec ./text.sh {} ;

6、find的ok操作

-ok和-exec行为一样,不过它会给出提示,让用户决定是否执行相应的操作。

找出自己家目录下所有的.txt文件并删除

[root@oracledb ~]# find $HOME/. -name "*.txt" -ok rm -f {} ;
< rm ... /root/./log1.txt > ? y
< rm ... /root/./study/dir2/test.txt > ? y
< rm ... /root/./log.txt > ? y
[root@oracledb ~]#

7、find与xargs

在使用find命令的-exec选项处理匹配到的文件时, find命令将所有匹配到的文件一起传递给exec执行。但有些系统对能够传递给exec的命令长度有限制,这样在find命令运行几分钟之后,就会出现溢出错误。错误信息通常是“参数列太长”或“参数列溢出”。这就是xargs命令的用处所在,特别是与find命令一起使用。

find命令把匹配到的文件传递给xargs命令,而xargs命令每次只获取一部分文件而不是全部,不像-exec选项那样。这样它可以先处理最先获取的一部分文件,然后是下一批,并如此继续下去

在有些系统中,使用-exec选项会为处理每一个匹配到的文件而发起一个相应的进程,并非将匹配到的文件全部作为参数一次执行;这样在有些情况下就会出现进程过多,系统性能下降的问题,因而效率不高;而使用xargs命令则只有一个进程。另外,在使用xargs命令时,究竟是一次获取所有的参数,还是分批取得参数,以及每一次获取参数的数目都会根据该命令的选项及系统内核中相应的可调参数来确定。

来看看xargs命令是如何同find命令一起使用的,并给出一些例子。

查找系统中的每一个普通文件,然后使用xargs命令来测试它们分别属于哪类文件

find . -type f -print | xargs file

在整个系统中查找内存信息转储文件(core dump) ,然后把结果保存到/tmp/core.log 文件中:

find / -name "core" -print | xargs echo "" >/tmp/core.log

用grep命令在所有的普通文件中搜索hostname这个词

find . -type f -print | xargs grep "hostname"

删除3天以前的所有东西

find . -ctime +3 -exec rm -rf {} ;
find ./ -mtime +3 -print|xargs rm -f –r

删除文件大小为零的文件

find ./ -size 0 | xargs rm -f &

find命令配合使用exec和xargs可以使用户对所匹配到的文件执行几乎所有的命令。


grep

http://www.cnblogs.com/peida/archive/2012/12/17/2821195.html

grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹 配的行打印出来。grep全称是Global Regular Expression Print,表示全局正则表达式版本,它的使用权限是所有用户。

grep的工作方式是这样的,它在一个或多个文件中搜索字符串模板。如果模板包括空格,则必须被引用,模板后的所有字符串被看作文件名。搜索的结果被送到标准输出,不影响原文件内容。

grep可用于shell脚本,因为grep通过返回一个状态值来说明搜索的状态,如果模板搜索成功,则返回0,如果搜索不成功,则返回1,如果搜索的文件不存在,则返回2。我们利用这些返回值就可进行一些自动化的文本处理工作。

1.命令格式:

grep [option] pattern file

2.命令功能:

用于过滤/搜索的特定字符。可使用正则表达式能多种命令配合使用,使用上十分灵活。

3.命令参数:

-a   --text   #不要忽略二进制的数据。   

-A<显示行数>   --after-context=<显示行数>   #除了显示符合范本样式的那一列之外,并显示该行之后的内容。   

-b   --byte-offset   #在显示符合样式的那一行之前,标示出该行第一个字符的编号。   

-B<显示行数>   --before-context=<显示行数>   #除了显示符合样式的那一行之外,并显示该行之前的内容。   

-c    --count   #计算符合样式的列数。   

-C<显示行数>    --context=<显示行数>或-<显示行数>   #除了显示符合样式的那一行之外,并显示该行之前后的内容。   

-d <动作>      --directories=<动作>   #当指定要查找的是目录而非文件时,必须使用这项参数,否则grep指令将回报信息并停止动作。   

-e<范本样式>  --regexp=<范本样式>   #指定字符串做为查找文件内容的样式。   

-E      --extended-regexp   #将样式为延伸的普通表示法来使用。   

-f<规则文件>  --file=<规则文件>   #指定规则文件,其内容含有一个或多个规则样式,让grep查找符合规则条件的文件内容,格式为每行一个规则样式。   

-F   --fixed-regexp   #将样式视为固定字符串的列表。   

-G   --basic-regexp   #将样式视为普通的表示法来使用。   

-h   --no-filename   #在显示符合样式的那一行之前,不标示该行所属的文件名称。   

-H   --with-filename   #在显示符合样式的那一行之前,表示该行所属的文件名称。   

-i    --ignore-case   #忽略字符大小写的差别。   

-l    --file-with-matches   #列出文件内容符合指定的样式的文件名称。   

-L   --files-without-match   #列出文件内容不符合指定的样式的文件名称。   

-n   --line-number   #在显示符合样式的那一行之前,标示出该行的列数编号。   

-q   --quiet或--silent   #不显示任何信息。   

-r   --recursive   #此参数的效果和指定“-d recurse”参数相同。   

-s   --no-messages   #不显示错误信息。   

-v   --revert-match   #显示不包含匹配文本的所有行。   

-V   --version   #显示版本信息。   

-w   --word-regexp   #只显示全字符合的列。   

-x    --line-regexp   #只显示全列符合的列。   

-y   #此参数的效果和指定“-i”参数相同。

  

4.规则表达式:

grep的规则表达式:

^  #锚定行的开始 如:'^grep'匹配所有以grep开头的行。    

$  #锚定行的结束 如:'grep$'匹配所有以grep结尾的行。    

.  #匹配一个非换行符的字符 如:'gr.p'匹配gr后接一个任意字符,然后是p。    

*  #匹配零个或多个先前字符 如:'*grep'匹配所有一个或多个空格后紧跟grep的行。    

.*   #一起用代表任意字符。   

[]   #匹配一个指定范围内的字符,如'[Gg]rep'匹配Grep和grep。    

[^]  #匹配一个不在指定范围内的字符,如:'[^A-FH-Z]rep'匹配不包含A-R和T-Z的一个字母开头,紧跟rep的行。    

(..)  #标记匹配字符,如'(love)',love被标记为1。    

<      #锚定单词的开始,如:'<grep'匹配包含以grep开头的单词的行。    

>      #锚定单词的结束,如'grep>'匹配包含以grep结尾的单词的行。    

x{m}  #重复字符x,m次,如:'0{5}'匹配包含5个o的行。    

x{m,}  #重复字符x,至少m次,如:'o{5,}'匹配至少有5个o的行。    

x{m,n}  #重复字符x,至少m次,不多于n次,如:'o{5,10}'匹配5--10个o的行。   

w    #匹配文字和数字字符,也就是[A-Za-z0-9],如:'Gw*p'匹配以G后跟零个或多个文字或数字字符,然后是p。   

W    #w的反置形式,匹配一个或多个非单词字符,如点号句号等。   

    #单词锁定符,如: 'grep'只匹配grep。  

POSIX字符:

为了在不同国家的字符编码中保持一至,POSIX(The Portable Operating System Interface)增加了特殊的字符类,如[:alnum:]是[A-Za-z0-9]的另一个写法。要把它们放到[]号内才能成为正则表达式,如[A- Za-z0-9]或[[:alnum:]]。在linux下的grep除fgrep外,都支持POSIX的字符类。

[:alnum:]    #文字数字字符   

[:alpha:]    #文字字符   

[:digit:]    #数字字符   

[:graph:]    #非空字符(非空格、控制字符)   

[:lower:]    #小写字符   

[:cntrl:]    #控制字符   

[:print:]    #非空字符(包括空格)   

[:punct:]    #标点符号   

[:space:]    #所有空白字符(新行,空格,制表符)   

[:upper:]    #大写字符   

[:xdigit:]   #十六进制数字(0-9,a-f,A-F)  

5.使用实例:

实例1:查找指定进程

命令:

ps -ef|grep svn

输出:

[root@localhost ~]# ps -ef|grep svn

root 4943   1      0  Dec05 ?   00:00:00 svnserve -d -r /opt/svndata/grape/

root 16867 16838  0 19:53 pts/0    00:00:00 grep svn

[root@localhost ~]#

说明:

第一条记录是查找出的进程;第二条结果是grep进程本身,并非真正要找的进程。

实例2:查找指定进程个数

命令:

ps -ef|grep svn -c

ps -ef|grep -c svn

输出:

[root@localhost ~]# ps -ef|grep svn -c

2

[root@localhost ~]# ps -ef|grep -c svn 

2

[root@localhost ~]#

说明:

实例3:从文件中读取关键词进行搜索

命令:

cat test.txt | grep -f test2.txt

输出:

[root@localhost test]# cat test.txt 

hnlinux

peida.cnblogs.com

ubuntu

ubuntu linux

redhat

Redhat

linuxmint

[root@localhost test]# cat test2.txt 

linux

Redhat

[root@localhost test]# cat test.txt | grep -f test2.txt

hnlinux

ubuntu linux

Redhat

linuxmint

[root@localhost test]#

说明:

输出test.txt文件中含有从test2.txt文件中读取出的关键词的内容行

实例3:从文件中读取关键词进行搜索 且显示行号

命令:

cat test.txt | grep -nf test2.txt

输出:

[root@localhost test]# cat test.txt 

hnlinux

peida.cnblogs.com

ubuntu

ubuntu linux

redhat

Redhat

linuxmint

[root@localhost test]# cat test2.txt 

linux

Redhat

[root@localhost test]# cat test.txt | grep -nf test2.txt

1:hnlinux

4:ubuntu linux

6:Redhat

7:linuxmint

[root@localhost test]#

说明:

输出test.txt文件中含有从test2.txt文件中读取出的关键词的内容行,并显示每一行的行号

实例5:从文件中查找关键词

命令:

grep 'linux' test.txt

输出:

[root@localhost test]# grep 'linux' test.txt 

hnlinux

ubuntu linux

linuxmint

[root@localhost test]# grep -n 'linux' test.txt 

1:hnlinux

4:ubuntu linux

7:linuxmint

[root@localhost test]#

说明:

实例6:从多个文件中查找关键词

命令:

grep 'linux' test.txt test2.txt

输出:

[root@localhost test]# grep -n 'linux' test.txt test2.txt 

test.txt:1:hnlinux

test.txt:4:ubuntu linux

test.txt:7:linuxmint

test2.txt:1:linux

[root@localhost test]# grep 'linux' test.txt test2.txt 

test.txt:hnlinux

test.txt:ubuntu linux

test.txt:linuxmint

test2.txt:linux

[root@localhost test]#

说明:

多文件时,输出查询到的信息内容行时,会把文件的命名在行最前面输出并且加上":"作为标示符

实例7:grep不显示本身进程

命令:

ps aux|grep [s]sh

ps aux | grep ssh | grep -v "grep"

输出:

[root@localhost test]# ps aux|grep ssh

root   2720  0.0  0.0  62656  1212 ?      Ss   Nov02   0:00 /usr/sbin/sshd

root  16834  0.0  0.0  88088  3288 ?      Ss   19:53   0:00 sshd: root@pts/0 

root  16901  0.0  0.0  61180   764 pts/0  S+   20:31   0:00 grep ssh

[root@localhost test]# ps aux|grep [s]sh]

[root@localhost test]# ps aux|grep [s]sh

root   2720  0.0  0.0  62656  1212 ?      Ss   Nov02   0:00 /usr/sbin/sshd

root  16834  0.0  0.0  88088  3288 ?      Ss   19:53   0:00 sshd: root@pts/0 

[root@localhost test]# ps aux | grep ssh | grep -v "grep"

root   2720  0.0  0.0  62656  1212 ?      Ss   Nov02   0:00 /usr/sbin/sshd

root  16834  0.0  0.0  88088  3288 ?      Ss   19:53   0:00 sshd: root@pts/0

说明:

实例8:找出已u开头的行内容

命令:

cat test.txt |grep ^u

输出:

[root@localhost test]# cat test.txt |grep ^u

ubuntu

ubuntu linux

[root@localhost test]#

说明:

实例9:输出非u开头的行内容

命令:

cat test.txt |grep ^[^u]

输出:

[root@localhost test]# cat test.txt |grep ^[^u]

hnlinux

peida.cnblogs.com

redhat

Redhat

linuxmint

[root@localhost test]#

说明:

实例10:输出以hat结尾的行内容

命令:

cat test.txt |grep hat$

输出:

[root@localhost test]# cat test.txt |grep hat$

redhat

Redhat

[root@localhost test]#

说明:

实例11:

命令:

输出:

[root@localhost test]# ifconfig eth0|grep "[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}"

          inet addr:192.168.120.204  Bcast:192.168.120.255  Mask:255.255.255.0

[root@localhost test]# ifconfig eth0|grep -E "([0-9]{1,3}.){3}[0-9]"

          inet addr:192.168.120.204  Bcast:192.168.120.255  Mask:255.255.255.0

[root@localhost test]#

说明:

实例12:显示包含ed或者at字符的内容行

命令:

cat test.txt |grep -E "ed|at"

输出:

[root@localhost test]# cat test.txt |grep -E "peida|com"

peida.cnblogs.com

[root@localhost test]# cat test.txt |grep -E "ed|at"

redhat

Redhat

[root@localhost test]#

说明:

实例13:显示当前目录下面以.txt 结尾的文件中的所有包含每个字符串至少有7个连续小写字符的字符串的行

命令:

grep '[a-z]{7}' *.txt

输出:

[root@localhost test]# grep '[a-z]{7}' *.txt

test.txt:hnlinux

test.txt:peida.cnblogs.com

test.txt:linuxmint

[root@localhost test]#


原文地址:https://www.cnblogs.com/bluestorm/p/10283869.html