linux查找目录下的所有文件中是否含有某个字符串

 我来答
吉祥二进制
高粉答主

推荐于2018-02-23 · 科技改变生活,生活改变科技。
吉祥二进制
采纳数:33926 获赞数:84579

向TA提问 私信TA
展开全部

1、可以使用grep命令来查找当前目录下所有文件中包含的某个特定字符。

2、示例:查找当前目录下所有带有set的文件 。 

说明:

-r 是递归查找

-n 是显示行号

* : 表示当前目录所有文件,也可以是某个文件名

WW52323
2018-03-30 · TA获得超过9121个赞
知道小有建树答主
回答量:25
采纳率:100%
帮助的人:3657
展开全部

查找目录下的所有文件中是否含有某个字符串使用:find .|xargs grep -ri "IBM"。正则表达式一般用来描述文本模式的特殊用法,由普通字符以及特殊字符组成。

在linux下查找某目录下所有文件包含某字符串的命令: 

1、从文件内容查找匹配指定字符串的行: $ grep "被查找的字符串" 文件名 。

2、从文件内容查找与正则表达式匹配的行: $ grep –e “正则表达式” 文件名 。

3、查找时不区分大小写: $ grep –i "被查找的字符串" 文件名 。

4、查找匹配的行数: $ grep -c "被查找的字符串" 文件名 。

5、从文件内容查找不匹配指定字符串的行: $ grep –v "被查找的字符串" 文件名 。

6、从根目录开始查找所有扩展名为.txt的文本文件,并找出包含"phpzixue.cn"的行 
find . -type f -name "*.txt" | xargs grep "phpzixue.cn"表示当前目录 -type 表示类型 f 表示普通文件 xargs 表示递归查找子目录 目标字符可带**等符号 可不带引号。

本回答被网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
fateland
2016-04-01 · TA获得超过1536个赞
知道小有建树答主
回答量:1509
采纳率:0%
帮助的人:996万
展开全部
grep "要查找的字符串" /目录/*.*
方法有很多。这是其中一种。
使用命令 man grep可查看帮助文档。

NAME
grep, egrep, fgrep - print lines matching a pattern

SYNOPSIS
grep [OPTIONS] PATTERN [FILE...]
grep [OPTIONS] [-e PATTERN | -f FILE] [FILE...]

DESCRIPTION
grep searches the named input FILEs (or standard input if no files are named, or if a single hyphen-minus (-)
is given as file name) for lines containing a match to the given PATTERN. By default, grep prints the matching
lines.

In addition, two variant programs egrep and fgrep are available. egrep is the same as grep -E. fgrep is the
same as grep -F. Direct invocation as either egrep or fgrep is deprecated, but is provided to allow historical
applications that rely on them to run unmodified.
OPTIONS
Generic Program Information
--help Print a usage message briefly summarizing these command-line options and the bug-reporting address, then
exit.

-V, --version
Print the version number of grep to the standard output stream. This version number should be included
in all bug reports (see below).

Matcher Selection
-E, --extended-regexp
Interpret PATTERN as an extended regular expression (ERE, see below). (-E is specified by POSIX.)

-F, --fixed-strings, --fixed-regexp
Interpret PATTERN as a list of fixed strings, separated by newlines, any of which is to be matched. (-F
is specified by POSIX, --fixed-regexp is an obsoleted alias, please do not use it in new scripts.)

-G, --basic-regexp
Interpret PATTERN as a basic regular expression (BRE, see below). This is the default.

-P, --perl-regexp
Interpret PATTERN as a Perl regular expression. This is highly experimental and grep -P may warn of
unimplemented features.

Matching Control
-e PATTERN, --regexp=PATTERN
Use PATTERN as the pattern. This can be used to specify multiple search patterns, or to protect a
pattern beginning with a hyphen (-). (-e is specified by POSIX.)

-f FILE, --file=FILE
Obtain patterns from FILE, one per line. The empty file contains zero patterns, and therefore matches
nothing. (-f is specified by POSIX.)

-i, --ignore-case
Ignore case distinctions in both the PATTERN and the input files. (-i is specified by POSIX.)

-v, --invert-match
Invert the sense of matching, to select non-matching lines. (-v is specified by POSIX.)

-w, --word-regexp
Select only those lines containing matches that form whole words. The test is that the matching
substring must either be at the beginning of the line, or preceded by a non-word constituent character.
Similarly, it must be either at the end of the line or followed by a non-word constituent character.
Word-constituent characters are letters, digits, and the underscore.

-x, --line-regexp
Select only those matches that exactly match the whole line. (-x is specified by POSIX.)

-y Obsolete synonym for -i.

General Output Control
-c, --count
Suppress normal output; instead print a count of matching lines for each input file. With the -v,
--invert-match option (see below), count non-matching lines. (-c is specified by POSIX.)

--color[=WHEN], --colour[=WHEN]
Surround the matched (non-empty) strings, matching lines, context lines, file names, line numbers, byte
offsets, and separators (for fields and groups of context lines) with escape sequences to display them
in color on the terminal. The colors are defined by the environment variable GREP_COLORS. The
deprecated environment variable GREP_COLOR is still supported, but its setting does not have priority.
WHEN is never, always, or auto.

-L, --files-without-match
Suppress normal output; instead print the name of each input file from which no output would normally
have been printed. The scanning will stop on the first match.

-l, --files-with-matches
Suppress normal output; instead print the name of each input file from which output would normally have
been printed. The scanning will stop on the first match. (-l is specified by POSIX.)
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
特中戏0H78a
推荐于2017-12-15 · TA获得超过292个赞
知道小有建树答主
回答量:293
采纳率:0%
帮助的人:233万
展开全部
grep -rnl '要搜索的字符串' *
*表示当前目录的所有文件和子文件夹,可以替换为某个文件夹名或者指定的文件名
-r 表示递归查找
-n 表示不显示匹配所在的行号
-l 表示只输出文件名
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
百度网友e2629ea
2016-03-20 · TA获得超过289个赞
知道答主
回答量:13
采纳率:0%
帮助的人:6.8万
展开全部
简单来说如果是你查找某目录下的文件名就使用find ,如果是查找某文件内容,可以使用grep .两个命令都有具体的用法,自行百度《linux 就该这么学》第二章 里有详细的解释和示例。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(5)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式