| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2770 人关注过本帖, 3 人收藏
标题:find命令简述
只看楼主 加入收藏
guixiaolan
Rank: 3Rank: 3
等 级:新手上路
威 望:6
帖 子:356
专家分:0
注 册:2006-4-20
收藏(3)
 问题点数:0 回复次数:1 
find命令简述
在系统管理中find命令无疑是一个功能很强大的命令.这里我只做一些常用的简单的介绍,就当抛砖引玉吧.其它的参数的作用,相信大家都可以通过以后的学习和摸索来慢慢的进行了解.

[bo]0.文件准备[/bo]
[root@localhost example]# [un]tree[/un]
.
|-- example1.conf
|-- example2.conf
|-- find命令详解.txt
|-- subexample1
|   `-- example1.conf
`-- subexample2
    `-- example1.conf

2 directories, 5 files
[root@localhost example]# [un]ls -l [/un]#注意example1.conf与example2.conf文件的属主和权限不同,参考ls –l命令
总计 40
-rw-rw-rw-     1    root       gxlinux 53     04-19 15:01 example1.conf
-rw-rw-r--     1    gxlinux    gxlinux 0      04-19 14:57 example2.conf
-rw-rw-r--     1    gxlinux    gxlinux 972    04-19 16:41 find命令详解.txt
drwxrwxr-x     2    gxlinux    gxlinux 4096   04-19 15:07 subexample1
drwxrwxr-x     2    gxlinux    gxlinux 4096   04-19 15:07 subexample2
[root@localhost example]# [un]cat example1.conf[/un]
Linux
        Fedora
        Redhat
        CentOS
DataBase
        MySQL
        MSSQL
# . -表示当前目录(点号表示当前目录)

[bo]1.type参数[/bo]
[root@localhost example]# find . -type d #查找目录
.
./subexample1
./subexample2
[root@localhost example]# find . -type f #查找普通文件
./example1.conf
./subexample1/example1.conf
./example2.conf
./subexample2/example1.conf
#b-块设备文件 c-字符设备文件 p-管道文件 l-符号链接文件

[bo]2.user参数[/bo]
[root@localhost example]# find . -user root #查找当前目录下属主为root的文件或目录
./example1.conf
#同理[bo]group参数[/bo],表示按照所属的组来查找

[bo]3.perm参数[/bo]
[root@localhost example]# find . -perm 666 #查找权限为rw-rw-rw-的文件或目录
#r、w、x分别表示4、2、1,rw-相加得6
./example1.conf

[bo]4.name参数[/bo]
[root@localhost example]# find . -name "example1*"#查找文件名以example1开头的文件或目录(*是正则表达式,匹配任意个字符,下面的[1-2]匹配1到2,参考正则表达式)
./example1.conf
./subexample1/example1.conf
./subexample2/example1.conf
[root@localhost example]# find . -name "example[1-2]*"
./example1.conf
./subexample1/example1.conf
./example2.conf
./subexample2/example1.conf

[bo]5.size参数[/bo]
[root@localhost example]# find . -size +50c -type f #查找大于50字节的普通文件
./example1.conf
./find命令详解.txt
[root@localhost example]# find . -size -50c -type f #查找小于50字节的普通文件
./subexample1/example1.conf
./example2.conf
./subexample2/example1.conf

[bo]6.prune与path参数[/bo]
[root@localhost example]# find . -path ./subexample1 -prune -o –print #查找不在./subexample1目录下的文件或目录
.
./find命令详解.txt
./example1.conf
./example2.conf
./subexample2
./subexample2/example1.conf
# -o表示||(或) -a表示&&(与)
[root@localhost example]# find . -path ./subexample1 -o -name "example2*" -prune -o –print #查找不在./subexample1目录下或者不以example2开头的文件或目录
.
./find命令详解.txt
./example1.conf
./subexample1/example1.conf
./subexample2
./subexample2/example1.conf
[root@localhost example]# find . \( -path ./subexample1 -o -path ./subexample2 \) -prune -o -print
.
./find命令详解.txt
./example1.conf
./example2.conf

[bo]7.mtime参数[/bo]
[root@localhost example]# find . -mtime +1 -type d #查找1天前修改的文件,n天改写为n即可
[root@localhost example]# find . -mtime -1 -type d #查找1天内修改的文件
.
./subexample1
./subexample2
# -mmin n查找系统中最后N分钟被改变文件数据的文件
# -amin n查找系统中最后N分钟访问的文件
# -cmin n查找系统中最后N分钟被改变文件状态的文件
#####
# -mtime n查找系统中最后N天被改变文件数据的文件
# -atime n查找系统中最后N天被访问的文件
# -ctime n查找系统中最后N天被改变文件状态的文件

[bo]8.ok与exec参数[/bo]
[root@localhost example]# find . -type f -size 0 -ok rm {} \; #查找大小为0的普通文件并删除它
#格式:-ok参数后写shell命令,注意{}与\;之间有一个空格
< rm ... ./subexample1/example1.conf > ? n
< rm ... ./example2.conf > ? n
< rm ... ./subexample2/example1.conf > ? n
[root@localhost example]# find . -name "example1*" -ok grep "Fed" {} \; #查找以example1开头的文件,然后用grep命令看看这些文件中是否有”Fed”字符,并且给出提示(删除文件时有用)
< grep ... ./example1.conf > ? y
        Fedora
< grep ... ./subexample1/example1.conf > ? y
< grep ... ./subexample2/example1.conf > ? y
[root@localhost example]# find . -name "example1*" -exec grep "Fed" {} \; #同上,但是不给出提示,直接执行,删除文件时小心
        Fedora

[bo]9.xargs命令[/bo]
[root@localhost example]# find . -name "example1*" | xargs grep "Fed"
./example1.conf:        Fedora
[root@localhost example]# find . -name "example1*"|xargs echo ""  > 1.log #查找文件,并将查找结果写入日志文件中
[root@localhost example]# cat 1.log
 ./example1.conf ./subexample1/example1.conf ./subexample2/example1.conf
(xargs命令同exec参数,建议使用该命令,因为它是分批执行,而exec是将find 查找到的所有结果一次执行,结果过大会出现错误)
搜索更多相关主题的帖子: find 命令 简述 
2008-04-21 01:11
lan2008
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2008-4-30
收藏
得分:0 
总结得不错!
2008-04-30 16:24
快速回复:find命令简述
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017378 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved