find 命令在linux系统主要用于遍历文件等级,找到匹配的文件.和目录,并针对找到的文件或者目录项进行后续的操作. find 可以根据名称,创建时间,修改时间,文件大小,属主,以及权限进行查找. 可以通过‘-exec’ 对找到的文件或者文件夹执行后续的操作
(1) 根据文件大小搜索文件
- 查找大于1024字节的文件
find ~ -size +1024c
2. 查找等于1024字节的文件
find ~ -size 1024c
3. 查找小于1500字节的文件
find ~ -size -1500c
4. 查找大于512k字节的文件
find ~ -size +512k
5. 查找等于1M字节的文件
find ~ -size 1M
6. 查找小于1G字节的文件
find ~ -size -1G
7. 查找大于10块的文件
find ~ -size +10
8. 查找等于10块的文件
find ~ -size -10
9. 查找大于10M小于20M的文件
find ./ -size +10M -size -20M
10. 查找小于10块的文件
find ~ -size -10
11. 查找文件/目录字节为0的文件(即空文件)
find / -empty
(2) 根据文件内容查找文件
从/etc目录查找所有匹配*.conf 的文件,且文件里面包含centos关键字,并打印所在行
[root@localhost next-terminal]# find /etc -name "*.conf" -type f -print | xargs grep centos
/etc/ntp.conf:server 0.centos.pool.ntp.org iburst
/etc/ntp.conf:server 1.centos.pool.ntp.org iburst
/etc/ntp.conf:server 2.centos.pool.ntp.org iburst
/etc/ntp.conf:server 3.centos.pool.ntp.org iburst
(3) 根据日期查找文件(atime,mtime,ctime 分别对应access访问, modify 修改,create创建)
查找24小时以内修改过的文件
find / -mtime 0
查找7天以前修改的文件
find / -mtime +7
查找7天以内修改的文件
find / -mtime +7
(4) 查找并执行后续操作
find . -name "*.go" -type f -exec ls -l {} \;
find / -name "*.h" -exec grep -rns "include" {} \;