基础命令(二)-文件搜索查询

304 阅读5分钟

grep

grep命令用于在指定文件中查找字符

命令格式:grep [options] [搜索字符] [文件名称]

可选选项作用
-b将可执行的二进制文件当做文本文件来搜索
-c仅显示找到的次数
-i搜索过程忽略大小写
-n显示行号
-v反选——搜索非关键字的行
示例:
[root@localtk ~]# ls
anaconda-ks.cfg  find_res.txt  test.txt

[root@localtk ~]# cat test.txt 
jksjahkhr
hello
hadoop spark java
python
rust
json
js
json
go
golang
c++
c
c#
d++
aq2l
kljljal
lop

# 搜索a字符并且显示行号
[root@localtk ~]# grep -n  a test.txt 
1:jksjahkhr
3:hadoop spark java
10:golang
15:aq2l
16:kljljal

# 搜索非a字符并且显示行号
[root@localtk ~]# grep -nv  a test.txt 
2:hello
4:python
5:rust
6:json
7:js
8:json
9:go
11:c++
12:c
13:c#
14:d++
17:lop

find

find命令用于在文件系统中查找文件

详细说明见find --help

命令格式(常用):

find [查找路径] [寻找条件] [操作]

命令格式(完整):

find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]

下面的参数解释针对的是[寻找条件]或者[expressions]

常用参数

参数作用
-name匹配名称
-perm匹配权限(mode为完全匹配,-mode为包含)
-user匹配所有者
-group匹配所有组
-mtime -n +n匹配修改内容的时间(-n表示n天以内,+n表示n天以前)
-atime -n +n匹配访问文件的时间(-n表示n天以内,+n表示n天以前)
-ctime -n +n匹配修改权限的时间(-n表示n天以内,+n表示n天以前)
-nouser匹配无所有者的文件
-nogroup匹配无所有组的文件
-type b/d/c/p/l/f匹配文件类型,块设备、目录、字符设备、管道、链接文件、文件
-prune忽略某个目录
-exec {} ;对搜索到的结果进一步的处理
# 1.查找当前目录下以g开头的文件(名称匹配)
[root@localtk Tools]# ls
buildbot  ccbench  c-globals  clinic  demo  freeze  gdb  i18n  importbench  iobench  msi  nuget  parser  pynche  README  scripts  ssl  stringbench  test2to3  tz  unicode  unittestgui

[root@localtk Tools]# find . -name g*
./gdb

# 2.匹配文件大小, 查找当前路径下大于50K的文件
[root@localtk Tools]# ll
总用量 20
drwxr-xr-x.  2 501 501   76 12月 19 2019 buildbot
drwxr-xr-x.  2 501 501   24 12月 19 2019 ccbench
drwxr-xr-x.  2 501 501   73 12月 19 2019 c-globals
drwxr-xr-x.  2 501 501   37 11月  8 15:38 clinic
drwxr-xr-x.  2 501 501  237 12月 19 2019 demo
drwxr-xr-x.  3 501 501 4096 12月 19 2019 freeze
drwxr-xr-x.  2 501 501   26 12月 19 2019 gdb
drwxr-xr-x.  2 501 501   69 12月 19 2019 i18n
drwxr-xr-x.  2 501 501   42 12月 19 2019 importbench
drwxr-xr-x.  2 501 501   24 12月 19 2019 iobench
drwxr-xr-x. 15 501 501 4096 12月 19 2019 msi
drwxr-xr-x.  2 501 501  149 12月 19 2019 nuget
drwxr-xr-x.  2 501 501   24 11月  8 15:38 parser
drwxr-xr-x.  3 501 501 4096 12月 19 2019 pynche
-rw-r--r--.  1 501 501 1826 12月 19 2019 README
drwxr-xr-x.  2 501 501 4096 11月  8 15:38 scripts
drwxr-xr-x.  2 501 501   54 12月 19 2019 ssl
drwxr-xr-x.  2 501 501   42 12月 19 2019 stringbench
drwxr-xr-x.  4 501 501   83 12月 19 2019 test2to3
drwxr-xr-x.  2 501 501   22 12月 19 2019 tz
drwxr-xr-x.  3 501 501  227 12月 19 2019 unicode
drwxr-xr-x.  2 501 501   46 12月 19 2019 unittestgui

