Linux中根据文件夹/文件名查找其所在的磁盘位置(含find命令解析)

本文介绍了在Linux系统中如何使用find、grep、which和whereis命令查找文件和文件夹。通过find命令可以精确地定位文件类型、大小和路径,而grep命令则能帮助过滤文件内容。which和whereis命令则专注于查找可执行文件和手册页。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

背景

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命令的参数:

  1. path:要查找的目录路径。

    • ~ 表示$HOME目录
      * . 表示当前目录
      * / 表示根目录
  2. print:表示将结果输出到标准输出。

  3. exec:对匹配的文件执行该参数所给出的shell命令。
    形式为command {} ;,注意{}与;之间有空格

  4. ok:与exec作用相同,
    区别在于,在执行命令之前,都会给出提示,让用户确认是否执行

  5. |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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

秃秃爱健身

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值