Unix/Linux command(1):find 与 grep

find - search for files in a directory hierarchy
grep, egrep, fgrep, zgrep, zegrep, zfgrep - print lines that match patterns

此文与windows command(9):FIND FINDSTR对照。

查找字符串

windows中的find/findstr更改为grep即可,实现在单个文件中查找某个字符串。

1
2
$grep "hello there" hi.txt
hello there

查找的字符串中有"

由于grep的pattern是支持正则表达式regex的,所有只要用\"转义"即可。

1
2
$grep "\"hello" hi.txt
h"hello"h

多文件查找

1
grep "hello" *.txt

查找目录

1
2
3
grep -nir "music.163" ./ # 查找当前文件夹及其子目录下包含`music.163`字符串的文件,并打印行号

find . -name "file_name" | xargs grep -nir "search_string" # 过滤文件之后,对每个文件查找

显示行号

1
2
3
4
$grep -n "hello" hi.txt
1:hello
2:h"hello"h
3:hello there

通配符查找

1
2
3
4
$grep  '.*llo' hi.txt
hello
h"hello"h
hello there

find

常用option:
-name: name matching, can use regex.
-iname: like-name, but case insensitive.
-regex: regex匹配文件名(包含路径),egfind ./ -regex ".*/*hexo.py".
-type: file type.d is directory, f is file.

find-examples
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
find / \! -name "*.c" -print
Print out a list of all the files whose names do not end in .c.

find / -newer ttt -user wnj -print
Print out a list of all the files owned by user ``wnj'' that are
newer than the file ttt.

find / \! \( -newer ttt -user wnj \) -print
Print out a list of all the files which are not both newer than
ttt and owned by ``wnj''.

find / \( -newer ttt -or -user wnj \) -print
Print out a list of all the files that are either owned by
``wnj'' or that are newer than ttt.

find / -newerct '1 minute ago' -print
Print out a list of all the files whose inode change time is more
recent than the current time minus one minute.

find / -type f -exec echo {} \;
Use the echo(1) command to print out a list of all the files.

find -L /usr/ports/packages -type l -exec rm -- {} +
Delete all broken symbolic links in /usr/ports/packages.

find /usr/src -name CVS -prune -o -depth +6 -print
Find files and directories that are at least seven levels deep in
the working directory /usr/src.

find /usr/src -name CVS -prune -o -mindepth 7 -print
Is not equivalent to the previous example, since -prune is not
evaluated below level seven.

grep

常用option:
-i(--ignore-case): 忽略大小写
-r (--recursive): 递归查找子目录
-n (--line-number): 打印行号
-l (--files-with-match): 只打印匹配的文件名

grep-examples
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# To find all occurrences of the word `patricia' in a file:

$ grep 'patricia' myfile

# To find all occurrences of the pattern `.Pp' at the beginning of a line:

$ grep '^\.Pp' myfile

# The apostrophes ensure the entire expression is evaluated by grep instead
# of by the user's shell. The caret `^' matches the null string at the
# beginning of a line, and the `\' escapes the `.', which would otherwise
# match any character.

# To find all lines in a file which do not contain the words `foo' or
# `bar':

$ grep -v -e 'foo' -e 'bar' myfile

# A simple example of an extended regular expression:
$ egrep '19|20|25' calendar
# Peruses the file `calendar' looking for either 19, 20, or 25.

References:

  1. man findman grep
  2. man find
  3. find example
  4. man grep
  5. grep example
  6. linux find -regex 使用正则表达式