首页 > 系统服务 > 详细

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

时间:2015-08-14 02:12:09      阅读:141      评论:0      收藏:0      [点我收藏+]

一、find查找用法

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

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

?

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 {} \;
使用-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” 显示特定权限的目录

?

?

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

原文:http://daizj.iteye.com/blog/2235106

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!