手机版
你好,游客 登录 注册
背景:
阅读新闻

Linux基础教程:Linux find命令用法小结

[日期:2011-12-17] 来源:Linux社区  作者:Linux [字体:大 中 小]

find是个使用频率比较高的命令。常常用它在系统特定目录下,查找具有某种特征【名字类型属主权限等】的文件。
find命令的格式: find  [-path ..] -options [-print -exec -ok]

path:要查找的目录路径。

~ 表示$HOME目录
. 表示当前目录
/ 表示根目录
-print :表示将结果输出到标准输出
-exec :对匹配的文件执行该参数所给出的shell命令。形式为 command  {} \; ,注意{}与\; 之间有空格
-ok :与-exec作用相同,区别在于,在执行命令之前,都会给出提示,让用户确认是否执行

options常用的有下选项:
-name 按照名字查找
-perm 安装权限查找
-prune 不再当前指定的目录下查找
-user 文件属主来查找
-group 所属组来查找
-nogroup 查找无有效所属组的文件
-nouser 查找无有效属主的文件
-type 按照文件类型查找
下面通过一些简单的例子来介绍下find的常规用法:

1、按名字查找

在当前目录及子目录中,查找大写字母开头的txt文件

[root@localhost ~]# find . -name '[A-Z]*.txt' -print 

在/etc及其子目录中,查找host开头的文件

[root@localhost ~]# find /etc -name 'host*' -print 

在$HOME目录及其子目录中,查找所有文件

[root@localhost ~]# find ~ -name '*' -print 

在当前目录及子目录中,查找不是out开头的txt文件

[root@localhost .code]# find . -name "out*" -prune -o -name "*.txt" -print   

2、按目录查找

在当前目录除aa之外的子目录内搜索 txt文件

[root@localhost .code]# find . -path "./aa" -prune -o -name "*.txt" -print 

在当前目录及除aa和bb之外的子目录中查找txt文件

[root@localhost .code]# find . \( -path "./aa" -o -path "./bb" \) -prune -o -name "*.txt" -print   

在当前目录,不再子目录中,查找txt文件

[root@localhost .code]# find .  ! -name "." -type d -prune -o -type f -name "*.txt" -print 

3、按权限查找

在当前目录及子目录中,查找属主具有读写执行,其他具有读执行权限的文件

[root@localhost ~]# find . -perm 755 -print 

4、按类型查找

在当前目录及子目录下,查找符号链接文件

[root@localhost .code]# find . -type l -print 

5、按属主及属组

查找属主是www的文件

[root@localhost .code]# find / -user www -type f -print 

查找属主被删除的文件

[root@localhost .code]# find /  -nouser -type f -print 

查找属组mysql的文件

[root@localhost .code]# find /  -group mysql -type f  -print 

查找用户组被删掉的文件

[root@localhost .code]# find /  -nogroup -type f  -print 

linux
相关资讯       Linux find命令 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款