CentOS 文件查找常用方法
在 CentOS 中,常用的文件查找手段包括:find(实时、条件精确)、locate(基于数据库、速度极快)、以及用于定位命令的 which/whereis;若要在文件内容中查找关键字,则使用 grep。下面按场景给出高效用法与示例。
按文件名或路径查找
find / -name "a.txt" 2>/dev/nullfind /etc -iname "*SELINUX*"find /tmp -type d -name "1*"find /tmp -type f -name "1*"2>/dev/null 可屏蔽错误输出。按大小、时间与权限查找
find / -size +1G(大于 1GB)、find / -size -10M(小于 10MB)。单位可用 b、c、w、k、M、G。find . -perm 644find . -perm -644find /root -name "*.txt" -user root -size -20M。快速定位与命令路径查找
updatedb;安装:yum install -y mlocate。locate hello.txt。which ss。whereis ifconfig。在文件内容中查找关键字
grep -i -n "happy" hello.txtfind /etc -name "ifcfg*" | xargs grep -n "eth0"查找后处理与实用技巧
find / -name "*.tmp" -ok rm -i {} \;find /var/log -name "*.log" -mtime +7 -exec mv {} {}.bak \;ls -i 查看 inode;find /tmp -inum <inode号> 反向定位。find / -path /proc -prune -o -name "*.conf" -print。find /opt -name "*.conf" -print0 | xargs -0 ls -l(处理含空格路径更安全)。