谷动谷力

 找回密码
 立即注册
查看: 1319|回复: 4
收起左侧

【命令用法】linux下如何快速查找文件 find grep which whereis ocate

[复制链接]
发表于 2022-1-13 17:59:09 | 显示全部楼层 |阅读模式
本帖最后由 sunsili 于 2023-6-2 13:47 编辑
( ]% B) X8 M, N- J. t
* k/ n0 ^% [4 U5 {& ]7 [【命令用法】linux下如何快速查找文件 find grep which whereis ocate
5 L8 q7 T% j+ v  L! G/ y
$ y5 ]* Y- }, y5 k) N. L/ c/ j; h6 W5 V) U; C
前言, ?, v4 K$ z7 L3 Q( ?" `
在使用linux时,经常需要进行文件查找。其中查找的命令主要有find grep which whereis ocate,但它们是有区别的。; i) v% M" O' U) `4 n
& ], ?4 e' o0 P; a" Z! ?
  它们的区别:                  (1)find命令是根据文件的属性进行查找,如文件名,文件大小,所有者,所属组,是否为空,访问时间,修改时间等。9 i) O' f6 Z& M- I
                  (2)grep是根据文件的内容进行查找,会对文件的每一行按照给定的模式(patter)进行匹配查找。
8 C+ z" ]2 E# n. a' i8 @8 X' x                  (3)which       查看可执行文件的位置 ,只有设置了环境变量的程序才可以用
; s$ z/ x# p0 F6 r4 O3 Y: D                  (4)whereis    寻找特定文件,只能用于查找二进制文件、源代码文件和man手册页
7 _( C2 |; }1 [                  (5)locate       配合数据库查看文件位置 ,详情:locate -h查看帮助信息
/ F4 {) M9 b: Z           ) y; ^; u: ~7 |8 G

# B9 L. W8 J) I/ q. ^9 Z( o( F一.find命令
* {  N. W) a4 \# U
# Q/ g) g0 s! ~" \    基本格式:find  path expressionfind --help
) V1 g+ e$ c& |6 ]" |% yUsage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]$ Z$ t) C& G8 f6 R  q
& T- s5 D  P: z, ]% y( D4 }
default path is the current directory; default expression is -print0 q# `% F4 s0 T/ l
expression may consist of: operators, options, tests, and actions:" P3 w0 `$ {# O4 W8 T6 |* {
* o: P# S; C! R; V; b# D
operators (decreasing precedence; -and is implicit where no others are given):) H3 d' S/ K8 [1 _
      ( EXPR )   ! EXPR   -not EXPR   EXPR1 -a EXPR2   EXPR1 -and EXPR2
