find命令

常用操作

# 查找 3 天前的文件并删除
find ./ -mtime +3 -name "pcm_*" | xargs rm -rf 

# 查找 3 天内的文件
find ./ -mtime -3 -name "pcm_*" | xargs rm -rf 

# 查找 30 分钟内的文件
find ./ -mmin -30 -name "pcm_*" | xargs rm -rf 

# 查找 30 分钟前的文件
find ./ -mmin +30 -type f -name "pcm_*" | xargs rm -rf 

# 查找当前目录下 2022 年的目录
find ./ -maxdepth 1 -type d -newermt '2022-01-01' ! -newermt '2023-01-01'

# 查找2023年9月号之前的所有目录
find ./ -type d -maxdepth 1 ! -newrmt 2023-09-01

# 找出/tmp/目录下所有者为非root的所有文件,且文件名中包含fstab字符的文件;
find /tmp/ -not -user root -a  -iname "*fstab*"  -ls

# 查找/var目录下所有者为root,且所属组为mail的所有文件或目录
find /var/ -user root -a -group mail -ls

# 查找/var目录下所有者不为root,bin或hadoop的所有文件或目录
find /var/ -not -user root -a -not -user bin -a -not -user hadoop -ls
find /var -not \( -user root -o -user bin -o -user hadoop \) -ls

# 查找/etc目录下最近一周内其内容修改过,且属主不是root用户也不是hadoop用户的文件或目录
find /etc/ -mtime -7 -a -not \( -user root -o -user hadoop \) -ls

# 查找当前系统上没有属主或属组,且最近一周曾被访问过的文件或目录
find / \( -nouser -o -nogroup \) -a -atime -7 -ls

# 查找/etc目录下大于1M且类型为普通文件的所以文件
find /etc/ -size +1M -type f -ls
find /etc/ -size +1M -type f -exec ls -lh {} \;

# 查找/etc目录下所以用户都没有写权限的文件
find /etc/ -not -perm /222 -type f -ls

# 查找/etc目录下至少有一类用户没有执行权限的文件
find /etc/ -not -perm -111 -type f -ls

# 查找/etc/init.d/目录下,所有用户都有执行权限,且其他用户有写权限的所有文件
find /etc/init.d/  -perm -111 -a -perm -002 -type f -ls
find /etc/init.d/  -perm -113 -type f -ls

# 查找 Ubuntu 目录,并删除
find ./ -name ubuntu -type d | xargs rm -rf
find ./ -name ubuntu -type d -exec rm -rf {} \;
上一篇