目录
1.通过具体/模糊文件名找文件位置
find
1,找名为ump-api-error.log的文件在什么位置,通过-name指定文件名
[root@ump03 ~]# find / -name ump-api-error.log
/var/log/httpd/ump-api-error.log
2,找文件名前缀是ump-api-er的文件在什么位置,名字后面的我记不清了,用*匹配
[root@ump03 bin]# find / -name ump-api-er*
/var/log/httpd/ump-api-error.log-20200405
/var/log/httpd/ump-api-error.log-20200412
/var/log/httpd/ump-api-error.log
3,只记得文件名中有mp和-error,我想知道这个文件在什么位置
[root@ump03 bin]# find / -name *mp*-error*
/var/log/httpd/ump-api-error.log-20200405
/var/log/httpd/ump-api-error.log-20200412
/var/log/httpd/ump-api-error.log
/usr/lib/dracut/modules.d/99kdumpbase/kdump-error-handler.service
/usr/lib/dracut/modules.d/99kdumpbase/kdump-error-handler.sh
locate
locate命令其实是"find -name"的另一种写法,但是要比后者快得多,原因在于它不搜索具体目录,而是搜索一个数据库(/var/lib/locatedb),这个数据库中含有本地所有文件信息。Linux系统自动创建这个数据库,并且每天自动更新一次,所以使用locate命令查不到最新变动过的文件。为了避免这种情况,可以在使用locate之前,先使用updatedb命令,手动更新数据库。
locate命令的使用实例:
$ locate /etc/sh
搜索etc目录下所有以sh开头的文件。
$ locate ~/m
搜索用户主目录下,所有以m开头的文件。
$ locate -i ~/m
搜索用户主目录下,所有以m开头的文件,并且忽略大小写。
2 通过目录找含有指定内容的文件
1,查找当前目录下含有内容443的文件
[root@scrum-staging-4 harbor]# grep -r "443" ./
./common/templates/nginx/notary.upstream.conf: server notary-server:4443;
./common/templates/nginx/nginx.https.conf: listen 443 ssl;
./common/templates/nginx/notary.server.conf: listen 4443 ssl;
./common/templates/notary/server-config.json: "http_addr": ":4443"
./common/templates/notary/server-config.postgres.json: "http_addr": ":4443"
./common/config/adminserver/env:NOTARY_URL=http://notary-server:4443
./prepare:notary_url = "http://notary-server:4443"
./docker-compose.yml: - 443:443
./docker-compose.yml: - 4443:4443
3 通过文件找指定内容
1,在文件test中找含有内容hyzx的行
[root@ump03 ~]# cat test
demo for
two
hyzx-1
hyzx
hhh,hyzx
[root@ump03 ~]# grep hyzx ./test
hyzx-1
hyzx
hhh,hyzx
2,在文件test、test2中找含有内容hyzx的行(多个文件时后面空格分隔)
[root@ump03 ~]# cat test2
hyzx-2
hhh-hyzx
h
[root@ump03 ~]# grep hyzx ./test test2
./test:hyzx-1
./test:hyzx
./test:hhh,hyzx
test2:hyzx-2
test2:hhh-hyzx
3,找test文件中含有内容hyzx的行数
[root@ump03 ~]# grep -c hyzx ./test
3
4,查找docker-compose.yml文件中出现字符443的及其前(-B)、后(-A)、前后各5行(-C)的内容
[root@scrum-staging-4 harbor]# grep -C 5 "443" ./docker-compose.yml
networks:
- harbor
dns_search: .
ports:
- 8880:80
- 443:443
- 4443:4443
depends_on:
- postgresql
- registry
- core
- portal
[root@scrum-staging-4 harbor]# grep -A 5 "443" ./docker-compose.yml
- 443:443
- 4443:4443
depends_on:
- postgresql
- registry
- core
- portal
4 找某程序路径
whereis
whereis命令只能用于程序名的搜索,而且只搜索二进制文件(参数-b)、man说明文件(参数-m)和源代码文件(参数-s)。如果省略参数,则返回所有信息。
whereis命令的使用实例:
$ whereis grep
[root@localhost oa]# whereis mysql
mysql: /usr/lib64/mysql /usr/local/mysql /usr/share/mysql /usr/local/mysql/bin/mysql
which
which命令的作用是,在PATH变量指定的路径中,搜索某个系统命令的位置,并且返回第一个搜索结果。也就是说,使用which命令,就可以看到某个系统命令是否存在,以及执行的到底是哪一个位置的命令。
which命令的使用实例:
$ which grep
[root@localhost oa]# which php
/usr/local/php7/bin/php