# h# B4 K% D: e+ v. d$ p      EXPR1 -o EXPR2   EXPR1 -or EXPR2   EXPR1 , EXPR2
( D+ `/ n9 R" s+ ^. X1 _' j0 h# A* @2 A) `; v
positional options (always true): -daystart -follow -regextype2 `7 u* M2 ]4 i' J

& P. G! A5 r4 A0 p8 E+ ~8 Pnormal options (always true, specified before other expressions):5 M) N6 E) Q  d  H7 x& M
      -depth --help -maxdepth LEVELS -mindepth LEVELS -mount -noleaf& g& D2 Z, \( n8 I( k
      --version -xautofs -xdev -ignore_readdir_race -noignore_readdir_race9 r5 r/ X0 ^/ v, x1 Q

1 m8 e: ?0 t8 [  r( ktests (N can be +N or -N or N): -amin N -anewer FILE -atime N -cmin N- d9 s4 R7 ^( X$ E' h
      -cnewer FILE -ctime N -empty -false -fstype TYPE -gid N -group NAME
* ]5 f. T% e* k- _      -ilname PATTERN -iname PATTERN -inum N -iwholename PATTERN -iregex PATTERN5 `4 k6 L- Z% r
      -links N -lname PATTERN -mmin N -mtime N -name PATTERN -newer FILE
4 B5 ^5 b2 u' f      -nouser -nogroup -path PATTERN -perm [-/]MODE -regex PATTERN
+ p# U3 W# l$ {% u  X      -readable -writable -executable0 g2 l$ Y5 u0 u( @( d( B, E$ _( B
      -wholename PATTERN -size N[bcwkMG] -true -type [bcdpflsD] -uid N
& ^3 L7 t- k/ h. f6 S; V( u      -used N -user NAME -xtype [bcdpfls]
2 N2 [7 ~. u9 I  A" h1 d9 d( J" D      -context CONTEXT! j7 e4 h2 \& N1 ^

1 H) s; d& J; x* W( n7 t$ |& V+ W  Y' n# j% u  z
actions: -delete -print0 -printf FORMAT -fprintf FILE FORMAT -print$ B1 H# P- _0 {/ Z% Z# F
      -fprint0 FILE -fprint FILE -ls -fls FILE -prune -quit1 J, O& s3 K. x/ K  @) V% }$ g
      -exec COMMAND ; -exec COMMAND {} + -ok COMMAND ;7 b" {# |9 [1 h/ c9 \
      -execdir COMMAND ; -execdir COMMAND {} + -okdir COMMAND ;
. t+ A) n+ N5 E- ]9 `* E% W/ z' i; M! i* q. M' @
Report (and track progress on fixing) bugs via the findutils bug-reporting
6 ^; W  x$ G4 L% I5 b4 Q# bpage at http://savannah.gnu.org/ or, if you have no web access, by sending* _4 [' T4 B& {, E
email to <bug-findutils@gnu.org>.
8 |( }, v- y! I1 Z- J/ j2 N
' c9 u2 u" G2 W3 Y- Q+ a5 o! a! s& E: E$ H9 Z. i
    1.按照文件名查找
& P( k9 [8 [: K" _% \* g  p* x) h) R" ~6 G* |2 ?# \  h" m( ~# O
    (1)find / -name httpd.conf  #在根目录下查找文件httpd.conf,表示在整个硬盘查找1 i% h8 f: s, @- u  n2 w0 W
    (2)find /etc -name httpd.conf  #在/etc目录下文件httpd.conf! T9 e9 _8 H! e+ W& n6 w
    (3)find /etc -name '*srm*'  #使用通配符*(0或者任意多个)。表示在/etc目录下查找文件名中含有字符串‘srm’的文件. ]% {' A! G: x6 Q1 P$ e7 i: h0 ]
    (4)find . -name 'srm*'   #表示当前目录下查找文件名开头是字符串‘srm’的文件9 m, ~* ]0 L" T; `0 Y" o

8 ?+ T$ y3 c* N: y    2.按照文件特征查找     
3 J& F' b5 q- ^  M. Y! f0 I2 z; ?/ K, w
    (1)find / -amin -10   # 查找在系统中最后10分钟访问的文件(access time)2 U. s9 N  P: j; r3 ^
    (2)find / -atime -2   # 查找在系统中最后48小时访问的文件
( l; Q) K7 g- F" ?  R5 Q    (3)find / -empty   # 查找在系统中为空的文件或者文件夹
0 N! I6 M2 J$ s1 u    (4)find / -group cat   # 查找在系统中属于 group为cat的文件% c9 ~! S0 K) {) ?) _: G
    (5)find / -mmin -5   # 查找在系统中最后5分钟里修改过的文件(modify time)
6 c% G0 ^9 N) v9 s* g, P; Z    (6)find / -mtime -1   #查找在系统中最后24小时里修改过的文件
8 H( U) |1 V: z7 v- t5 h    (7)find / -user fred   #查找在系统中属于fred这个用户的文件$ M, i9 H8 R' g; d! e
    (8)find / -size +10000c  #查找出大于10000000字节的文件(c:字节,w:双字,k:KB,M:MB,G:GB)( M+ v9 i& J8 v# C
    (9)find / -size -1000k   #查找出小于1000KB的文件
* N! L4 S3 J* i' A/ {- \. B8 Z. H' p3 g/ R
    3.使用混合查找方式查找文件
4 T2 j6 s/ _  }% f* A" S# j% v0 J2 f9 P& C! o
    参数有: !,-and(-a),-or(-o)。2 a: m8 [2 c) A# D

: g$ t9 c% d& n    (1)find /tmp -size +10000c -and -mtime +2   #在/tmp目录下查找大于10000字节并在最后2分钟内修改的文件( [% G1 U- h( L+ k8 ]
         (2)find / -user fred -or -user george   #在/目录下查找用户是fred或者george的文件文件- A) K! Y. P6 \" h; \2 }. X
         (3)find /tmp ! -user panda  #在/tmp目录中查找所有不属于panda用户的文件7 m2 F; t. J1 a3 A3 p
        
