linux查找目录下的所有文件中是否含有某个字符串 linux查找目录下的所有文件中是否含有某个字符串
linux\u67e5\u627e\u76ee\u5f55\u4e0b\u7684\u6240\u6709\u6587\u4ef6\u4e2d\u662f\u5426\u542b\u6709\u67d0\u4e2a\u5b57\u7b26\u4e32grep -rn "hello,world!" *
* : \u8868\u793a\u5f53\u524d\u76ee\u5f55\u6240\u6709\u6587\u4ef6\uff0c\u4e5f\u53ef\u4ee5\u662f\u67d0\u4e2a\u6587\u4ef6\u540d
-r \u662f\u9012\u5f52\u67e5\u627e
-n \u662f\u663e\u793a\u884c\u53f7
-R \u67e5\u627e\u6240\u6709\u6587\u4ef6\u5305\u542b\u5b50\u76ee\u5f55
-i \u5ffd\u7565\u5927\u5c0f\u5199
\u4e0b\u9762\u662f\u4e00\u4e9b\u6709\u610f\u601d\u7684\u547d\u4ee4\u884c\u53c2\u6570\uff1a
grep -i pattern files \uff1a\u4e0d\u533a\u5206\u5927\u5c0f\u5199\u5730\u641c\u7d22\u3002\u9ed8\u8ba4\u60c5\u51b5\u533a\u5206\u5927\u5c0f\u5199\uff0c
grep -l pattern files \uff1a\u53ea\u5217\u51fa\u5339\u914d\u7684\u6587\u4ef6\u540d\uff0c
grep -L pattern files \uff1a\u5217\u51fa\u4e0d\u5339\u914d\u7684\u6587\u4ef6\u540d\uff0c
grep -w pattern files \uff1a\u53ea\u5339\u914d\u6574\u4e2a\u5355\u8bcd\uff0c\u800c\u4e0d\u662f\u5b57\u7b26\u4e32\u7684\u4e00\u90e8\u5206\uff08\u5982\u5339\u914d\u2018magic\u2019\uff0c\u800c\u4e0d\u662f\u2018magical\u2019\uff09\uff0c
grep -C number pattern files \uff1a\u5339\u914d\u7684\u4e0a\u4e0b\u6587\u5206\u522b\u663e\u793a[number]\u884c\uff0c
grep pattern1 | pattern2 files \uff1a\u663e\u793a\u5339\u914d pattern1 \u6216 pattern2 \u7684\u884c\uff0c
grep pattern1 files | grep pattern2 \uff1a\u663e\u793a\u65e2\u5339\u914d pattern1 \u53c8\u5339\u914d pattern2 \u7684\u884c\u3002
\u8fd9\u91cc\u8fd8\u6709\u4e9b\u7528\u4e8e\u641c\u7d22\u7684\u7279\u6b8a\u7b26\u53f7\uff1a
\ \u5206\u522b\u6807\u6ce8\u5355\u8bcd\u7684\u5f00\u59cb\u4e0e\u7ed3\u5c3e\u3002
\u4f8b\u5982\uff1a
grep man * \u4f1a\u5339\u914d \u2018Batman\u2019\u3001\u2018manic\u2019\u3001\u2018man\u2019\u7b49\uff0c
grep '\<man' * \u5339\u914d\u2018manic\u2019\u548c\u2018man\u2019\uff0c\u4f46\u4e0d\u662f\u2018Batman\u2019\uff0c
grep '\' \u53ea\u5339\u914d\u2018man\u2019\uff0c\u800c\u4e0d\u662f\u2018Batman\u2019\u6216\u2018manic\u2019\u7b49\u5176\u4ed6\u7684\u5b57\u7b26\u4e32\u3002
'^'\uff1a\u6307\u5339\u914d\u7684\u5b57\u7b26\u4e32\u5728\u884c\u9996\uff0c
'$'\uff1a\u6307\u5339\u914d\u7684\u5b57\u7b26\u4e32\u5728\u884c\u5c3e\uff0c
2\uff0cxargs\u914d\u5408grep\u67e5\u627e
find -type f -name '*.php'|xargs grep 'GroupRecord'
grep -rn "\u8981\u67e5\u627e\u7684\u5b57\u7b26\u4e32" *
-r \u662f\u9012\u5f52\u67e5\u627e
-n \u662f\u663e\u793a\u884c\u53f7
* : \u8868\u793a\u5f53\u524d\u76ee\u5f55\u6240\u6709\u6587\u4ef6\uff0c\u4e5f\u53ef\u4ee5\u662f\u67d0\u4e2a\u6587\u4ef6\u540d\u3002
grep\u547d\u4ee4\u7684\u5e38\u7528\u683c\u5f0f\u4e3a\uff1agrep [\u9009\u9879] \u201d\u6a21\u5f0f\u201c [\u6587\u4ef6]\u3002
\u6a21\u5f0f\u90e8\u5206\uff1a
1\u3001\u76f4\u63a5\u8f93\u5165\u8981\u5339\u914d\u7684\u5b57\u7b26\u4e32\uff0c\u8fd9\u4e2a\u53ef\u4ee5\u7528fgrep\uff08fast grep\uff09\u4ee3\u66ff\u6765\u63d0\u9ad8\u67e5\u627e\u901f\u5ea6\uff0c\u6bd4\u5982\u6211\u8981\u5339\u914d\u4e00\u4e0bhello.c\u6587\u4ef6\u4e2dprintf\u7684\u4e2a\u6570\uff1afgrep -c "printf" hello.c\u3002
2\u3001\u4f7f\u7528\u57fa\u672c\u6b63\u5219\u8868\u8fbe\u5f0f\u3002
\u6269\u5c55\u8d44\u6599\uff1a
\u67e5\u627e\u5b57\u7b26\u4e32\u7684\u7b2c\u4e8c\u79cd\u65b9\u6cd5\uff1a
find .|xargs grep \u201c\u8981\u67e5\u627e\u7684\u5b57\u7b26\u4e32\u201d
find . -exec grep \u201c\u8981\u67e5\u627e\u7684\u5b57\u7b26\u4e32\u201d {} \;
find / -name "\u8981\u67e5\u627e\u7684\u5b57\u7b26\u4e32"
find / -name "\u8981\u67e5\u627e\u7684\u5b57\u7b26\u4e32"
1、可以使用grep命令来查找当前目录下所有文件中包含的某个特定字符。
2、示例:查找当前目录下所有带有set的文件 。
说明:
-r 是递归查找
-n 是显示行号
* : 表示当前目录所有文件,也可以是某个文件名
查找目录下的所有文件中是否含有某个字符串使用:find .|xargs grep -ri "IBM"。正则表达式一般用来描述文本模式的特殊用法,由普通字符以及特殊字符组成。
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.)
grep -rnl '要搜索的字符串' *
*表示当前目录的所有文件和子文件夹,可以替换为某个文件夹名或者指定的文件名
-r 表示递归查找
-n 表示不显示匹配所在的行号
-l 表示只输出文件名
简单来说如果是你查找某目录下的文件名就使用find ,如果是查找某文件内容,可以使用grep .两个命令都有具体的用法,自行百度《linux 就该这么学》第二章 里有详细的解释和示例。
绛旓細1銆佺櫥褰昄inux绯荤粺涔嬪悗鎵撳紑缁堢锛岀‘瀹氫綘瑕佹壘鍏抽敭瀛楃殑鐩綍銆2銆佹煡鐪嬩笅褰撳墠鐩綍涓嬫墍鏈夌殑鏂囦欢锛屼綘鎵瑕佹壘鐨勫叧閿瓧灏卞湪褰撳墠鐩綍涓嬫煇涓枃浠堕噷闈3銆乫ind -type f -print杩欎釜鍛戒护鍙互鍒楀嚭褰撳墠鐩綍涓嬬殑鎵鏈夋枃浠躲4銆乼ype鍚庨潰鐨刦鏄痜ile鐨勬剰鎬濓紝鎵浠ュ彧鍒楀嚭鏂囦欢锛岃屾帓闄ゆ枃浠跺す銆5銆佹帴涓嬫潵鍙鍦╢ind鍛戒护鍚庨潰鍔犱笂ex...
绛旓細-nogroup 鏌ユ壘鏃犳湁鏁堟墍灞炵粍鐨勬枃浠锛屽嵆璇ユ枃浠舵墍灞炵殑缁勫湪/etc/groups涓笉瀛樺湪銆-nouser 鏌ユ壘鏃犳湁鏁堝睘涓荤殑鏂囦欢锛屽嵆璇ユ枃浠剁殑灞炰富鍦/etc/passwd涓笉瀛樺湪銆-newer file1 ! file2 鏌ユ壘鏇存敼鏃堕棿姣鏂囦欢file1鏂颁絾姣旀枃浠秄ile2鏃х殑鏂囦欢銆-type 鏌ユ壘鏌愪竴绫诲瀷鐨勬枃浠讹紝璇稿锛歜 - 鍧楄澶囨枃浠躲俤 - 鐩綍銆俢 -...
绛旓細绛旀锛氬湪Linux涓嬶紝鍙互浣跨敤`find`鍛戒护鏉ユ煡鎵句竴涓枃浠跺す鎵鍦ㄧ殑鐩綍銆傝缁嗚В閲婏細1. 浣跨敤`find`鍛戒护锛 `find`鏄疞inux绯荤粺涓竴涓潪甯稿己澶х殑鍛戒护锛屽彲浠ョ敤鏉ユ悳绱㈡枃浠剁郴缁熶腑鐨勬枃浠舵垨鐩綍銆傞氳繃鎸囧畾鎼滅储鐨勮捣濮嬭矾寰勫拰瑕佹悳绱㈢殑鍐呭锛宍find`鍛戒护鍙互閫掑綊鍦伴亶鍘嗙洰褰曞苟鎵惧埌鍖归厤椤广2. 鍩烘湰璇硶锛 浣跨敤`find`鍛戒护鏌ユ壘...
绛旓細Is鏄疞ist鐨勭缉鍐欙紝琛ㄧず鏄剧ず鏂囦欢鐩綍鍒楄〃銆傦紙鍙樉绀洪潪闅愯棌鏂囦欢鐨鏂囦欢鍚嶏級鍙傛暟锛-a锛--all鐨勭缉鍐欙紝鏄剧ず鎵鏈夋枃浠锛屽寘鎷殣钘忔枃浠讹紙浠 . 寮澶寸殑鏂囦欢锛-L锛氬垪鍑洪暱鏁版嵁涓诧紝鏄剧ず鏂囦欢鐨勫ぇ灏忥紝鏃堕棿绛夋暟鎹俊鎭俵s -la锛氳〃绀烘樉绀烘枃浠跺寘鎷鐨勬墍鏈淇℃伅銆
绛旓細1銆侀鍏堝湪璇ユ枃浠跺す涓偣鍑婚紶鏍囧彸閿紝鏂板缓銆佹枃鏈枃浠躲2銆佸叾娆℃墦寮璇ユ枃鏈枃浠躲3銆佹渶鍚庡弻鍑昏繍琛屽垰鍒涘缓鐨勬枃浠跺嵆鍙
绛旓細鍦ㄦ病鏈夊浘褰㈢晫闈㈢殑Linux涓闇瑕佹煡鐪嬩竴涓鏂囦欢鐨勫唴瀹癸紝杩欓噷鍒嗕韩涓鏌ユ壘鏂规硶銆1銆侀鍏堝湪鐢佃剳涓墦寮Putty杞欢锛岃繛鎺ヤ笂Linux锛屽涓嬪浘鎵绀恒2銆佺劧鍚庡湪Linux涓煡鐪嬫枃浠跺唴瀹圭殑鏂瑰紡鏈変袱绉嶏紝涓绉嶆槸浣跨敤cat鍛戒护锛屽涓嬪浘鎵绀恒3銆佸湪cat鍛戒护鍚庨潰绱ц窡鐫鏂囦欢锛岀湅鍥句腑绀轰緥銆4銆佽繕鏈変竴绉嶆槸浣跨敤vi鍛戒护涔熸槸鍙互鐪嬪埌鏂囦欢鍐呭鐨勶紝...
绛旓細Linux涓嬫悳绱㈡枃浠锛屽彲浠ヤ娇鐢╢ind鍛戒护銆1銆乫ind鍛戒护鐢ㄦ潵鍦ㄦ寚瀹鐩綍涓嬫煡鎵炬枃浠銆備换浣曚綅浜庡弬鏁颁箣鍓嶇殑瀛楃涓查兘灏嗚瑙嗕负娆鏌ユ壘鐨鐩綍鍚嶃傚鏋滀娇鐢ㄨ鍛戒护鏃讹紝涓嶈缃换浣曞弬鏁帮紝鍒檉ind鍛戒护灏嗗湪褰撳墠鐩綍涓嬫煡鎵惧瓙鐩綍涓庢枃浠躲傚苟涓斿皢鏌ユ壘鍒扮殑瀛愮洰褰曞拰鏂囦欢鍏ㄩ儴杩涜鏄剧ず銆2銆佸懡浠や竴鑸牸寮忥細find(閫夐」)(鍙傛暟)3銆佸父鐢ㄥ弬鏁...
绛旓細Linux涓娴忚褰撳墠鐩綍涓嬫墍鏈浠".c"涓哄悗缂鍚鐨勬枃浠锛屽彲浠ヤ娇鐢╨s鍛戒护锛岀洿鎺ユ墽琛屽涓嬪懡浠:ls *.c 瑕佽幏鍙栨洿澶氫俊鎭彲浠ヤ娇鐢╨s鍛戒护鐨-l鍙傛暟 ls -l *.c
绛旓細ls鏄垪鍑轰竴鑸殑鏂囦欢鍜鏂囦欢澶锛宭s -a鏄垪鍑鎵鏈鐨勶紝鍖呮嫭闅愯棌鏂囦欢鍜岄殣钘忔枃浠跺す锛沴s -l鏄垪鍑烘枃浠剁殑璇︾粏淇℃伅锛沴s -la灏辨槸涓婇潰涓や釜鐨勫彔鍔犳晥鏋滃暒
绛旓細鏌ユ壘鐩綍涓嬬殑鎵鏈夋枃浠涓槸鍚﹀惈鏈夋煇涓瓧绗︿覆 find .|xargs grep -ri "IBM" 鏌ユ壘鐩綍涓嬬殑鎵鏈夋枃浠朵腑鏄惁鍚湁鏌愪釜瀛楃涓,骞朵笖鍙墦鍗板嚭鏂囦欢鍚 find .|xargs grep -ri "IBM" -l 1.姝e垯琛ㄨ揪寮 (1)姝e垯琛ㄨ揪寮忎竴鑸敤鏉ユ弿杩版枃鏈ā寮忕殑鐗规畩鐢ㄦ硶,鐢辨櫘閫氬瓧绗(渚嬪瀛楃a-z)浠ュ強鐗规畩瀛楃(绉颁负鍏冨瓧绗,濡/銆*銆?绛)缁勬垚...