find
用于在指定目录下查找文件和目录
Linux 中的文件没有创建时间这一说,只有访问时间(atime)、数据修改时间(mtime)、状态修改时间(ctime)
文件时间属性:
| 文件时间属性 | 说明 | 描述 | 查看 |
| mtime | 修改时间 | 文件内容最后一次修改的时间 | ls -l |
| atime | 访问时间 | 对文件进行一次读操作,例如 cat,more 等操作 | ls -lu |
| ctime | 状态时间 | 当文件的状态被改变的时候,状态时间就会随之改变。例如当使用 chmod、chown 等 | ls -lc |
示例:
| 实例 | 注释 |
|---|---|
| find /etc -name ".txt" -ls | 查找文件以列表的形式查看 |
| find /etc -iname ".txt" | 忽略大小写 |
| find /-inum 2621 | 按inode号查找 |
| find /etc -size +5M(-5 5) | 查找大于5M的文件 |
| find /etc -mtime +5(-5 5) | 查找过去5天内数据被修改过的文件 |
| find /etc -atime +5(-5 5) | 查找过去5天内被读取过的文件 |
| find /etc -ctmie +5(-5 5) | 查找过去5天内状态被修改过的文件 |
| find /etc -maxdepth 2 -a -name ".txt" | 按照目录深度查找文件 |
| find /etc -name ".txt" -o -name ".txt" | -o 或者 -a 并且 |
| find /etc ! -name ".txt" | 除.txt外的目录和文件 ! 取反 |
| find /etc -type f | 查找路径下的文件(c-字型装置文件,b-区块装置文件) |
| find /etc -type d | 查找路径下的目录(p-具名贮列,l-符号连结) |
| find . -perm 644 | 查找当前目录下权限为644的文件和目录 |
| find /etc -name ".txt*" -exec cp -rvf {} /tmp \; | *查找路径下文件并复制到路径下 |
| find /etc -name ".txt" |xargs rm -rf | 整体删除 |
| find / -name "*.txt" -mtime -10 -type f -print | 10天以内被修改过的文件(内容修改时间 ctime 元数据) |
| ls -t /tmp/xx | 查看修改文件的时间并从最新排序 |
| find / -user xxx | 查找属于用户xx的文件 |
补充
1 定时清理日志文件
编写脚本
#!/bin/bash # 设置要清理的日志文件路径 LOG_DIR="/path/to/log/directory/" # 设置要清理的日志文件后缀 LOG_SUFFIX=".log" # 遍历日志文件并删除带有指定后缀的文件 find "$LOG_DIR" -type f -name "*$LOG_SUFFIX" -exec rm -f {} \;
放入定时任务
crontab -l */5 * * * * sh /home/service/logs/log-clear.sh

浙公网安备 33010602011771号