学习find

本文详细介绍了 Linux 下 find 命令的使用方法及其常用选项,包括按时间筛选文件的功能,例如根据访问时间和状态改变时间来查找文件。此外还讲解了如何使用-depth选项实现目录的深度遍历。

find是linux系统下一个常用的命令。

用户命令:

 

expression      The first argument that starts with a -,  or
                     is  a ! or a (, and all subsequent arguments
                     are interpreted as an expression made up  of
                     the  following  primaries  and operators. In
                     the descriptions, wherever n is  used  as  a
                     primary  argument,  it  is  interpreted as a
                     decimal integer  optionally  preceded  by  a
                     plus (+) or minus (-) sign, as follows:

                     +n       more than n

                     n        exactly n

                     -n       less than n

 

以一个减号(-)、或者(!)或者(()打头的第一个参数和所有后续的参数一起被解析为一个表达式

 

     -atimen         True if the file was accessed  n  days  ago.
                     The  access  time  of directories in path is
                     changed by find itself.

如果文件在n天之前被访问。在path中的目录的访问时间将会被find更新

 

     -ctime n        True if the file's status was changed n days
                     ago.
如果文件在n天之前被修改

 

     -depth          Always true. Causes descent of the directory
                     hierarchy  to be done so that all entries in
                     a directory are acted on before  the  direc-
                     tory  itself.   This can be useful when find
                     is used with cpio(1) to transfer files  that
总是为true。执行深度优先原则。

`find` 是 Linux/Unix 系统中一个非常强大且常用的命令行工具,用于在文件系统中搜索文件和目录。它可以基于多种条件(如名称、类型、大小、时间等)进行搜索。 ### 基本语法 ```bash find [搜索路径] [选项] [操作] ``` ### 常用示例 1. **按名称搜索** ```bash find /path/to/search -name "filename.txt" ``` 在指定路径下搜索名为 `filename.txt` 的文件。 2. **忽略大小写** ```bash find /path/to/search -iname "*.txt" ``` 搜索所有扩展名为 `.txt` 的文件,忽略大小写。 3. **按类型搜索** ```bash find /path/to/search -type d # 搜索目录 find /path/to/search -type f # 搜索普通文件 ``` 4. **按大小搜索** ```bash find /path/to/search -size +10M # 搜索大于 10MB 的文件 find /path/to/search -size -1k # 搜索小于 1KB 的文件 ``` 5. **按时间搜索** ```bash find /path/to/search -mtime -7 # 搜索 7 天内修改过的文件 find /path/to/search -atime +30 # 搜索 30 天前访问过的文件 ``` 6. **组合条件** ```bash find /path/to/search -name "*.log" -size +1M -mtime +30 ``` 搜索扩展名为 `.log`、大小大于 1MB 且修改时间超过 30 天的文件。 7. **执行操作** ```bash find /path/to/search -name "*.tmp" -delete # 删除所有 .tmp 文件 find /path/to/search -name "*.sh" -exec chmod +x {} \; # 给所有 .sh 文件添加可执行权限 ``` ### 注意事项 - `find` 默认递归搜索子目录。 - 使用 `-exec` 或 `-delete` 等操作时要小心,避免误删或误操作。 - 如果路径中包含空格或特殊字符,建议用引号括起来。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值