IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 系统运维 -> Linux中根据文件夹/文件名查找其所在的磁盘位置(含find命令解析) -> 正文阅读

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

背景

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
  系统运维 最新文章
配置小型公司网络WLAN基本业务(AC通过三层
如何在交付运维过程中建立风险底线意识,提
快速传输大文件,怎么通过网络传大文件给对
从游戏服务端角度分析移动同步(状态同步)
MySQL使用MyCat实现分库分表
如何用DWDM射频光纤技术实现200公里外的站点
国内顺畅下载k8s.gcr.io的镜像
自动化测试appium
ctfshow ssrf
Linux操作系统学习之实用指令(Centos7/8均
上一篇文章      下一篇文章      查看所有文章
加:2021-08-05 17:44:38  更:2021-08-05 17:46:16 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/13 17:14:51-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码