find命令

find

find 查找位置 -条件 条件值 -exec 动作 {} \;
-name
这里写图片描述
这里写图片描述
find /mnt -name file1 ##查找/mnt名字是file1的文件
这里写图片描述
-not 条件
-user
find /mnt -user root ##查找/mnt 下用户是 root的文件和目录
这里写图片描述
-group
find /mnt -group root ##查找/mnt 下组是 root的文件
这里写图片描述
-size
-perm
–maxdepth
–mindepth
find /etc -maxdepth 1 -name *.conf ##查找/etc下名字 以.conf结尾的文件,最大查找深度为1层。
这里写图片描述
find /etc -maxdepth 2 -mindepth 2 -name *.conf ##查找/etc下名字 以.conf结尾的文件,查找深度为2层。
这里写图片描述
find /etc -maxdepth 3 -mindepth 2 -name *.conf ##查找/etc下名字 以.conf结尾的文件,最大查找深度为3层,最小深度为2层。
这里写图片描述

        -a
        -o
        -type   f   文件
            d   目录
            c   字符设备
            b   块设备
            s   套节字
            l   链接

find /mnt -group root -type d ##查找/mnt 组是root的目录
find /mnt -group root -type d -a -user root ##查找/mnt 目录下组和用户是root的目录
find /mnt -group root -type d -a -user student ##查找/mnt 目录下组是root和用户是student的目录
find /mnt -group root -type d -o -user student -type d ##查找/mnt 目录下组是root或者用户是student的目录
这里写图片描述
dd if=/dev/zero of=/mnt/file1 bs=1024 count=10 ##大小为10k的file1文件
dd if=/dev/zero of=/mnt/file2 bs=1024 count=20 ##大小为20k的file2文件
dd if=/dev/zero of=/mnt/file3 bs=1024 count=30 ##大小为30k的file3文件
这里写图片描述
find /mnt -size 20k ##查找/mnt目录下大小为20K的文件
find /mnt -size -20k ##查找/mnt目录下大小小于20K的文件
find /mnt -size +20k ##查找/mnt目录下大小大于20K的文件
这里写图片描述

### Linux `find` 命令使用指南 `find` 是一个强大的命令行工具,用于在目录层次结构中搜索文件和目录。它提供了丰富的选项来精确控制搜索行为,并支持基于多种条件(如名称、类型、大小、时间等)进行过滤。 #### 1. 基本语法 `find` 的基本语法如下: ```bash find [路径] [选项] [表达式] ``` - `[路径]`:指定搜索的起始目录。如果省略,则默认为当前目录。 - `[选项]`:控制搜索行为的参数。 - `[表达式]`:定义搜索条件的逻辑表达式。 #### 2. 常用选项 以下是一些常用的 `find` 命令选项及其功能: - **限制目录深度** 使用 `-maxdepth` 和 `-mindepth` 选项可以限制搜索的目录深度[^1]。例如,仅搜索当前目录而不进入子目录: ```bash find . -maxdepth 1 -name "*.txt" ``` - **跟随符号链接** 默认情况下,`find` 不会跟随符号链接。若需要强制跟随符号链接,可使用 `-L` 选项。但需注意,若遇到指向自身的符号链接,可能会导致死循环。 - **根据文件类型搜索** 可以通过 `-type` 选项指定文件类型[^1]。常见的文件类型包括: - `f`:普通文件 - `d`:目录 - `l`:符号链接 示例:查找所有普通文件: ```bash find /path/to/directory -type f ``` - **按名称搜索** 使用 `-name` 或 `-iname`(忽略大小写)选项可以根据文件名进行搜索。例如,查找扩展名为 `.log` 的文件: ```bash find /var -name "*.log" ``` - **按修改时间搜索** 可以使用 `-mtime`、`-atime` 和 `-ctime` 选项根据文件的时间戳进行搜索。例如,查找最近 7 天内修改过的文件: ```bash find /path/to/directory -mtime -7 ``` - **按大小搜索** 使用 `-size` 选项可以根据文件大小进行搜索。例如,查找大于 100MB 的文件: ```bash find /path/to/directory -size +100M ``` #### 3. 综合示例 以下是一些综合使用的示例: - 查找当前目录下所有普通文件并删除: ```bash find . -type f -exec rm -f {} \; ``` - 查找 `/home` 目录下所有空文件: ```bash find /home -type f -empty ``` - 查找 `/etc` 目录下所有最近 24 小时内访问过的配置文件: ```bash find /etc -type f -atime 0 ``` #### 4. 注意事项 - 使用 `-exec` 选项执行命令时,请确保命令的安全性,避免误删重要文件。 - 当搜索范围较大时,`find` 命令可能需要较长时间完成,建议结合 `-maxdepth` 限制搜索范围。 ```bash # 示例:查找当前目录下所有普通文件并打印其权限 find . -type f -exec ls -l {} \; ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值