背景
Linux中查找文件位置的方式有很多种,我们主要介绍find命令、grep命令,另外捎带一提whereis命令、which命令。
一、查找命令(Find Command)
findcommand is very featureful command used with a lot of different options. More details about find command can be found from the following tutorial.
find命令是非常有特色的命令,它具有许多不同的选项。 可从以下教程中找到有关find命令的更多详细信息。
Linux使用示例查找命令
1)仅查找文件(Find Only Files)
We can search only files by providing file type as -type f. We will search files those named conf in this example. We will use the glob start and end of the search term in order to accept any prefix or postfix for the search term. So this will match conffff, myconf, myconfffff, myconfiguration.txt etc.
我们可以通过将文件类型设置为-type f来仅搜索文件。 在此示例中,我们将搜索名为conf文件。 我们将使用全球范围内搜索词的开头和结尾,以便接受搜索词的任何前缀或后缀。 因此,这将匹配conffff , myconf , myconfffff , myconfiguration.txt等。
# 在当前目录及子目录下查找名称中包含conf的文件
$ find . -type f -name "*conf*"
Alternatively, we can specify the path we want to search for the given file name. We will provide the path according to … In this example, we will search in the /etc path.
或者,我们可以指定要搜索给定文件名的路径。 我们将根据提供路径. 。 在此示例中,我们将在/opt路径中搜索。
# 在/opt目录中查找名称中包含conf的文件
$ find /opt -type f -name "*conf*"
2)仅查找文件夹(Find Only Folders)
We may need only to find the folder. We will specify the type like below a directory.
我们可能只需要找到该文件夹。 我们将在目录下指定类型。
# 在/opt目录中查找所有名称中包含home的文件夹
$ find /opt -type d -name "*home*"
find命令介绍
find命令用来在指定目录下查找文件。任何位于参数之前的字符串都将被视为欲查找的目录名。如果使用该命令时,不设置任何参数,则find命令将在当前目录下查找子目录与文件。并且将查找到的子目录和文件全部进行显示。
1)语法
find path -option [ -print ] [ -exec -ok |xargs |grep ] [ command {} \; ]
find命令的参数:
-
path:要查找的目录路径。
- ~ 表示$HOME目录
* . 表示当前目录
* / 表示根目录
- ~ 表示$HOME目录
-
print:表示将结果输出到标准输出。
-
exec:对匹配的文件执行该参数所给出的shell命令。
形式为command {} ;,注意{}与;之间有空格 -
ok:与exec作用相同,
区别在于,在执行命令之前,都会给出提示,让用户确认是否执行 -
|xargs 与exec作用相同 ,起承接作用
区别在于 |xargs 主要用于承接删除操作 ,而 -exec 都可用 如复制、移动、重命名等
- options :表示查找方式
2)常用选项(option)
-type c : 文件类型是 c 的文件。
- d: 目录
- f: 一般文件
- c: 字型装置文件
- b: 区块装置文件
- p: 具名贮列
- l: 符号连结
- s: socket
-maxdepth c:c表示递归查找文件时的最大层数
-mindepth c:c表示递归查找文件时的最小层数
-mount, -xdev : 只检查和指定目录在同一个文件系统下的文件,避免列出其它文件系统中的文件
-amin n : 在过去 n 分钟内被读取过
-anewer file : 比文件 file 更晚被读取过的文件
-atime n : 在过去n天内被读取过的文件
-cmin n : 在过去 n 分钟内被修改过
-cnewer file :比文件 file 更新的文件
-ctime n : 在过去n天内被修改过的文件
-empty : 空的文件-gid n or -group name : gid 是 n 或是 group 名称是 name
-ipath p, -path p : 路径名称符合 p 的文件,ipath 会忽略大小写
-name name, -iname name : 文件名称符合 name 的文件。iname 会忽略大小写
-size n : 文件大小 是 n 单位,b 代表 512 位元组的区块,c 表示字元数,k 表示 kilo bytes,w 是二个位元组。
二、Grep命令(Grep Command)
grep command mainly filters given text and files contents but we can use it to find files and folders. For more detail
grep命令主要过滤给定文本和文件内容,但是我们可以使用它来查找文件和文件夹。 欲了解更多信息
Linux Grep命令简介和示例
We can use ls command recursively and grep the files and folder we want to find. In this example, we will search for files and folders whose names contain backup .
我们可以递归使用ls命令,并grep我们要查找的文件和文件夹。 在此示例中,我们将搜索名称包含store文件和文件夹。
$ ls -R -l | grep store
三、相关其他命令
1)哪个命令(Which Command)
whichcommand is not an actual file and folder search. which command simply search current environment executable files. This is generally useful if we are looking for a command which is not included in PATH variable and can not use automatically.
which命令不是实际的文件和文件夹搜索。 which命令仅搜索当前环境的可执行文件。 如果我们要寻找一个不包含在PATH变量中并且不能自动使用的命令,这通常很有用。
$ which ls
2)Whereis命令 (Whereis Command)
whereis command is used to list given search term related binary, source, or man page files. In this example, we will search for ls binary and related man page files.
whereis命令用于列出与给定搜索词相关的二进制,源或手册页文件。 在此示例中,我们将搜索ls二进制文件和相关的手册页文件。
$ whereis ls