Linux :: 文件查找指令【2】:find 指令(重点):用于在文件树中查找文件(指定路径/目录),并作出相应的处理(可能访问磁盘)【随知识体系持续更新】

前言:本篇是 Linux 基本操作篇章的内容!
笔者使用的环境是基于腾讯云服务器:CentOS 7.6 64bit。


学习集:


目录索引:
1. 基本语法格式和功能
2. 文件查找中的基本使用
- - 2.1 创建测试用例
- - 2.2 「-name」:按照文件名查找文件【重点】
- - 2.3 查找当前目录下指定文件/目录
- - 2.4 查找指定目录下指定文件/目录
3. 关于 find 的说明
4. 相关文章或系列推荐


1. 基本语法格式和功能

基本语法:find pathname [option]


功能: 用于在文件树种查找文件,并作出相应的处理(可能访问磁盘)


更新补充:find 指令常与 grep 指令 在面试中被提及,需让你回答异同!


2. 文件查找中的基本使用

2.1 创建测试用例

/* 指令测试准备 */
[Mortal@VM-12-16-centos test_findsome]$ mkdir -p a/aa
[Mortal@VM-12-16-centos test_findsome]$ mkdir -p a/bb
[Mortal@VM-12-16-centos test_findsome]$ mkdir -p a/cc
[Mortal@VM-12-16-centos test_findsome]$ mkdir -p b/aa
[Mortal@VM-12-16-centos test_findsome]$ touch a/aa/t1.txt
[Mortal@VM-12-16-centos test_findsome]$ touch a/aa/t2.txt
[Mortal@VM-12-16-centos test_findsome]$ touch a/cc/t2.txt
[Mortal@VM-12-16-centos test_findsome]$ touch b/aa/t2.txt
[Mortal@VM-12-16-centos test_findsome]$ tree .
.
|-- a
|   |-- aa
|   |   |-- t1.txt
|   |   `-- t2.txt
|   |-- bb
|   `-- cc
|       `-- t2.txt
`-- b
    `-- aa
        `-- t2.txt

6 directories, 4 files

2.2 「-name」:按照文件名查找文件【重点】

用法:find pathname -name filename

  • 即:find 指定路径 -name 指定文件名

说明:在第一次查询过程中可能速度慢,但后续再查速度会有提升!【可以理解为就是第一次在遍历式查找,过程中可能访问磁盘】

2.3 查找当前目录下指定文件/目录

查找当前目录下指定文件/目录

/* 查找当前目录下指定文件 */
[Mortal@VM-12-16-centos test_findsome]$ find . -name t2.txt
./b/aa/t2.txt
./a/cc/t2.txt
./a/aa/t2.txt

/* 查找当前目录下指定目录 */
[Mortal@VM-12-16-centos test_findsome]$ find . -name aa
./b/aa
./a/aa

2.4 查找指定目录下指定文件/目录

查找指定目录下指定文件/目录

[Mortal@VM-12-16-centos test_findsome]$ find ./a -name t2.txt
./a/cc/t2.txt
./a/aa/t2.txt

[Mortal@VM-12-16-centos test_findsome]$ find ./a -name t1.txt
./a/aa/t1.txt

/* 指定查找当前用户目录下的指定文件/目录 */
[Mortal@VM-12-16-centos test_findsome]$ ls ~
StudyingOrder_Linux  test1  test2  test3  test_cp  test_findsome  test_mkdir  test_mv  test_txtfile
[Mortal@VM-12-16-centos test_findsome]$ find ~ -name test_findsome
/home/Mortal/test_findsome

3. 关于 find 的说明

  • Linux 下 find 命令在目录结构中搜索文件,并执行指定的操作。
  • Linux下 find 命令提供了相当多的查找条件,功能很强大。由于 find 具有强大的功能,所以它的选项也很多,其中大部分选项都值得我们花时间来了解一下。(由于当前知识体系简单,未涉及如文件系统之类的知识,故:本篇内容将在持续学习中不断更新 find 的使用,当前只作简单查找认识!

以下说明供暂时打个照面即可!

  • 即使系统中含有网络文件系统( NFS),find 命令在该文件系统中同样有效,只你具有相应的权限。
  • 在运行一个非常消耗资源的 find 命令时,很多人都倾向于把它放在后台执行,因为遍历一个大的文件系统可能会花费很长的时间(这里是指30G字节以上的文件系统)。


4. 相关文章或系列推荐

1. Linux 学习目录合集


2. Linux :: 【基础指令篇 :: 查找 / 查询指令:(1)】:: which 指令 :指定系统文件(指令)查找指令 | 查询指令的别名
3. Linux :: 内容过滤指令【3】:grep 指令【详解】:在指定文件中过滤搜索信息、(模糊)查找包含指定字符串的内容!


猜你喜欢

转载自blog.csdn.net/weixin_53202576/article/details/131153981