[root@localtk Tools]# find . -size +50
./demo/ss1.py
./stringbench/stringbench.py
./unicode/makeunicodedata.py
./msi/bundle/bootstrap/PythonBootstrapperApplication.cpp
./msi/bundle/SideBar.png
./clinic/clinic.py
./scripts/texi2html.py
./gdb/libpython.py


# 3.查找当前目录下所有的文件, 可以发现当前目录下有三个是目录, 直接进行文件查找, 会进行递归查找
[root@localtk home]# ll
总用量 8
drwxr-xr-x. 4 root root  33 11月 22 20:37 data
-rw-r--r--. 1 root root 536 11月 22 20:36 Dockerfile
lrwxrwxrwx. 1 root root  19 11月 22 21:32 libstdc++.so.6 -> libstdc++.so.6.0.19
-rw-r--r--. 1 root root 181 11月 22 20:36 main-roadskim.sh
drwxr-xr-x. 2 root root  24 11月 23 11:37 output
drwxr-xr-x. 4 root root  56 11月 22 20:36 SZProj_Linux

# 会找到很多文件, 如果我的需求是仅仅查找当前目录下的文件, 不进行递归查找怎么办?使用-prune
[root@localtk home]# find . -type f
./SZProj_Linux/.vscode/settings.json
./SZProj_Linux/build/.cmake/api/v1/query/client-vscode/query.json
./SZProj_Linux/build/.cmake/api/v1/reply/cache-v2-8ddf83426c99281de516.json
./SZProj_Linux/build/.cmake/api/v1/reply/codemodel-v2-33311ee57467de104087.json
./SZProj_Linux/build/.cmake/api/v1/reply/index-2021-10-21T13-12-24-0445.json
./SZProj_Linux/build/.cmake/api/v1/reply/target-Continuous-Debug-fbf1e219e8723c3fb9a6.json
./SZProj_Linux/build/.cmake/api/v1/reply/target-ContinuousBuild-Debug-d9204eb64e910008eb69.json
./SZProj_Linux/build/.cmake/api/v1/reply/target-ContinuousConfigure-Debug-38867601ec488f1845cd.json
./SZProj_Linux/build/.cmake/api/v1/reply/target-ContinuousCoverage-Debug-92d29a15d11ff02f0f12.json
./SZProj_Linux/build/.cmake/api/v1/reply/target-ContinuousMemCheck-Debug-302584e19f269dfb64d2.json
./SZProj_Linux/build/.cmake/api/v1/reply/target-ContinuousStart-Debug-3b89a571e0f0b4a16902.json
./SZProj_Linux/build/.cmake/api/v1/reply/target-ContinuousSubmit-Debug-5ae02209cb577210abbe.json
.......
...

# 避开SZProj_Linux目录
# 用法: find 查找文件的目录 -path 需要排除的目录 -prune -o [expression]
[root@localtk home]# find . -path ./SZProj_Linux -prune -o -type f
./SZProj_Linux
./Dockerfile
./main-roadskim.sh
./data/input/conf.csv
./data/input/link.csv
./data/input/node.csv
./output/result.csv

# 避开多个目录怎么做?
[root@localtk home]# find ./ \( -path "./data" -o -path "./SZProj_Linux"  \) -prune -o -type f
./SZProj_Linux
./Dockerfile
./main-roadskim.sh
./data
./output/result.csv

# 4.筛选出.sh文件复制到另一个目录
[root@localtk home]# ls
data  Dockerfile  libstdc++.so.6  main-roadskim.sh  output  SZProj_Linux  test

# -exec参数后面跟的是command命令,它是以 ; 为结束标志,由于各个系统中分号会有不同的意义,因此在前面加上反斜杠\,{} 代表前面find查找出来的文件名
[root@localtk home]# find ./ -name main*.sh -exec cp -arf {} ./test/ \;
cp: "./test/main-roadskim.sh""./test/main-roadskim.sh" 为同一文件