find命令通过遍历,查找指定类型的文件或目录
find有很多命令格式,例如
1 、使用name选项
find ~ -name "*.txt" -print 查找~ (代表$HOME)中符合txt的文件
find . -name "*.txt" -print 查找当前目录中符合txt的文件
find . -name "*[A-Z]" -print 查找当前目录中以一个大写字母开头的文件
find /etc -name "host*" -print 查找当前目录中以host开头的的文件
find /oracle -name "*" -print 查看oracle目录下所有文件
2、使用perm 选项 按照文件权限模式来查找文件
find . -perm 755 -print 在当前目录下查找权限是755 的文件
3、find /apps -name "/apps/bin" -prune -o -print
在/apps 下查找文件,但是不查询/apps/bin 目录下的文件
4、find ~ -user oracle -print 查询$home 下属主为oracle的文件
find /home -nouser oracle -print 查询/home 下属没有属主的文件
5、按更改时间查找文件
find / -mtime -5 -print 在根目录下查找更改时间在5日内的文件
find / -mtime +3 -print 在根目录下查找更改时间在3日前的文件
6、查找比某个文件新或旧的文件
格式: newest_file_name ! oldest_file_name
find . -newer age.awk ! -newer belts.awk -exec ls -l {} \ ; 查找比age.awk新但比belts.awk旧的文件
7、使用type
find /etc -type d -print 查找/etc下所有目录
find . ! -type d -print 查找当前目录下除目录以外的所有类型文件
find /etc -type l -print 在/ e t c目录下查找所有的符号链接文件
8、使用size选项
find . -size +1000000c -print 在当前目录下查找文件长度大于1 M字节的文件 c是字节
find /home/apache -size 100c -print 在/home/apache目录下查找文件长度恰好为100字节的文件
find . -size +10 -print 在当前目录下查找长度超过10块的文件(一块等于512字节)
9、当匹配到一些文件以后,可能希望对其进行某些操作,这时就可以使用-exec选项。
find . -type f -exec ls -l {} \ ;
find logs -type f -mtime +5 -exec rm {} \ ;
find logs -type f -mtime +5 -ok rm {} \ ; 会出现提示
10、xargs 通过每次获取一部分文件 可以防止exec 命令长度有限制,报错“参数列太长”或“参数列溢出”
在整个系统中查找内存信息转储文件(core dump) ,然后把结果保存到/tmp/core.log 文件中:
find . -name "core" -print | xargs echo "" >/tmp/core.log
查询在/apps/oracle 目录下所有用户具有读,写,执行权限的文件,并回收相应的写权限
find /apps/oracle -perm -7 -print | xargs chmod o-w
11、查询在config.xml中的orcl字符串
find ./ -name "config.xml" -exec sed -n '/orcl/'p {} \;
find ./ -name "config.xml" -exec sed -n '/cluster=true/'p {} \;
find ./ -name "config.xml" -exec sed -n '/190/'p {} \;
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/10065315/viewspace-621722/ ,如需转载,请注明出处,否则将追究法律责任。