Chinaunix首页 | 论坛 | 博客
  • 博客访问: 112272
  • 博文数量: 28
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 202
  • 用 户 组: 普通用户
  • 注册时间: 2014-05-31 21:51
个人简介

諸惡莫做,眾善奉行,自淨其意!

文章分类

全部博文(28)

文章存档

2018年(1)

2017年(3)

2015年(3)

2014年(21)

我的朋友

分类: LINUX

2014-10-13 20:49:33

原文地址:
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 查找到的所有结果一次执行,结果过大会出现错误)
阅读(1435) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~