MENU

find、grep、wc使用方法简单总结

April 3, 2018 • Read: 4316 • 常用命令

简单总结一下,几个常用的linux命令。

一、find

   用来查找特定文件,在linux下一切皆是文件,因此十分有用。

   基本格式:`find  path  expression  [-exec  command  {} \;]` 

  会默认遍历到子目录。

1、find /home/work/ -name "*.cpp"

   查找/home/work/目录下,所有文件名以.cpp结尾的文件,当然目录也会匹配出来。

2、find /home/work/ -name -type f "*.cpp"

    查找/home/work/目录下,所有文件名以.cpp结尾的普通文件。

3、find /home/work/ -iname "*.cpp"

  -iname 表示忽略大小写,-name表示不忽略大小写,精确匹配。

4、find /home/work/ /etc/ /usr -name "*.cpp"

  查找多个目录下的.cpp文件

 扩展 : `find  /home/work/   /etc/  /usr  -name  "[ab].cpp"`     查找a.cpp或者b.cpp文件

              `find  /home/work/   /etc/  /usr  -name  "?.cpp"`           "?"表示任意一个字符

5、 find /home/work/ -size +20M

  查找大于20M的文件,-20M表示小于20M

6、find /home/work -atime -5

  查找5天内访问过的文件, +5 表示5天内没有访问过的文件。

  扩展:用stat命令可以获得相关的时间戳。atime表示最近一次访问的时间,mtime表示最近一次内容修改的时间,ctime表示最近一次属性修改的时间,单位是天。amin,          mmin,cmin单位是分钟。

7、find /home/work -atime -5 -a -size -1M

  查找5天访问过的并且大小小于1M的文件。

 -a    与连接 ,   -o  或连接, -not  条件取反  

二、grep

    用来匹配特定的文本行,全称是Global Regular Expression Print。

    基本格式:  grep  [OPTIONS]   PATTERN    [FILE...]

1、grep "hello" ./*

   遍历当前目录下的所有文件,把含有"hello"的文本行的打印出来。

   `grep   -r  "hello"  ./*`  会遍历子目录。

2、其他参数控制

  -i    匹配时,忽略大小写

  -n   显示行号

  -c   只显示匹配到的文本行总数

   -l   只显示匹配到文本行的文件名

   -v  显示没有匹配到的文本行,相当于取反操作。

三、wc

    文本统计工具,全城word count。使用方法很简单。能将文件的行数、字数、字节数打印出来。

1、wc -l test.txt

   统计test.txt中文本的行数。

2、wc -c test.txt

   统计字节数    

3、wc -w test.txt

  统计字数,我所理解的字就是一个单词,单词都是又空格、tab键、换行符来分割的。

4、wc -m test.txt

   统计字符数。这和字节数是有区别的,因为一个中文字符会占3个字节。

四、实例操作

1、统计/home/work/目录下所有以.txt结尾的文件个数(不包括目录)

find /home/work/ -type f -name "*.txt" | wc -l

2、删除/home/work/目录下所有以.txt结尾的文件

find /home/work/ -type f -name "*.txt" -exec rm -f {} ;

  -exec 后面跟着执行动作,就是一个操作命令,即每查找到一个文件就执行rm -f 动作,{}就代表查找到的文件,\; 表示命令的结束。

 注意,{}和\之间有空格。

 也可以这样做:

find /home/work/ -type f -name "*.txt" | xargs rm -f

3、统计/home/work/当前目录下有多少普通文件(即不遍历子目录)

ls /home/work -l | grep '^-' |wc -l

  -l 参数显示出文件的属性,开头为‘-’表示普通文件,从而排除目录,'^-'表示匹配开头为‘-’的字符串

兼总条贯 知至知终

Leave a Comment

已有 5 条评论
  1. Dina

    Helⅼo there, You've done a fantastic job.
    I'll ceгtaіnly digg it and persоnally recommmend to my fгiends.
    I'm confident they'll ƅe benefited from this web site.

  2. Leland

    I аbsolutely love your bblog and find many of уour post's to be just what I'm
    looking for. can you offer guest writers to wгite cоntent tto suit yoᥙr needs?

    I wouldn't mind producing a post or elaborating onn a lot of the subjects you write
    with regards to here. Again, awesome web log!

  3. Finlay

    Wow, thhis ⲣaragraph is pleasant, my sister
    is analyzing suⅽh things, tgerefore I am going
    to leet know her.

  4. Kam

    Generally I don't read post on blogs, however I wish tto say that
    this write-up very compelled me to take a look att and do
    so! Your writing style has been surprised me. Thanks,
    qite nice post.

  5. Fleta

    Thɑnk you a lot for sharing this with all off us you rеally understand what you're talking approximately!
    Bookmarked. Please additionall discuss with mmy site =).
    We will have a ⅼin aɑⅼternate arrɑngеment among us