$ R$ \/ W4 ?. L# Y7 g4 S: a5 O5 }& f* Y' q' b0 a5 ]
  二、grep命令
3 a4 ]; u8 D5 V/ i6 h3 Z2 x' P  @6 i/ T4 `& c4 C+ c
     基本格式:grep expressiongrep --help
4 P2 X" G/ W) CUsage: grep [OPTION]... PATTERN [FILE]...4 V$ d; z& ]* x; g5 e6 [
Search for PATTERN in each FILE.
7 J4 u9 U9 K' UExample: grep -i 'hello world' menu.h main.c" t! t, ^7 x6 P" R4 i" O9 x

/ t  J( Z# O& BPattern selection and interpretation:+ g. K3 p$ O2 ~/ O4 k
  -E, --extended-regexp     PATTERN is an extended regular expression
2 z! S5 v3 |/ H) B  -F, --fixed-strings       PATTERN is a set of newline-separated strings. }/ W$ q+ Q8 z5 ^2 j5 }
  -G, --basic-regexp        PATTERN is a basic regular expression (default)3 a6 U: s9 R) M* Y. T* P
  -P, --perl-regexp         PATTERN is a Perl regular expression
+ h9 \" h- k1 ^- Q5 N3 }3 m  -e, --regexp=PATTERN      use PATTERN for matching
- z1 U: [3 q8 @: u  -f, --file=FILE           obtain PATTERN from FILE
& H/ {- ?8 n( e9 G4 N  -i, --ignore-case         ignore case distinctions
$ G  C6 m2 P! ?/ d  -w, --word-regexp         force PATTERN to match only whole words% R6 N$ g; a" @' A4 u. u: y
  -x, --line-regexp         force PATTERN to match only whole lines
0 C  Y* y! S) M0 |' G( y  -z, --null-data           a data line ends in 0 byte, not newline
1 X- {1 m# e8 p/ z) Z0 q
: i# R& E& A9 Y# Z$ nMiscellaneous:, C' T9 ]- Q2 s8 @; I
  -s, --no-messages         suppress error messages8 s& K# O: N8 u
  -v, --invert-match        select non-matching lines
- z3 ^6 K2 p1 F7 v3 I2 e% s$ c  -V, --version             display version information and exit
9 i, ~) \& S4 G- H- T  y      --help                display this help text and exit. R: M* [. C4 v) f: n1 V6 y4 }9 `

9 p2 U( c, v  `8 @1 ?3 t. U: G$ |Output control:+ b& r4 M" J; w# n& o0 j
  -m, --max-count=NUM       stop after NUM selected lines- o$ W: @: c1 b& e
  -b, --byte-offset         print the byte offset with output lines5 Z' w0 r5 B. w, W8 v
  -n, --line-number         print line number with output lines6 @# m: X: a. b: n' |
      --line-buffered       flush output on every line
" M2 i% [9 Q; V$ z  -H, --with-filename       print file name with output lines
( C* D' I  B8 L0 X  -h, --no-filename         suppress the file name prefix on output- g! q0 y7 G( o* V! d7 E
      --label=LABEL         use LABEL as the standard input file name prefix
1 @% J3 F0 i0 K+ n6 q  -o, --only-matching       show only the part of a line matching PATTERN
& C  a) _2 X/ ~, ?  -q, --quiet, --silent     suppress all normal output
8 C8 _& ^+ G9 o& N' i' w      --binary-files=TYPE   assume that binary files are TYPE;9 Z$ F, I: q5 F& b+ W3 d
                            TYPE is 'binary', 'text', or 'without-match'6 I: a( N; X) V9 V  Q0 x
  -a, --text                equivalent to --binary-files=text
/ \* _2 V( m1 R# B( s1 t0 j! l  -I                        equivalent to --binary-files=without-match- u) o5 Q0 _9 s+ W! V1 v, F
  -d, --directories=ACTION  how to handle directories;
5 R( K+ I5 P; a                            ACTION is 'read', 'recurse', or 'skip'
1 Y" R: q: T- W! }  -D, --devices=ACTION      how to handle devices, FIFOs and sockets;7 A( H+ F8 m) l7 {7 }
                            ACTION is 'read' or 'skip'
  z) L- }3 Y! z( @2 Y  -r, --recursive           like --directories=recurse
8 t7 W& Q+ i, w$ h, \$ Y6 F  -R, --dereference-recursive
4 _- j4 _$ H6 L0 u+ m4 v                            likewise, but follow all symlinks. z$ K$ f4 l. h# Y. F# G
      --include=FILE_PATTERN$ Q) F  d3 {  y# U' c( o+ {6 l
                            search only files that match FILE_PATTERN1 r7 A& j- y! G7 U
      --exclude=FILE_PATTERN
. q7 ~  J/ E: \" ~" H& x; ]                            skip files and directories matching FILE_PATTERN
1 I0 u* v3 D* {" K' X9 V) x      --exclude-from=FILE   skip files matching any file pattern from FILE! s( Y: ^( p5 A7 c
      --exclude-dir=PATTERN directories that match PATTERN will be skipped.
, s/ B5 @0 @3 o- l  -L, --files-without-match print only names of FILEs with no selected lines5 {, c- \+ c7 z6 O/ P2 k3 r+ T" _
  -l, --files-with-matches  print only names of FILEs with selected lines9 I2 ~- n1 Z1 M: E! P
  -c, --count               print only a count of selected lines per FILE
0 e: F, ^8 F$ {( K* c6 @! ]  -T, --initial-tab         make tabs line up (if needed)
0 S$ s8 d% Q+ @8 B1 u  -Z, --null                print 0 byte after FILE name
# m8 e; T! J5 S6 G9 P  n. S
. }+ g  m" B5 W6 X) EContext control:
5 L/ d6 {# Z% E  -B, --before-context=NUM  print NUM lines of leading context
- M6 T/ a0 l' z- S* A0 p; b" d  -A, --after-context=NUM   print NUM lines of trailing context% G$ _. S! \( e& i! i1 U: }* c
  -C, --context=NUM         print NUM lines of output context
, E/ _8 o5 h) Y) n3 W) m  -NUM                      same as --context=NUM
% f. l! Q! e- ]1 m; G8 g      --group-separator=SEP use SEP as a group separator
7 Z* Q3 L9 D+ q% ]3 {' a      --no-group-separator  use empty string as a group separator
% @& T6 P6 x0 f$ f  H- f      --color[=WHEN],0 u9 V- ~. ~1 H( X. g1 @
      --colour[=WHEN]       use markers to highlight the matching strings;
3 \8 s' u: n7 a9 q- e* C. ?$ G                            WHEN is 'always', 'never', or 'auto'! I8 P' }3 V/ L2 E' O6 o
  -U, --binary              do not strip CR characters at EOL (MSDOS/Windows)  R; ~, o" g8 c8 Q: ~: A+ s8 q2 K

( u% c- r: a1 }! B) SWhen FILE is '-', read standard input.  With no FILE, read '.' if& ~3 m0 l( u2 c; j$ S
recursive, '-' otherwise.  With fewer than two FILEs, assume -h.
7 A/ {6 U5 W& d( _: t- sExit status is 0 if any line is selected, 1 otherwise;
9 y7 W; O1 K" x+ Nif any error occurs and -q is not given, the exit status is 2.' r: s4 w' o2 P1 }9 [! Q" V

8 r  Z* Z. ?& b4 W( Q
' y- `5 F5 I" Y0 \5 G# F7 W     1.主要参数; d/ u! M: ?* |  i

( u6 [" y9 J* ], e! r! d    [options]主要参数:; B2 T: _& f9 @' C+ L" Q/ J7 h* _! k- _
    -c:只输出匹配行的计数。) D1 ~3 c* S4 I$ |
    -i:不区分大小写8 W' ~) i/ c. u5 U. }% {( S+ X
    -h:查询多文件时不显示文件名。7 u4 c( |  O: j0 [+ c7 s# k
    -l:查询多文件时只输出包含匹配字符的文件名。# [6 b, s6 l# {  t. |+ e- O5 S
    -n:显示匹配行及行号。) n% p, f/ E+ r
    -s:不显示不存在或无匹配文本的错误信息。+ B, u# W4 Y$ }1 r+ w; q! s6 z6 H5 [
    -v:显示不包含匹配文本的所有行。" Y* I) H5 B$ o& y. p4 c- J

, L5 n6 S9 i; R7 }  d    pattern正则表达式主要参数:9 Z. i6 X2 j! ~6 e
    \: 忽略正则表达式中特殊字符的原有含义。
4 b0 V5 r& @# Z/ B: y    ^:匹配正则表达式的开始行。; e+ u3 S2 d/ O; N7 Q# s
    $: 匹配正则表达式的结束行。  i, M8 ~2 s; q
    \<:从匹配正则表达 式的行开始。: j8 }  H$ S. p5 ?" g* @7 J% n9 S
    \>:到匹配正则表达式的行结束。
+ o+ s0 U/ F: F& G    [ ]:单个字符,如[A]即A符合要求 。
; x. q5 l+ D: I: z3 K, N6 n    [ - ]:范围,如[A-Z],即A、B、C一直到Z都符合要求 。
( I) a% o7 [. T2 J1 [+ Z    .:所有的单个字符。+ X4 }" s# x5 q0 {' [6 s: U
    * :有字符,长度可以为0。0 V2 j  M& c' c! d. c8 f
, N9 E0 z! C1 r* Y: j/ D$ M# ?7 k1 R3 t
    2.实例  * @) F# H) j( ~7 Q9 ^

7 b5 G7 A) \4 t1 U/ g2 J        grep -r "字符串"  很方便
7 H* |, G2 T1 m- s* W
- `6 Z% U( _" t4 Y/ q7 C( \9 R  (1)grep 'test' d*  #显示所有以d开头的文件中包含 test的行
3 @2 o9 q- p. E+ I  (2)grep ‘test’ aa bb cc    #显示在aa,bb,cc文件中包含test的行
0 R$ j4 _% J, Y  (3)grep ‘[a-z]\{5\}’ aa   #显示所有包含每行字符串至少有5个连续小写字符的字符串的行
; [4 O7 V  O1 D% I6 @  (4)grep magic /usr/src  #显示/usr/src目录下的文件(不含子目录)包含magic的行
, D! p' F% C( W: i  f4 i  (5)grep -r magic /usr/src  #显示/usr/src目录下的文件(包含子目录)包含magic的行
- R2 n5 Y6 C! H2 c+ f  (6)grep -w pattern files :#只匹配整个单词,而不是字符串的一部分(如匹配’magic’,而不是’magical’),) ?4 y( V2 ]0 Y1 C
) d9 e! l' i+ ]- z  X9 |
参考链接:https://blog.csdn.net/xxmonstor/article/details/80507769
, s. Z+ U8 b8 Y% `: V. [5 s1 G' ^" d
/ A" l& c% j. s# W! W0 K6 P* W
回复

使用道具 举报

 楼主| 发表于 2023-6-2 11:01:20 | 显示全部楼层
cat log4js.2023-06-02-10.log | grep "cmd\":120"  //查找 cmd":120  \" 转译
/ W; X8 F+ r0 C5 H0 t% y7 `9 {; v* H; @- L. h
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-6-26 14:24:06 | 显示全部楼层
查找所有文件名含“bbs.sunsili.com”的文件& p8 u- I% E  x( G! r* O, [
find / * | grep "bbs.sunsili.com"
# |% A: E: F: o7 I+ v2 g& ]4 e4 Z
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-7-12 15:10:39 | 显示全部楼层
查找所有内部含“bbs.sunsili.com”的文件
! \/ n+ ]' D% z; \8 N( I# g9 _grep -rn "bbs.sunsili.com" /
回复 支持 反对

使用道具 举报

发表于 2023-9-22 12:01:26 | 显示全部楼层
查找内容包含bbs.sunsili.com文件
  b6 K0 z$ {2 k, K# G- J- Wgrep -rl "bbs.sunsili.com" ./* M+ L' l; b" }; n0 c7 w- N
( `# H/ B* I% P0 p2 o' G3 o

$ G+ ]5 d$ @2 K! x$ F4 w, L
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|深圳市光明谷科技有限公司|光明谷商城|Sunshine Silicon Corpporation ( 粤ICP备14060730号|Sitemap

GMT+8, 2024-3-29 15:23 , Processed in 0.093320 second(s), 40 queries .

Powered by Discuz! X3.2 Licensed

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表