在Linux中快速找到文件的几种方法

本文主要用于介绍几种Linux常用的查找文件的方法

1. find

find命令是Linux在查找文件所在位置时最常使用的命令之一,一般是直接通过文件名进行查询所在位置。

1
[root@localhost ~]# find 搜索路径 [选项] 搜索内容

在进行搜查的时候,主要有以下三种选项,其中前两种是我们使用比较多的

1
2
3
-name: 按照文件名搜索;
-iname: 按照文件名搜索,不区分文件名大小;
-inum: 按照 inode 号搜索;

例如在当前目录下查找我所想要的application.js,,可以看到该文件是在js目录下,-iname选项的用法也是类似的

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
GENGKAIYU-MB0:public onlyone$ ls -rlt
total 32
drwxr-xr-x 3 onlyone staff 96 12 17 16:20 links
drwxr-xr-x 3 onlyone staff 96 12 17 16:20 about
drwxr-xr-x 3 onlyone staff 96 12 17 16:20 tags
-rw-r--r-- 1 onlyone staff 110 12 17 16:20 baidusitemap.xml
-rw-r--r-- 1 onlyone staff 1280 12 17 16:20 content.json
drwxr-xr-x 3 onlyone staff 96 12 17 16:20 categories
drwxr-xr-x 3 onlyone staff 96 12 17 16:20 repository
-rw-r--r-- 1 onlyone staff 898 12 17 16:20 sitemap.xml
drwxr-xr-x 5 onlyone staff 160 12 17 16:20 images
-rw-r--r-- 1 onlyone staff 3285 12 17 16:20 favicon.png
drwxr-xr-x 7 onlyone staff 224 12 17 16:20 fonts
drwxr-xr-x 9 onlyone staff 288 12 17 16:20 js
drwxr-xr-x 4 onlyone staff 128 12 17 16:20 css
GENGKAIYU-MB0:public onlyone$ find ./ -name application.js
.//js/application.js
GENGKAIYU-MB0:public onlyone$

但是find命令是必须要求完全匹配的,当你想要使用正则表达式的时候会出现如下错误(这个问题当文件与搜索目录在同一级时会出现)

1
2
GENGKAIYU-MB0:js onlyone$ find ./ -name app*
find: application.min.js: unknown primary or operator

在这种情况下可以通过在如下方法进行解决

1
2
3
4
5
6
7
8
9
10
//切换到上级目录 
GENGKAIYU-MB0:public onlyone$ find ./ -name app*
.//js/application.min.js
.//js/application.js

//在搜索内容前加上引号
GENGKAIYU-MB0:js onlyone$ find ./ -name 'app*'
.//application.min.js
.//application.js

2. which & whereis

这两个命令更多的倾向于一些可执行的命令或文件

which其实查找的是每个Linux系统Path目录下的可执行文件

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
//先查看我的PATH变量
GENGKAIYU-MB0:gblog onlyone$ echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/go/bin:/usr/local/go/bin:/Users/onlyone/Downloads/apache-maven-3.6.3/bin
GENGKAIYU-MB0:gblog onlyone$ ls -lrt /usr/local/bin/
total 8
lrwxr-xr-x 1 onlyone admin 28 3 31 2020 brew -> /usr/local/Homebrew/bin/brew
lrwxr-xr-x 1 onlyone admin 30 3 31 2020 telnet -> ../Cellar/telnet/63/bin/telnet
lrwxr-xr-x 1 onlyone admin 37 4 2 2020 corkscrew -> ../Cellar/corkscrew/2.0/bin/corkscrew
lrwxr-xr-x 1 onlyone admin 38 4 2 2020 pcre2-config -> ../Cellar/pcre2/10.34/bin/pcre2-config
lrwxr-xr-x 1 onlyone admin 35 4 2 2020 pcre2grep -> ../Cellar/pcre2/10.34/bin/pcre2grep
lrwxr-xr-x 1 onlyone admin 35 4 2 2020 pcre2test -> ../Cellar/pcre2/10.34/bin/pcre2test
lrwxr-xr-x 1 onlyone admin 28 4 2 2020 git -> ../Cellar/git/2.26.0/bin/git
lrwxr-xr-x 1 onlyone admin 38 4 2 2020 git-cvsserver -> ../Cellar/git/2.26.0/bin/git-cvsserver
lrwxr-xr-x 1 onlyone admin 41 4 2 2020 git-receive-pack -> ../Cellar/git/2.26.0/bin/git-receive-pack
lrwxr-xr-x 1 onlyone admin 34 4 2 2020 git-shell -> ../Cellar/git/2.26.0/bin/git-shell
lrwxr-xr-x 1 onlyone admin 43 4 2 2020 git-upload-archive -> ../Cellar/git/2.26.0/bin/git-upload-archive
lrwxr-xr-x 1 onlyone admin 40 4 2 2020 git-upload-pack -> ../Cellar/git/2.26.0/bin/git-upload-pack
-rwxr--r-- 1 onlyone staff 3754 4 2 2020 idea
lrwxr-xr-x 1 root admin 42 4 10 2020 bf -> /usr/local/bifrost/bin/bifrostClient.1.0.0
lrwxr-xr-x 1 root admin 41 4 10 2020 bfAgent -> /usr/local/bifrost/bin/bifrostAgent.1.0.0
lrwxr-xr-x 1 onlyone admin 31 12 14 17:31 node -> ../Cellar/node/13.12.0/bin/node
lrwxr-xr-x 1 onlyone admin 33 12 14 17:54 hexo -> ../lib/node_modules/hexo/bin/hexo
lrwxr-xr-x 1 onlyone admin 38 12 15 21:33 npm -> ../lib/node_modules/npm/bin/npm-cli.js
lrwxr-xr-x 1 onlyone admin 38 12 15 21:33 npx -> ../lib/node_modules/npm/bin/npx-cli.js
/如上 telnet的命令存储于/usr/local/bin/telnet目录下
GENGKAIYU-MB0:gblog onlyone$ which telnet
/usr/local/bin/telnet

whereis与which类似,只不过它的查找范围更广,不通过PATH变量进行查找,通过文件索引进行查找

1
2
3
4
5
[root@iz2ze0ldeq21cpwq3l04yzz ~]# whereis ls
ls: /usr/bin/ls /usr/share/man/man1/ls.1.gz
[root@iz2ze0ldeq21cpwq3l04yzz ~]# which ls
alias ls='ls --color=auto'
/usr/bin/ls