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

如题所述

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

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

说明:

-r 是递归查找

-n 是显示行号

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

温馨提示:答案为网友推荐,仅供参考
第1个回答  2018-03-30

查找目录下的所有文件中是否含有某个使用: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 表示递归查找子目录 目标字符可带**等符号 可不带引号。

本回答被网友采纳
第2个回答  2016-04-01
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.)
第3个回答  推荐于2017-12-15
grep -rnl '要搜索的字符串' *
*表示当前目录的所有文件和子文件夹,可以替换为某个文件夹名或者指定的文件名
-r 表示递归查找
-n 表示不显示匹配所在的行号
-l 表示只输出文件名本回答被提问者采纳
第4个回答  2016-03-20
简单来说如果是你查找某目录下的文件名就使用find ,如果是查找某文件内容,可以使用grep .两个命令都有具体的用法,自行百度《linux 就该这么学》第二章 里有详细的解释和示例。

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网