您的位置:首页 > 运维架构 > Linux

linux 查找出包含某个字符串的所有文件的方法详解

2015-09-13 19:20 651 查看
一、find查找用法

find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]

find默认的寻找路径为当前目录,并且默认的表达式为-print

 伦理片 http://www.dotdy.com/

1、列出某个路径下所有文件,包括子目录。

find /data -name “*”

2、查找在某个路径下所有包含“test abc”字符串的文件

find /data -name “*” | xargs grep “test abc”

3、-name:按照文件名查找

find /data -name “*.txt”

4、-perm:按照权限查找文件

find /data -perm 755  查找权限为755的文件

5、-user和-nouser

find /data -user zhao -print 查找文件属主是zhao的文件

find /data -nouser -print 查找文件属主已经被删除的文件

6、-group和-nogroup

find /data -group test -print 查找文件群组是zhao的文件

7、按照时间

find /data -mtime -5 -print 文件更改时间在5天内的文件

find /data -mtime +3 -print 文件更改时间在3天前的文件

find /data -newer file1 -print 查找比文件file1新的文件

8、按照类型查找

find /data -type d -print 查找所有目录

9、按照大小

find /data -size +1000000C -print 查找文件大小大于1000000字节(1M)的文件

10、查找位于本文件系统里面的文件

find /data -name “*.txt” -mount -print

-exec,-ok:find命令对于匹配文件执行该参数所给出shell命令,相应命令形式为: ‘command’ {} \;

-ok 在执行命令前要确认

find /data -type f -exec ls -l {} \;

find /data -name “*.log” -mtime +5 -ok rm {} \;

find /data -name core -exec rm {} \;

 

注意:-exec 在{}和\之间必须要有空格,同时\;之间不能有空格,否则会报上面的错。 

使用-x dev参数

防止find搜索其他分区

find . -size 0 -exec rm {} \;

删除尺寸为0的文件

11. xargs与-exec功能类似

find /data -type f |xargs grep -i “Mary”

在所有文件中检索字符串Mary

ls *~ |xargs rm -rf 删除所有以~结尾的文件

二、grep用法

Usage: grep [OPTION]... PATTERN [FILE]..

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                ignored for backwards compatibility

 

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

 

grep与正则表达式同用

grep “^[^abc]” *.f 提取行首不是abc的行

grep “[0-9]\{3\}[8]” *.f 提取类似xxx8的行,x表示任意数字

grep “a\{2\}” *.f 显示a至少出现两次的行,注意grep是以行处理单位的

grep -n “^$” file 列出空行的行号

-E参数,可以使用”与”和”或”模式

grep -E “abc | def” *.f 显示包含abc或者def的行

ex:

ls -l | grep “^$” 显示目录

ls -l | grep “^d” 显示文件

ls -l | grep “^d..x..x..x” 显示特定权限的目录
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: