find--在某个指定的路径下查找需要的文件或目录

格式:

find [查找位置] [查找标准] [处理动作]

查找位置:默认为当前目录,可以指定多个目录,多个之间用空格

查找标准:默认为查找指定目录下的所有文件

处理动作:显示到标准输出,默认为print

查找标准

以文件名查找

-name "文件名称" : 根据文件名查找,支持glob(文件名通配),注意文件名需用双引号

[[email protected] ~]# find /etc/ -name "ifcfg-eth0"
/etc/sysconfig/network-scripts/ifcfg-eth0
[[email protected] ~]# find /etc/ -name "ifcfg*"
/etc/sysconfig/network-scripts/ifcfg-eth0
/etc/sysconfig/network-scripts/ifcfg-lo
/etc/sysconfig/network-scripts/ifcfg-eth0bak
[[email protected] ~]#

-iname "文件名称",根据文件名查找,不区分大小写

[[email protected] ~]# mkdir Justin juStin jusTin
[[email protected] ~]# find -iname "justin"
./juStin
./jusTin
./Justin
[[email protected] ~]#

-inum "文件inode",查找指定inode的文件

find ./* -inum 1049741 -delete

直接删除,不会询问你确认删除。

以文件的属性查找

-user "USERNAME" ----根据属主查找

-group "GROUP" --- 根据属组查找

-uid "UID" ---根据UID查找

-gid "GID" ---根据GID查找

-nouser--- 查找没有属主的文件

-nogroup--- 查找没有属组的文件

#在/dev目录下查找用户名为root组为disk的文件并用长格式显示
[[email protected] ~]# find /dev/ -user root -a -group disk -ls
 10630    0 crw-rw----   1 root     disk              9月 24 10:18 /dev/sg0
 10241    0 brw-rw----   1 root     disk              9月 24 10:18 /dev/ram3
  9915    0 brw-rw----   1 root     disk              9月 24  2013 /dev/ram1

多条件查找连接符

-a: 与

-o: 或

-not、!:非

[[email protected] home]# ls
111.txt  222.log  justin  justin1  justin2
[[email protected] home]# find /home/ -name "*.txt" -o -name "*.log"
/home/222.log
/home/111.txt
[[email protected] home]# find /home/ -name "*.txt" -a -name "*.log"


以文件类型查找

-type 文件类型

文件类型指的是普通文件(f)、目录(d)、块设备文件(b)、字符设备文件(c)、符号链接文件(l)、命令管道文件(p)、套接字文件(s)等

[[email protected] ~]# find /bin/ -type l
/bin/mail
/bin/rview
/bin/ex
/bin/gtar


以文件大小查找

-size”大小条件

一般使用“+”、“-”号设置超过或小于指定的大小作为查找条件。常用的容量单位包括k(注意是小写)、M、G。

[[email protected] ~]# find /root/ -size 2k        ;大小为2K的文件
/root/.gconf/apps/brasero/config/priority/%gconf.xml
/root/.gconf/desktop/gnome/accessibility/keyboard/%gconf.xml
/root/.gnote/89099c13-c354-48d6-89ce-0415b1db3c78.note
/root/.gnote/db520e7c-3433-4433-b35a-3ad9a14e16d4.note
/root/anaconda-ks.cfg
[[email protected] ~]# find /root/ -size +50k   ;大小超过50k的文件
/root/.cache/ibus/pinyin/user-1.3.db
/root/.cache/ibus/bus/registry.xml
/root/.pulse/8665cfcff9c76f94fac16e0000000022-device-volumes.tdb
/root/.gstreamer-0.10/registry.i386.bin
/root/.gconfd/saved_state
[[email protected] ~]#

-empty 空文件(目录)

[[email protected] ~]# find -empty /

-maxdepth 限制find命令在目录中按照递减方式查找文件的时候搜索文件超过某个级别或者搜索过多的目录

[[email protected] ~]# find . -maxdepth 2 -name fred

假如这个fred文件在./sub1/fred目录中,那么这个命令就会直接定位这个文件,查找很容易成功。假如,这个文件在./sub1/sub2/fred目录中,那么这个命令就无法查找到。因为前面已经给find命令在目录中最大的查询目录级别为2,只能查找2层目录下的文件。这样做的目的就是为了让find命令更加精确的定位文件,如果你已经知道了某个文件大概所在的文件目录级数,那么加入-maxdepth n 就很快的能在指定目录中查找成功。


-depth   #使查找在进入子目录前先行查找完本目录减方

-mount                       #查文件时不跨越文件系统mount点
-follow                      #如果遇到符号链接文件,就跟踪链接所指的文件


以文件修改时间查找

-atime [+|-]# : access time访问时间,默认为天,#表示#天的这个时间点,+#表示至少有#天没访问, -#表示#天之内有访问

-mtime [+|-]# : modify time修改时间,#表示#天的这个时间点没有被修改,+#表示至少有#天没有修改 , -#表示#天之内有修改

-ctime [+|-]# : change time改变时间,#表示#天的这个时间点没有被改变,+#表示至少有#天没有被改变 , -#表示#天之内有被改变

mtime和ctime的区别在于,只有修改了文件的内容,才会更新文件的mtime,而对文件更名,修改文件的属主等操作,只会更新ctime。举例说明: 对文件进行mv操作,mtime不变,ctime更新;编辑文件内容,mtime和ctime同时修改。


-amin [+|-]# :时间为分钟,#表示#分钟的这个时间点没有被访问,+#表示至少有#分钟没有被访问 , -#表示#分钟之内访问

-mmin [+|-]# :时间为分钟,#表示#分钟的这个时间点没有被修改,+#表示至少有#分钟没有被修改 , -#表示#分钟之内有被修改

-cmin [+|-]# :时间为分钟#表示#分钟的这个时间点没有被改变,+#表示至少有#分钟没有被改变 , -#表示#分钟之内有被改变

[[email protected] logs]# find /opt/queryweb/logs -mtime +7 -type f -name "*.log" -exec rm -f {} \;
[[email protected] logs]#


查看和修改文件时间

• modification time(mtime,修改时间):当该文件的“内容数据”更改时,就会更新这个时间。内容数据指的是文件的内容,而不是文件的属性。比如:vim后保存文件。ls -l列出的时间就是这个时间。

• status time(ctime,状态时间):当该文件的”状态(status)”改变时,就会更新这个时间,举例来说,更改了权限与属性,就会更新这个时间。

• access time(atime,存取时间):当“取用文件内容”时,就会更新这个读取时间。举例来说,使用cat、more去读取 ~/.bashrc,就会更新ati,,ls、stat命令都不会修改文件的访问时间


1、stat查看文件时间

[[email protected] justin]# stat companies.csv 
  File: ‘companies.csv’
  Size: 123011072     Blocks: 240256     IO Block: 4096   regular file
Device: 803h/2051d    Inode: 69567919    Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1002/  justin)   Gid: ( 1002/  justin)
Access: 2018-10-10 15:55:18.533453294 +0800    #Access访问时间
Modify: 2018-10-10 16:00:30.159437579 +0800    #Modify修改时间
Change: 2018-10-10 16:00:30.159437579 +0800    #Change状态改变时间
 Birth: -
[[email protected] justin]#


2、ls查看文件时间

[[email protected] justin]# ls -l --time=ctime companies.csv 
-rw-r--r-- 1 justin justin 123011072 Oct 10 16:00 companies.csv
[[email protected] justin]# ls -l --time=atime companies.csv 
-rw-r--r-- 1 justin justin 123011072 Oct 10 15:55 companies.csv
[[email protected] justin]#

ls参数里没有--mtime这个参数,因为我们默认通过ls -l查看到的时间就是mtime


3、修改文件时间

touch来修改文件时间

-a : 仅修改access time。
-c : 仅修改时间,而不建立文件。
-d : 后面可以接日期,也可以使用 --date="日期或时间"
-m : 仅修改mtime。
-t : 后面可以接时间,格式为 [YYMMDDhhmm]

[[email protected] justin]# touch -d "2018-11-13 12:42:13" companies.csv #同时修改文件的修改时间和访问时间
[[email protected] justin]# stat companies.csv 
  File: ‘companies.csv’
  Size: 123011072     Blocks: 240256     IO Block: 4096   regular file
Device: 803h/2051d    Inode: 69567919    Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1002/  justin)   Gid: ( 1002/  justin)
Access: 2018-11-13 12:42:13.000000000 +0800
Modify: 2018-11-13 12:42:13.000000000 +0800
Change: 2018-11-13 12:43:00.232438932 +0800
 Birth: -
[[email protected] justin]# touch -m -d "2018-11-12 12:42:13" companies.csv    #只修改文件的修改时间
[[email protected] justin]# stat companies.csv 
  File: ‘companies.csv’
  Size: 123011072     Blocks: 240256     IO Block: 4096   regular file
Device: 803h/2051d    Inode: 69567919    Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1002/  justin)   Gid: ( 1002/  justin)
Access: 2018-11-13 12:42:13.000000000 +0800
Modify: 2018-11-12 12:42:13.000000000 +0800
Change: 2018-11-13 12:44:04.007434571 +0800
 Birth: -
[[email protected] justin]#


touch -acmr companies.csv SHYH_file.txt     把后一个文件的时间修改成和前一个相同

[[email protected] justin]# stat companies.csv SHYH_file.txt 
  File: ‘companies.csv’
  Size: 123011072     Blocks: 240256     IO Block: 4096   regular file
Device: 803h/2051d    Inode: 69567919    Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1002/  justin)   Gid: ( 1002/  justin)
Access: 2018-11-13 12:42:13.000000000 +0800
Modify: 2018-11-12 12:42:13.000000000 +0800
Change: 2018-11-13 12:44:04.007434571 +0800
 Birth: -
  File: ‘SHYH_file.txt’
  Size: 187           Blocks: 8          IO Block: 4096   regular file
Device: 803h/2051d    Inode: 67449450    Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1002/  justin)   Gid: ( 1002/  justin)
Access: 2018-06-09 20:27:24.505046754 +0800
Modify: 2018-06-09 20:27:09.664047769 +0800
Change: 2018-06-09 20:27:09.664047769 +0800
 Birth: -
[[email protected] justin]# touch -acmr companies.csv SHYH_file.txt 
[[email protected] justin]# stat companies.csv SHYH_file.txt 
  File: ‘companies.csv’
  Size: 123011072     Blocks: 240256     IO Block: 4096   regular file
Device: 803h/2051d    Inode: 69567919    Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1002/  justin)   Gid: ( 1002/  justin)
Access: 2018-11-13 12:42:13.000000000 +0800
Modify: 2018-11-12 12:42:13.000000000 +0800
Change: 2018-11-13 12:44:04.007434571 +0800
 Birth: -
  File: ‘SHYH_file.txt’
  Size: 187           Blocks: 8          IO Block: 4096   regular file
Device: 803h/2051d    Inode: 67449450    Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1002/  justin)   Gid: ( 1002/  justin)
Access: 2018-11-13 12:42:13.000000000 +0800
Modify: 2018-11-12 12:42:13.000000000 +0800
Change: 2018-11-13 12:45:46.876431277 +0800
 Birth: -
[[email protected] justin]#

touch -d "2 days ago" companies.csv    支持像date命令一样参数修改文件时间

[[email protected] justin]# touch -d "2 days ago" companies.csv 
[[email protected] justin]# stat companies.csv 
  File: ‘companies.csv’
  Size: 123011072     Blocks: 240256     IO Block: 4096   regular file
Device: 803h/2051d    Inode: 69567919    Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1002/  justin)   Gid: ( 1002/  justin)
Access: 2018-11-11 12:47:19.500920869 +0800
Modify: 2018-11-11 12:47:19.500920869 +0800
Change: 2018-11-13 12:47:19.500424943 +0800
 Birth: -
[[email protected] justin]#




-newer   f1 !f2  查找更改时间比f1新但比f2旧的文件


以文件权限查找

-perm [+|-] MODE

 不带[+|-]表示精确权限匹配,

 +表示任何一类用户的任何一位权限匹配

 - 表示每类用户的每位权限都匹配

#精确匹配/etc下权限为755的文件。ugo都必须同时满足
[[email protected] ~]# find /etc/ -perm 755 -ls |more
622593   16 drwxr-xr-x  98 root     root        12288 Aug  6 14:39 /etc/
622778    8 drwxr-xr-x   2 root     root         4096 Jan 12  2007 /etc/jvm
622638    8 drwxr-xr-x   2 root     root         4096 Aug  2 16:44 /etc/xinetd.d
#755用二进制表示为111 101 101;+后跟权限匹配原则是满足所有权限位的一个位就可以
[[email protected] ~]# find /etc/ -perm +755 -ls |more
622593   16 drwxr-xr-x  98 root     root        12288 Aug  6 14:39 /etc/
622625    8 -rw-r--r--   1 root     root          135 Aug  6 13:31 /etc/printcap
622645   20 -rw-r--r--   1 root     root        14100 Sep  5  2006 /etc/mime.types
#-后跟权限匹配原则是ugo中所有位权限必须同时满足,属主必须为rwx,属组和other至少为rw
[[email protected] ~]# find /etc/ -perm -755 -ls |more
622593   16 drwxr-xr-x  98 root     root        12288 Aug  6 14:39 /etc/
622778    8 drwxr-xr-x   2 root     root         4096 Jan 12  2007 /etc/jvm
622638    8 drwxr-xr-x   2 root     root         4096 Aug  2 16:44 /etc/xinetd.d


处理动作

-print---find命令将匹配的文件输出到标准输出。
-exec---对匹配的文件执行该参数所给出的shell命令。相应命令的形式为'command' {} \;,注意{}和\;之间的空格,同时两个{}之间没有空格

-ok---和-exec的作用相同,只不过以一种更为安全的模式来执行该参数所给出的shell命令,在执行每一个命令之前,都会给出提示,让用户来确定是否执行

# 在当前目录下查找除目录以外的所有类型的文件 ,并打印出来
[[email protected] test]# find . ! -type d –print
#查找root目录下的以log结尾的文件,将其复制到test目录下。
[[email protected] test]# find /root –name “*log” –type f –exec cp {} /root/test/ \; 2>/dev/null
#通过-exec参数指定后面要执行的命令,{}表示将查找到的内容全部复制,\;表示命令的结束,2>/dev/null是指将执行查找过程中出现的错误信息重定向到黑洞文件中,也就是不显示那些错误信息。
#长格式显示test下的普通文件
[[email protected] test]# find . -type f -exec ls -l {  } \;
#***test下大小为0的文件
[[email protected] test]# find ./ -size 0 -exec rm {} \;
#在当前目录中查找所有文件名以.conf结尾、更改时间在5日以上的文件,并***它们,只不过在***之前先给出提示
[[email protected] test]# find . -name "*.conf"  -mtime +5 -ok rm {  } \;

/dev/null,外号叫"黑洞",它等价于一个只写文件,所有写入它的内容都会永远丢失.,而尝试从它那儿读取内容则什么也读不到。如果不想让消息以标准输出显示或写入文件,那么可以将消息重定向到它

/dev/zero主要的用处是用来创建一个指定长度用于初始化的空文件,就像临时交换文件。

xargs

 在使用find命令的-exec选项处理匹配到的文件时, find命令将所有匹配到的文件一起传递给exec执行。但有些系统对能够传递给exec的命令长度有限制,这样在find命令运行几分钟之后,就会出现溢出错误。错误信息通常是“参数列太长”或“参数列溢出”。find命令把匹配到的文件传递给xargs命令,而xargs命令每次只获取一部分文件而不是全部,这样它可以先处理最先获取的一部分文件,然后是下一批,并如此继续下去。

在有些系统中,使用-exec选项会为处理每一个匹配到的文件而发起一个相应的进程,并非将匹配到的文件全部作为参数一次执行;这样在有些情况下就会出现进程过多,系统性能下降的问题,因而效率不高;

而使用xargs命令则只有一个进程。另外,在使用xargs命令时,究竟是一次获取所有的参数,还是分批取得参数,以及每一次获取参数的数目都会根据该命令的选项及系统内核中相应的可调参数来确定。

#查找系统中的每一个普通文件,然后使用xargs命令来测试它们分别属于哪类文件[[email protected] ~]# find . -type f -print | xargs file
#在整个系统中查找内存信息转储文件(core dump) ,然后把结果保存到/tmp/core.log 文件中:
[[email protected] ~]# find / -name "core" -print | xargs echo "" >/tmp/core.log
#用grep命令在所有的普通文件中搜索hostname这个词
[[email protected] ~]# find . -type f -print | xargs grep "hostname"
#***3天以前的所有东西 (find . -ctime +3 -exec rm -rf {} \;)
[[email protected] ~]# find ./ -mtime +3 -print|xargs rm -f –r
#***文件大小为零的文件
[[email protected] ~]# find ./ -size 0 | xargs rm -f &

xargs

xargs命令是给其他命令传递参数的一个过滤器,也是组合多个命令的一个工具。它擅长将标准输入数据转换成命令行参数,默认从管道传来的值是放在最后的

xargs能够处理管道或者stdin并将其转换成特定命令的命令参数。xargs是构建单行命令的重要组件之一。

xargs也可以将单行或多行文本输入转换为其他格式,例如多行变单行,单行变多行。xargs的默认命令是echo,空格是默认定界符。这意味着通过管道传递给xargs的输入将会包含换行和空白,不过通过xargs的处理,换行和空白将被空格取代。xargs是构建单行命令的重要组件之一。如下:

[[email protected] ~]# cat xargs.txt 
1 2 3 4
a b c d
A B C D
a1 b2 c3 d4
A1 B2 C3 D4
[[email protected] ~]# cat xargs.txt | xargs
1 2 3 4 a b c d A B C D a1 b2 c3 d4 A1 B2 C3 D4
[[email protected] ~]#


    -0 当sdtin含有特殊字元时候,将其当成一般字符,像/'空格等

    -a file 从文件中读入作为sdtin


[[email protected] ~]# cat xargs.txt 
1 1 1 1
2 2
3 3 3 
4 4 4 4
5 5 5 5
[[email protected] ~]# xargs -a xargs.txt 
1 1 1 1 2 2 3 3 3 4 4 4 4 5 5 5 5
[[email protected] ~]#


    -e flag ,注意有的时候可能会是-E,flag必须是一个以空格分隔的标志,当xargs分析到含有flag这个标志的时候就停止。


[[email protected] ~]# cat xargs.txt |xargs -E "3" echo
1 1 1 1 2 2
[[email protected] ~]#


   -p 操作具有可交互性,每次执行comand都交互式提示用户选择


[[email protected] ~]# cat xargs.txt |xargs -p echo
echo 1 1 1 1 2 2 3 3 3 4 4 4 4 5 5 5 5 ?...y
1 1 1 1 2 2 3 3 3 4 4 4 4 5 5 5 5
[[email protected] ~]#


    -n xargs 的-n选项设置每次送给command命令的参数个数,参数以空白字符或<newline>换行符分割


[[email protected] ~]# cat xargs.txt |xargs -n 3  echo
1 1 1
1 2 2
3 3 3
4 4 4
4 5 5
5 5
[[email protected] ~]#


    -t 启用命令行输出模式:其先回显要运行的命令,然后执行命令,打印出命令结果,跟踪与调试xargs的利器,也是研究xargs运行原理的好办法;

    -i -i 选项告诉 xargs 可以使用{}代替传递过来的参数, 建议使用-I,其符合POSIX标准

   -I  格式: xargs -I rep-str comand rep-srt rep-str 为代替传递给xargs参数, 可以使 {} $ @ 等符号 ,其主要作用是当xargs command 后有多个参数时,调整参数位置。eg:find . -name "*.txt " |xargs -I {} cp {} /tmp ;默认管道会将find的结果传递给cp做为最后的参数,通过-I来指定起传递过来参数的未知

    -r 如果没有要处理的参数传递给xargsxargs 默认是带 空参数运行一次,如果你希望无参数时,停止 xargs,直接退出,使用 -r 选项即可,其可以防止xargs 后面命令带空参数运行报错。If the standard input does not contain any nonblanks, do not run the command, exit

    -s size 设置每次构造Command行的长度总大小,包括 command +init-param +传递参数,Size 参数必须是正整数

    -L  num Use at most max-lines nonblank input lines per command line.-s是含有空格的。

    -l  同-L

    -d delim 分隔符,默认的xargs分隔符是回车,argument的分隔符是空格,这里修改的是xargs的分隔符

[[email protected] ~]# echo "51ctox51ctox51cto"|xargs -dx
51cto 51cto 51cto

[[email protected] ~]# echo "51ctox51ctox51cto"|xargs -n1 -dx
51cto
51cto
51cto

[[email protected] ~]#


    -x exit的意思,主要是配合-s使用。

   -e 选项,只能传递一个参数。在单条命令中使用多个 -e 选项,得到多个pattern,以此实现OR操作。

    -P 修改最大的进程数,默认是1,为0时候为as many as it can ,这个例子我没有想到,应该平时都用不到的吧。

查找并显示文件

找到某个文件是我们的目的,我们更想知道查找到的文件的详细信息和属性,如果我们采取现查找文件,在使用LS命令来查看文件信息是相当繁琐的,现在我们也可以把这两个命令结合起来使用。

[[email protected] ~]# find / -name "httpd.conf" -ls 
12063 34 -rw-r--r-- 1 root root 33545 Dec 30 15:36 /etc/httpd/conf/httpd.conf
[[email protected] ~]#

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


查找排除某个目录

-prune #忽略某个目录

[[email protected] svn]# find 127_Client/ -type f -iname "[[:digit:]]*"
127_Client/product/938592691/127B/201801311237/CFG/127Bcfg.zip
127_Client/product/938592691/321/201801311237/CFG/321cfg.zip
127_Client/938592691/127B/201801311237/CFG/127Bcfg.zip
127_Client/938592691/321/201801311237/CFG/321cfg.zip
[[email protected] svn]# find 127_Client/ -path 127_Client/product -prune -o -type f -iname "[[:digit:]]*"
127_Client/product
127_Client/938592691/127B/201801311237/CFG/127Bcfg.zip
127_Client/938592691/321/201801311237/CFG/321cfg.zip
[[email protected] svn]# find 127_Client/ -path 127_Client/product -prune -o -type f -iname "[[:digit:]]*" -print
127_Client/938592691/127B/201801311237/CFG/127Bcfg.zip
127_Client/938592691/321/201801311237/CFG/321cfg.zip
[[email protected] svn]# find 127_Client/ \( -path 127_Client/product -o -path 127_Client/938592691/127B \) -prune -o -type f -iname "[[:digit:]]*" -print
127_Client/938592691/321/201801311237/CFG/321cfg.zip
[[email protected] svn]#

注意:排除的目录后面不能带"/",且要忽略的路径参数必须紧跟着搜索的路径之后,否则不起作用,-o参数必须加上,路径有空格必须用双引号括起来,排除多个目录需要使用圆括号括起来,且括号前后需要用转义符转义,让shell不对括号做特殊解释,而留给find命令解释其意义。


which---查找外部命令所对应的程序文件

[[email protected] ~]# which ls
alias ls='ls --color=auto'
    /bin/ls
[[email protected] ~]# which pwd
/bin/pwd
[[email protected] ~]# which cd
/usr/bin/which: no cd in (/usr/lib/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin)
[[email protected] ~]#

说明:which命令用于查找Linux外部命令所对应的程序文件,其搜索范围由环境变量PATH决定,在PATH变量指定的路径中,搜索某个系统命令的位置,并且返回第一个搜索结果。也就是说,使用which命令,就可以看到某个系统命令是否存在,以及执行的到底是哪一个位置的命令,执行命令后,首先显示出系统中所设置命令的别名,然后是命令的程序文件如“/bin/ls”。如果要查找的是一个内部命令,那将找不到任何对应的程序文件如cd。


whereis---查找文件或目录

-b---只找二进制文档

-m---只查找manual路径下的文件

-s---只查找source下的

-u---查找以上三个找不到的文件档案

[[email protected] ~]# whereis pwd
pwd: /bin/pwd /usr/share/man/man1/pwd.1.gz /usr/share/man/man1p/pwd.1p.gz
[[email protected] ~]# whereis etc
etc: /usr/local/etc
[[email protected] ~]# whereis -b pwd
pwd: /bin/pwd
[[email protected] ~]# whereis -m pwd
pwd: /usr/share/man/man1/pwd.1.gz /usr/share/man/man1p/pwd.1p.gz
[[email protected] ~]# whereis -s pwd
pwd:
[[email protected] ~]# whereis -u pwd
pwd: /bin/pwd /usr/share/man/man1/pwd.1.gz /usr/share/man/man1p/pwd.1p.gz
[[email protected] ~]#

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


locate---查找文件

#查找/etc下pass开头的文件
[[email protected] ~]# locate /etc/pass
/etc/passwd
/etc/passwd-
[[email protected] ~]#

说明:locate命令其实是“find -name”的另一种写法,但是要比后者快得多,原因在于它不搜索具体目录,而是搜索一个数据库(/var/lib/locatedb),这个数据库中含有本地所有文件信息。Linux系统自动创建这个数据库,并且每天自动更新一次,所以使用locate命令查不到最新变动过的文件。为了避免这种情况,可以在使用locate之前,先使用updatedb命令,手动更新数据库


grep---Global Regular Expression Print全局搜索正则表达式并将结果打印出来---在文件中查找并显示包含指定字符串的,目标是字符串

格式:grep [参数] 查找条件 目标文件

功能说明:grep指令是一种强大的文本搜索工具,是一个对行进行搜索的操作,它能使用正则表达式搜索文本,用于查找内容包含指定的范本样式的文件,如果发现某文件的内容符合所指定的范本样式,预设grep指令会把含有范本样式的那一行显示出来。若不指定任何文件名称,或是所给予的文件名为“-”,则grep指令会从标准输入设备读取数据。

Unix的grep家族包括grep、egrep和fgrep。 egrep表示扩展的grep,相比grep支持更多的元字符(元字符就是指那些在正则表达式中具有特殊意义的专用字符),"grep -E"相当于egrep。fgrep是fast grep,不支持元字符,但是搜索速度更快。grep搜索的结果被送到屏幕,不影响原文件内容。

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

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

-b 在显示符合范本样式的那一行之外,并显示该行之前的内容。

-c 计算符合范本样式的列数。

-C<显示列数>或-<显示列数>  除了显示符合范本样式的那一列之外,并显示该列之前后的内容。

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

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

-E 将范本样式为延伸的普通表示法来使用,意味着使用能使用扩展正则表达式。

-f<范本文件> 指定范本文件,其内容有一个或多个范本样式,让grep查找符合范本条件的文件内容,格式为每一列的范本样式。

-F 将范本样式视为固定字符串的列表。

-G 将范本样式视为普通的表示法来使用。

-h 在显示符合范本样式的那一列之前,不标示该列所属的文件名称。

-H 在显示符合范本样式的那一列之前,标示该列的文件名称。

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

-l 列出文件内容符合指定的范本样式的文件名称。

-L 列出文件内容不符合指定的范本样式的文件名称。

-n 在显示符合范本样式的那一列之前,标示出该列的编号。

-q 不显示任何信息。在静默模式中,grep 命令不会向标准输出打印任何输出,它仅是运行命令,然后根据命令执行成功与否返回退出状态,没有找到值则返回状态为1,找到则返回0.例:grep -q 'abc' test.txt

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

-s 不显示错误信息。

-v 反转查找。

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

-x 只显示全列符合的列。

-y 此参数效果跟“-i”相同。

-o 只输出文件中匹配到的部分。

grep搜索建议:

匹配字符串、调用变量时建议使用双引号:grep "ZOO_INFO" zookeeper.log   g r e p“$ M Y VA R” zookeeper.log

正则匹配应使用单引号:grep ‘\<ZOO_INFO\>’ zookeeper.log


--color=auto 匹配的关键用颜色显示出来

RHEL6基础之八查找、文件内容查看类命令

如果每次使用 grep 都得要自行加上 --color=auto 需要在 ~/.bashrc 内加上这行:『alias grep='grep --color=auto'』再以『 source ~/.bashrc 』来立即生效即可

RHEL6基础之八查找、文件内容查看类命令

[[email protected] ~]# source /root/.bashrc

-v---反向查找,即输出与查找条件不相符的行

RHEL6基础之八查找、文件内容查看类命令

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

[[email protected] home]# echo "a" > i
[[email protected] home]# echo "A" >> i
[[email protected] home]# cat i
a
A
[[email protected] home]# grep "a" i
a
[[email protected] home]# grep -i "a" i
a
A
[[email protected] home]#


-E---多个字符串一起搜寻,就是egrep

[[email protected] ~]# cat /etc/samba/smb.conf |egrep -v '#|;'

过滤掉#号和;号的行

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

[[email protected] ~]# cat /boot/grub/grub.conf | grep -A 2 title
title Red Hat Enterprise Linux (2.6.32-279.el6.i686)
    root (hd0,0)
    kernel /vmlinuz-2.6.32-279.el6.i686 ro root=UUID=31d213fb-c46d-4b1d-9300-f43c0c7afb94 rd_NO_LUKS  KEYBOARDTYPE=pc KEYTABLE=us rd_NO_MD crashkernel=auto LANG=zh_CN.UTF-8 rd_NO_LVM rd_NO_DM rhgb quiet
[[email protected] ~]# cat /boot/grub/grub.conf | grep title
title Red Hat Enterprise Linux (2.6.32-279.el6.i686)
[[email protected] ~]#

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

[[email protected] ~]# cat /boot/grub/grub.conf | grep -B 2 title
splashimage=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
title Red Hat Enterprise Linux (2.6.32-279.el6.i686)
[[email protected] ~]#

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

[[email protected] ~]# cat /boot/grub/grub.conf | grep -C 2 title
splashimage=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
title Red Hat Enterprise Linux (2.6.32-279.el6.i686)
    root (hd0,0)
    kernel /vmlinuz-2.6.32-279.el6.i686 ro root=UUID=31d213fb-c46d-4b1d-9300-f43c0c7afb94 rd_NO_LUKS  KEYBOARDTYPE=pc KEYTABLE=us rd_NO_MD crashkernel=auto LANG=zh_CN.UTF-8 rd_NO_LVM rd_NO_DM rhgb quiet
[[email protected] ~]#

-c或--count 计算符合范本样式行数。

[[email protected] ~]# cat /boot/grub/grub.conf | grep -c 2.6.32
3
[[email protected] ~]# cat /boot/grub/grub.conf
# grub.conf generated by anaconda
#
# Note that you do not have to rerun grub after making changes to this file
# NOTICE:  You have a /boot partition.  This means that
#          all kernel and initrd paths are relative to /boot/, eg.
#          root (hd0,0)
#          kernel /vmlinuz-version ro root=/dev/sda2
#          initrd /initrd-[generic-]version.img
#boot=/dev/sda
default=0
timeout=5
splashimage=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
title Red Hat Enterprise Linux (2.6.32-279.el6.i686)
    root (hd0,0)
    kernel /vmlinuz-2.6.32-279.el6.i686 ro root=UUID=31d213fb-c46d-4b1d-9300-f43c0c7afb94 rd_NO_LUKS  KEYBOARDTYPE=pc KEYTABLE=us rd_NO_MD crashkernel=auto LANG=zh_CN.UTF-8 rd_NO_LVM rd_NO_DM rhgb quiet
    initrd /initramfs-2.6.32-279.el6.i686.img
[[email protected] ~]#

-r或--recursive 递归的搜索

当要只知道要查找的字符串大致的目录位置不清除具体路径,通过递归搜索查找到

-L:输出时只显示不包含匹配项的文件名。

-l ----只显示匹配字符串的文件名不显示匹配的行,小写的L,通常和-r 一起使用

[[email protected] ~]# mkdir -p /home/grep/{grep1,grep2}
[[email protected] ~]# touch /home/grep/grep1/a.txt
[[email protected] ~]# echo "1 2 3" > /home/grep/grep1/a.txt
[[email protected] ~]# touch /home/grep/grep1/1.txt
[[email protected] ~]# echo "a b c" > /home/grep/grep1/1.txt
[[email protected] ~]# grep "a" /home/grep/
[[email protected] ~]# grep "a" -r /home/grep/
/home/grep/grep1/1.txt:a b c
[[email protected] ~]# grep "a" -rl /home/grep/
/home/grep/grep1/1.txt
[[email protected] ~]#

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

[[email protected] ~]# cat /boot/grub/grub.conf | grep -n title
14:title Red Hat Enterprise Linux (2.6.32-279.el6.i686)
[[email protected] ~]#

-o:只显示被模式匹配到的字符串,而不是整个行

[[email protected] ~]# cat /boot/grub/grub.conf | grep -no title
14:title
[[email protected] ~]#

-h:输出时每行行首不显示文件名

。-H:输出时每行行首显示文件名。

-w, –word-regexp:精确匹配,匹配单词还不是字符串,如想匹配“justin”,”justin_peng”就不会被匹配

\<: 单词锚定词首,例如\<r..t 那么root、rooter均可以匹配,而chroot就不能匹配

\>: 单词锚定词尾,例如r..t\> 那么root、chroot均可匹配,而chroot就不能匹配,如果要锚定root 可以使用\<root\>

[[email protected] home]# cat grep.txt 
justin
justin_peng
[[email protected] home]# grep "justin" grep.txt 
justin
justin_peng
[[email protected] home]# grep -w "justin" grep.txt 
justin
[[email protected] home]#

-R, -r, –recursive:递归搜索,你搜索的目录或子目录下的所有含有某个你要找的文件.

[[email protected] grep]# grep "justin" /home/
[[email protected] grep]# grep -R "justin" /home/
/home/grep/grep.txt:justin
/home/grep.txt:justin
/home/grep.txt:justin_peng
[[email protected] grep]#

-R, -r, --recursive       equivalent to --directories=recurse
      --include=FILE_PATTERN  只搜索匹配FILE_PATTERN的文件
      --exclude=FILE_PATTERN  忽略匹配FILE_PATTERN的文件和目录
      --exclude-from=FILE   忽略匹配从FILE中获取的FILE_PATTERN的文件
      --exclude-dir=PATTERN  忽略匹配FILE_PATTERN的目录

[[email protected] app]# grep -ir '/home/neo4j/' neo4jDB_update -r --exclude=*.log --exclude=*.heapsnapshot
#或者
[[email protected] app]# grep -ir '/home/neo4j/' neo4jDB_update -r --exclude=*.{log,heapsnapshot}

忽略/home/neo4j/目录及其子目录下所有以.log或者.heapsnapshot结尾的文件

[[email protected] app]# grep -ir '/home/neo4j/' neo4jDB_update -R --include=source.yml.20180903bak

目录下source.yml.20180903bak里搜索指定的字符

[[email protected] app]# grep -ir '/home/neo4j/' neo4jDB_update -r --exclude-dir={logs,log,heapDumpFile}

指定目录里排除特定的目录的目录里搜索指定字符

注意,这里include、exclude都是文件名或者目录名,不是文件或者目录的路径,-R或者-r都是递归搜索


对多个文件进行搜索

# grep -n 'linux' test.txt  test2.txt


正则匹配:

符号”[]“表示匹配指定范围内的任意单个字符,'passw[Oo]rd'匹password和passwOrd

[[email protected] ~]# echo "password" > /home/grep/grep1/a.txt
[[email protected] ~]# echo "passwOrd" >> /home/grep/grep1/a.txt
[[email protected] ~]# grep -nr 'passw[oO]rd' /home/grep/
/home/grep/grep1/a.txt:1:password
/home/grep/grep1/a.txt:2:passwOrd
[[email protected] ~]#

[] 里面不论有几个字节,他都谨一个一个字节的匹配,也就是只会匹配password和passwOrd,而不会匹配passwoOrd,在一组集合字节中,如果该字节组是连续的,例如大写英文/小写英文/数字等等, 就可以使用[a-z],[A-Z],[0-9],[a-zA-Z]等方式来书写

如果要匹配空格,最好用空格的[:space:]来匹配,如果直接用空格来匹配那么tab产生的空格是不会匹配的

[[email protected] ~]# grep '[[:space:]]' grep.txt

[[:alpha:]] 代表英文大小写字母 a-z A-Z

[[:upper:]] :表示大写字母[A-Z]

[[:alnum:]]: 所有的字母和数字

[[:blank:]] [[:space:]] 代表空格键与 [Tab] 按键两者

[[:digit:]] :表示数字 [0-9]


符号"[^]"表示匹配指定范围之外的任意单个字符,如:'[^a-fA-F]oo'匹配不包含A-F和a-f的一个字母开头,紧跟oo的行。

[[email protected] ~]# echo "Google" > /home/grep/grep1/1.txt
[[email protected] ~]# echo "google" >> /home/grep/grep1/1.txt
[[email protected] ~]# echo "Foogle" >> /home/grep/grep1/1.txt
[[email protected] ~]# echo "foogle" >> /home/grep/grep1/1.txt
[[email protected] ~]# cat /home/grep/grep1/1.txt
Google
google
Foogle
foogle
[[email protected] ~]# grep '[^a-fA-F]oo' /home/grep/grep1/1.txt
Google
google
[[email protected] ~]#

符号“^”表示以什么字符开头,“^word”表示以“word”开头,如果word前面有空格时需要使用[:space:]来匹配前面的空格[[email protected]_server home]# grep '^[[:space:]]*root' punct.txt

符号“$”表示以什么字符结尾,“word$”表示以“word”结尾。如果word后面有符号匹配时候需要使用[:punct:]来匹配后的符号[[email protected]_server home]# grep 'root[[:punct:]]*$' punct.txt 表示匹配punct.txt文件中以root单词结尾后面接任意多个符号的行

匹配行尾为小数点(.)因为小数点具有其他意义(底下会介绍),所以必须要使用转义字符(\)来加以解除其特殊意义,转义为一个普通字符!

[[email protected] ~]# grep '.$' /home/grep/grep1/a.txt
grep
grap
grbp
grabp
grcbp
grcbp.
[[email protected] ~]# grep '\.' /home/grep/grep1/a.txt
grcbp.
[[email protected] ~]#

小数点”.“表示任意一个非换行符的字符,'gr.p'匹配gr后接一个任意字符,然后是p

[[email protected] ~]# echo "grep" > /home/grep/grep1/a.txt
[[email protected] ~]# echo "grap" >> /home/grep/grep1/a.txt
[[email protected] ~]# echo "grbp" >> /home/grep/grep1/a.txt
[[email protected] ~]# echo "grabp" >> /home/grep/grep1/a.txt
[[email protected] ~]# echo "grcbp" >> /home/grep/grep1/a.txt
[[email protected] ~]# grep 'gr.p' /home/grep/grep1/a.txt
grep
grap
grbp
[[email protected] ~]#

星号“*”:基本正则表达式中表示匹配其前的字符出现0次或任意次数,例如ab*c,那么ac、abc、acc都会匹配,而abdc就不匹配,星号在基本正则表达式里只指次数,*不会匹配隐藏文件。只表示次数,不表示任意字符


扩展正则表达式中多以下元字符:

+ 匹配一个或多个前面的字符.它的作用和*很相似,但唯一的区别是它不匹配零个字 符的情况,如果要指定范围使用{n,m}来指定,如查找一位或2位数值的行:

[[email protected]_server ~]# egrep --color=auto '\<[0-9]{1,3}[[:space:]]' /proc/meminfo 
[[email protected]_server ~]# egrep --color=auto '\<[0-9]{3}[[:space:]]' /proc/meminfo 
[[email protected]_server ~]# egrep --color=auto '\<[0-9]+[[:space:]]' /proc/meminfo

| 表示或关系

例如egrep 'abc|def' /proc/meminfo  查找abc或者def

分组

例如 egrep 'abc|def' /proc/meminfo  查找abc或者def

       egrep 'ab(c|d)ef' /proc/meminfo  查找abcef或者abdef      

[[email protected]_server home]# egrep 'abc|def' 12.txt --color=auto
abc
abcdef
abcef
abdef
[[email protected]_server home]# egrep 'ab(c|d)ef' 12.txt --color=auto
abcef
abdef
[[email protected]_server home]#

问号“?”:基本正则表达式中表示匹配其前的字符出现0次或1次数。只做次数匹配,不做字符匹配

[[email protected] ~]# echo "google" > /home/grep/grep1/a.txt
[[email protected] ~]# echo "goooooooooooooogle" >> /home/grep/grep1/a.txt
[[email protected] ~]# grep 'g..gle' /home/grep/grep1/a.txt
google
[[email protected] ~]# grep 'g*gle' /home/grep/grep1/a.txt
google
goooooooooooooogle
[[email protected] ~]# grep 'g.gle' /home/grep/grep1/a.txt
[[email protected] ~]#

如果想查找两个字符之间任意个字符的条件,可以通过小数点和星号一起来匹配,如查找前尾分别为gr和g的行

[[email protected] ~]# grep 'gr.*g' /home/grep/grep1/a.txt

字符范围\{m,n \}:限制前一个字符重复出现最少m次,最多n次数

因为 { 与 } 的符号在 shell 是有特殊意义的,因此必须要使用字符   \ 来让他失去特殊意义

\{m,\} 表示至少m次,

\{0,n\} 表示最多n次,不能使用\{,n\}

\{m\}   表示只出现m次

() 将候选的所有元素放在()内,用|隔开

        "a(1|2|3)bc"满足的例子a1bc、mba3bcd

如果要以组的形式匹配,例如xaby 要匹配ab组合出现多次可以使用\(\)来匹配,例如x\(ab\)*y,表示xy中间中ab出现0次或n次

前向引用

[[email protected]_server home]# cat 1.txt 
He love his lover.
She like her liker.
He love his liker.
She like her lover.
[[email protected]_server home]#

例如要匹配love和lover、like和liker同时所在行

[[email protected]_server home]# grep '\(l..e\).*\1r' 1.txt 
He love his lover.
She like her liker.
[[email protected]_server home]#

这里\1表示匹配第一个左括号内l..e相同的内容,\(l\(..\)e\).*\2r表示匹配\(..\)这个内..相同的内容,以此类推

查找出现两个o的行

[[email protected] ~]# cat /home/grep/grep1/a.txt
"Open Source" is a good mechanism to develop programs.
apple is my favorite food.
Football game is not use feet only.
Oh! The soup taste good.
google is the best tools for search ke
goooooogle yes!
[[email protected] ~]# grep 'o\{2\}' /home/grep/grep1/a.txt
"Open Source" is a good mechanism to develop programs.
apple is my favorite food.
Football game is not use feet only.
Oh! The soup taste good.
google is the best tools for search ke
goooooogle yes!
[[email protected] ~]#

查找g后面出现2到5次o,然后紧跟g的行:

[[email protected] ~]# grep 'go\{2,5\}g' /home/grep/grep1/a.txt
google is the best tools for search ke
[[email protected] ~]#

查找g后面出现两次以上o后面跟g的行:

[[email protected] ~]# grep 'go\{2,\}g' /home/grep/grep1/a.txt
google is the best tools for search ke
goooooogle yes!
[[email protected] ~]# grep 'go*g' /home/grep/grep1/a.txt
google is the best tools for search ke
goooooogle yes!
[[email protected] ~]#

正则表达式特殊符号

字符类 代表意义
[:alnum:] 代表英文大小写字符及数字,即0-9,A-Z,a-z
[:alpha:] 代表任何英文大小字符,即A-Z,a-z
[:lower:] 代表小写字符,即a-z
[:upper:] 代表大写字符,即A-Z
[:digit:] 代表数字,即0-9
[:xdigit:] 代表十六进制的数字类型,因此包括0-9,A-F,a-f的数字与字符
[:blank:] 代表空格键与tab按键
[:graph:] 除了空格与tab按键之外的其它所有按键
[:space:] 任何会产生空白的字符,包括空格键,Tab键,CR等
[:cntrl:] 代表键盘上面的控制按键,既包括CR,LF,Tab,Del等
[:print:] 代表任意可打印字符
[:punct:] 代表标点符号,即" ' ? ! ; : # $



cat

cat只适合查看内容较短的文件,无法上翻页,只能显示、查看一屏内容

tac同上,只是倒叙显示内容

[[email protected] home]# cat test
111
222
333
[[email protected] home]# tac test
333
222
111
[[email protected] home]#

more

more适合查看长文件,可以上、下翻页,按空格键下翻一页,按Enter下翻一行,按b上翻一页,cat和more可以配合使用

[[email protected] log]# more boot.log
[[email protected] log]# cat boot.log |more

less

less使用查看长文件,而且可以搜索【关键词】,按N/n查找前后关键字

[[email protected] log]# less secure
Jan 22 10:58:30 justin sshd[1716]: Server listening on :: port 22.
Jan 22 10:59:12 justin sshd[1901]: Accepted password for root from 10.15.72.73 port 60959 ssh2
Jan 22 10:59:13 justin sshd[1901]: pam_unix(sshd:session): session opened for user root by (uid=0)
Jan 22 10:59:13 justin sshd[1901]: subsystem request for sftp
/port 22

head

查看文件头N行数据,默认头10,通过-n指定查看行数;

[[email protected] log]# head -n 2 secure
Nov 13 17:31:16 localhost sshd[1582]: Server listening on 0.0.0.0 port 22.
Nov 13 17:31:16 localhost sshd[1582]: Server listening on :: port 22.
[[email protected] log]#

tail

查看文件末尾n行,用法同上;参数-f动态实时

[[email protected] log]# tail -n 2 secure -f
[[email protected] log]# tail -fn 2 secure


转载于:https://blog.51cto.com/ityunwei2017/1301078

相关文章:

  • 2021-07-27
  • 2021-11-17
  • 2022-02-06
  • 2021-11-21
  • 2021-11-21
  • 2021-11-21
  • 2021-06-28
  • 2022-12-23
猜你喜欢
  • 2022-02-08
  • 2021-10-27
  • 2022-01-13
  • 2021-12-03
  • 2022-12-23
  • 2021-12-19
相关资源
相似解决方案