find命令使用

本文详细介绍了Linux中find命令的使用,包括根据类型、名称和正则表达式查找文件和目录。通过示例展示了如何查找当前目录下的文件、执行指定操作如打印和删除,并解释了命令的执行模式。此外,还提供了实际操作的例子来帮助理解。

1 find . -type d:查找当前目录下的目录(.代表当前目录,d代表目录,若是f则代表文件)
2 find . -name ‘‘a1.txt’’:精确匹配当前目录名字为a1.txt的文件(注意,如果文件名包含!等shell里的变量名,就必须使用单引号)
find . -name " *a1* ":(模糊查找)查找名字中带有a1的文件或目录
find . -name " *a1 ":(模糊查找)查找名字以a1结尾的文件或目录
find . -name " a1* ":(模糊查找)查找名字以a1开头的文件/目录
3find . -regex .*.txt:查找名字符合正则表达式的文件/目录,注意前面的.*(这在正则表达式中表示任意多个字符)

find命令执行模式:

例一:
find . -type d -print -exec ls {} ;:找到当前文件下的所有文件/目录及文件/目录的级联文件/目录
这句话的执行顺序是,先查找本目录的目录/文件,然后执行-exec ls {} \命令,这个命令的执行原理是先对当前目录执行ls然后输出,再对该目录下的所有目录执行ls,注意ls 和{} 以及\之间必须有空格,最后还必须加;
举例如下:
[root@oracle test]# ll
total 8
-rw-r–r–. 1 root root 0 Sep 24 16:17 1
-rw-r–r–. 1 root root 45 Sep 24 15:57 1.txt
-rw-r–r–. 1 root root 9 Sep 24 15:58 2.txt
-rw-r–r–. 1 root root 0 Sep 24 15:59 3.txt
-rw-r–r–. 1 root root 0 Sep 24 16:17 abc1
drwxr-xr-x. 2 root root 21 Sep 24 17:35 test1
drwxr-xr-x. 2 root root 6 Sep 24 16:00 test2
drwxr-xr-x. 2 root root 6 Sep 24 16:00 test3

[root@oracle test]# find . -type d -print -exec ls {} ;
.
1 1.txt 2.txt 3.txt abc1 test1 test2 test3
./test2
./test1
test1.1
./test3
例二:
find . -name abc1 -exec rm -rf {} ;找到当前目录下名为abc1的文件 并执行删除
[root@oracle test]# ll
total 8
-rw-r–r–. 1 root root 0 Sep 24 16:17 1
-rw-r–r–. 1 root root 45 Sep 24 15:57 1.txt
-rw-r–r–. 1 root root 9 Sep 24 15:58 2.txt
-rw-r–r–. 1 root root 0 Sep 24 15:59 3.txt
drwxr-xr-x. 2 root root 21 Sep 24 17:35 test1
drwxr-xr-x. 2 root root 6 Sep 24 16:00 test2
drwxr-xr-x. 2 root root 6 Sep 24 16:00 test3

我目前并不觉得这有什么用,直接列出来或删除不香吗;

### 基本格式 `find`命令的基本格式为:`find path expression`。其中,`path` 是要查找的起始目录,`expression` 是查找的条件和操作。 ### 常见使用场景及示例 #### 按照文件查找 - 在根目录下查找文件 `httpd.conf`: ```bash find / -name httpd.conf ``` - 在 `/etc` 目录下查找文件 `httpd.conf`: ```bash find /etc -name httpd.conf ``` - 使用通配符 `*`(表示 0 个或任意多个字符),在 `/etc` 目录下查找文件名中含有字符串 `srm` 的文件: ```bash find /etc -name '*srm*' ``` - 在当前目录查找文件名开头是字符串 `srm` 的文件: ```bash find . -name 'srm*' ``` #### 按照文件特征查找 - 查找在系统中最后 10 分钟访问的文件(access time): ```bash find / -amin -10 ``` - 查找在系统中最后 48 小时访问的文件: ```bash find / -atime -2 ``` - 查找在系统中为空的文件或者文件夹: ```bash find / -empty ``` - 查找在系统中属于 group 为 `cat` 的文件: ```bash find / -group cat ``` - 查找在系统中最后 5 分钟里修改过的文件(modify time): ```bash find / -mmin -5 ``` - 查找在系统中最后 24 小时里修改过的文件: ```bash find / -mtime -1 ``` - 查找在系统中属于 `fred` 这个用户的文件: ```bash find / -user fred ``` - 查找出大于 10000 字节的文件(`c` 表示字节): ```bash find / -size +10000c ``` - 查找出小于 1000KB 的文件: ```bash find / -size -1000k ``` #### 限制目录深度 `-maxdepth` 和 `-mindepth` 选项可以限制 `find` 命令遍历的目录深度,避免 `find` 命令没完没了地查找。例如,在 `/home` 目录下,只在当前目录及其下一级子目录中查找文件: ```bash find /home -maxdepth 2 ``` #### 跟随符号链接 默认情况下,`find` 命令不会跟随符号链接。`-L` 选项可以强制其改变这种行为,但如果碰上了指向自身的链接,`find` 命令就会陷入死循环中。例如: ```bash find -L /path/to/dir ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值