C语言再学习 -- Linux下find命令用法

来源:互联网 发布:智能垃圾桶 知乎 编辑:程序博客网 时间:2024/05/21 02:50

参看:linux下find(文件查找)命令的用法总结

linux下查找文件的命令有两个:locate 和 find

首先说一下locate:

这个命名是对其生成的数据库进行遍历(生成数据库的命令:uodatedb),这一特性决定了用locate查找文件速度很快,但是locate命令只能对文件进行模糊匹配,在精度上来说差了点,简单介绍下它的两个选项:

#locat

-i    查找文件的时候不区分大小写,如: locate -i passwd

-n    只显示查找结果的前N行,如: locate -n 5 passwd

root@zslf:~# locate -n 5 passwd/etc/passwd/etc/passwd-/etc/cron.daily/passwd/etc/init/passwd.conf/etc/pam.d/chpasswd


下面重点说下find:

find在不指定查找目录的情况下是对整个系统进行遍历查找的。

使用格式: find [指定查找目录] [查找规则] [查找完后执行的action]


[指定查找目录]

root@zslf:~# find /etc /tmp /root -name passwd/etc/cron.daily/passwd/etc/passwd/etc/pam.d/passwd

这里要注意的是目录之间要用空格分开


[查找规则]

1)根据文件名查找

-name    根据文件名查找 (精确查找)

-iname   根据文件名查找,但是不区分大小写

root@zslf:~# find /etc -name "passwd*"/etc/init/passwd.conf/etc/cron.daily/passwd/etc/passwd-/etc/passwd/etc/pam.d/passwd

root@zslf:~# find /etc -name "passwd?"/etc/passwd-

root@zslf:~# find /home/test -name "[ab].sh"/home/test/a.sh/home/test/b.sh

文件名通配:

*     表示通配任意的字符

?     表示通配任意的单个字符

[]    表示通配括号里面的任意一个字符


2)根据文件所属用户和组来查找文件

-user         根据属主来查找文件

-group     根据属组来查找文件

root@zslf:/opt# ls -la 总用量 32drwxr-xr-x  8 root root  4096 10月 21 11:28 .drwxr-xr-x 23 root root  4096  9月 20 13:18 ..drwxrwxr-x  3 zslf zslf  4096  9月 27 13:03 ethtool-4.6drwxr-xr-x  4 root root  4096  9月 28 10:46 filedrwxr-xr-x  3 root root  4096  9月  5 19:14 hisi-linuxdrwxr-xr-x  8  501 users 4096  9月 21 19:21 i2c-tools-3.0.1drwxr-xr-x  9 root root  4096 10月 28 14:43 mppdrwxr-xr-x 20 root root  4096  9月 19 15:47 rootfs_uclibc

参看:C语言再学习 -- 修改linux文件权限

root@zslf:/# find ./ -user zslf./opt/ethtool-4.6
root@zslf:/# find ./ -group zslf./opt/ethtool-4.6


3)通过用户和组来查找

-uid          根据用户查找

-group     根据用户组查找

列出目录内用户的识别码大于500的文件或目录root@zslf:/mnt/test# find  ./ -uid  +500././hh
列出目录内组id为500的文件或目录root@zslf:/mnt/test# find  ./ -gid  +500././hh


4)-a 、 -o 、 -not 的使用

root@zslf-virtual-machine:/mnt/test# ls -la总用量 8drwxr-xr-x 2 zslf zslf 4096 11月 25 09:45 .drwxr-xr-x 5 root root 4096 11月 24 17:20 ..-rw-r--r-- 1 zslf zslf    0 11月 25 09:42 a.sh-rw-r--r-- 1 zslf zslf    0 11月 25 09:42 b.sh-rw-r--r-- 1 zslf zslf    0 11月 25 09:42 c.sh-rw-r--r-- 1 root root    0 11月 25 09:45 f.sh-rw-r--r-- 1 root root    0 11月 25 09:45 g.sh


-a 连接两个不同的条件 (两个条件必须同时满足)

root@zslf:/mnt/test# find ./ -name "*.sh" -a -user zslf./a.sh./c.sh./b.sh

-o 连接两个不同的条件 (两个条件满足其一即可)

root@zslf:/mnt/test# find ./ -name "*.sh" -o -user zslf././f.sh./g.sh./a.sh./c.sh./b.sh

-not 对条件取反的

root@zslf:/mnt/test# find ./ -not -user zslf./f.sh./g.sh

5)根据文件时间戳的相关属性来查找文件

我们可以使用 stat 命令来查看一个文件的时间信息,如下:

root@zslf-virtual-machine:/mnt/test# stat ./  文件:"./"  大小:4096      块:8          IO 块:4096   目录设备:801h/2049dInode:291601      硬链接:2权限:(0755/drwxr-xr-x)  Uid:( 1000/    zslf)   Gid:( 1000/    zslf)最近访问:2016-11-25 09:45:24.699140785 +0800最近更改:2016-11-25 09:45:22.255140690 +0800最近改动:2016-11-25 09:45:22.255140690 +0800创建时间:-

-atime、-mtime、-ctime、-amin、-mmin、-cmin

这里的 -atime、-mtime、-ctime 分别对应的是 “最近一次访问时间”,“最近一次内容修改时间”,“最近一次属性修改时间”,这里的atime的单位指的是“天”,amin 的单位是分钟。

查找在五天内没有访问过的文件root@zslfe:/mnt/test# find ./ -atime +5
查找在五天内访问过的文件root@zslf:/mnt/test# find ./ -atime -5././f.sh./g.sh./a.sh./c.sh./b.sh


6)根据文件类型来查找文件

-type 

f    普通文件

d   目录文件

l    链接文件

b   块设备文件

c    字符设备文件

p   管道文件

s    socket文件

root@zslf-virtual-machine:/mnt/test# find ./ -type f./f.sh./g.sh./a.sh./c.sh./b.sh


7)根据大小来查找文件

-size

#find  ./  -size   2M           //查找在/tmp 目录下等于2M的文件
#find  ./  -size  +2M           //查找在/tmp 目录下大于2M的文件
#find  ./  -size  -2M           //查找在/tmp 目录下小于2M的文件

root@zslf-virtual-machine:/mnt/test# find  ./  -size  -2M././f.sh./g.sh./a.sh./c.sh./b.sh

8)根据文件权限查找文件

-perm

#find  ./  -perm   755         //查找在/tmp目录下权限是755的文件
#find  ./  -perm  +222       //表示只要有一类用户(属主,属组,其他)的匹配写权限就行
#find  ./  -perm  -222        //表示必须所有类别用户都满足有写权限

root@zslf-virtual-machine:/mnt/test# find ./ -perm +222././f.sh./g.sh./a.sh./c.sh./b.sh

9)-nouser 和 -nogroup

-nouser     查找无有效属主的文件

-nogroup  查找无有效所属组的文件

#find  ./  -nogroup –a –nouser       
在整个系统中查找既没有属主又没有属组的文件(这样的文件通常是很危险的,作为系统工程师的我们应该及时清除掉)


[查找完执行的action]

-print                             默认情况下的动作

-ls                                  查找到后用 ls 显示出来

-ok [commend]            查找后执行命令的时候询问用户时候要执行

-exec [commend]        查找后执行命令的时候不询问用户,直接执行

root@zslf:/mnt/test# ls -la总用量 8drwxr-xr-x 2 zslf zslf 4096 11月 25 09:45 .drwxr-xr-x 5 root root 4096 11月 24 17:20 ..-rw-r--r-- 1 zslf zslf    0 11月 25 09:42 a.sh-rw-r--r-- 1 zslf zslf    0 11月 25 09:42 b.sh-rw-r--r-- 1 zslf zslf    0 11月 25 09:42 c.sh-rw-r--r-- 1 root root    0 11月 25 09:45 f.sh-rw-r--r-- 1 root root    0 11月 25 09:45 g.sh
更改权限root@zslf-virtual-machine:/mnt/test# find ./ -name "*.sh" -exec chmod 777 {} \;root@zslf-virtual-machine:/mnt/test# ls -la总用量 8drwxr-xr-x 2 zslf zslf 4096 11月 25 09:45 .drwxr-xr-x 5 root root 4096 11月 24 17:20 ..-rwxrwxrwx 1 zslf zslf    0 11月 25 09:42 a.sh-rwxrwxrwx 1 zslf zslf    0 11月 25 09:42 b.sh-rwxrwxrwx 1 zslf zslf    0 11月 25 09:42 c.sh-rwxrwxrwx 1 root root    0 11月 25 09:45 f.sh-rwxrwxrwx 1 root root    0 11月 25 09:45 g.sh
这里注意  { } 的使用:替代查找到的文件。


重命名root@zslf:/mnt/test# find ./ -name "*.sh" -exec cp {} {}.old \;root@zslf:/mnt/test# ls -la总用量 8drwxr-xr-x 2 zslf zslf 4096 11月 25 10:17 .drwxr-xr-x 5 root root 4096 11月 24 17:20 ..-rwxrwxrwx 1 zslf zslf    0 11月 25 09:42 a.sh-rwxr-xr-x 1 root root    0 11月 25 10:17 a.sh.old-rwxrwxrwx 1 zslf zslf    0 11月 25 09:42 b.sh-rwxr-xr-x 1 root root    0 11月 25 10:17 b.sh.old-rwxrwxrwx 1 zslf zslf    0 11月 25 09:42 c.sh-rwxr-xr-x 1 root root    0 11月 25 10:17 c.sh.old-rwxrwxrwx 1 root root    0 11月 25 09:45 f.sh-rwxr-xr-x 1 root root    0 11月 25 10:17 f.sh.old-rwxrwxrwx 1 root root    0 11月 25 09:45 g.sh-rwxr-xr-x 1 root root    0 11月 25 10:17 g.sh.old

删除查找到的超过30天没有访问过的文件root@zslf:/mnt/test# find ./ -atime +30 -exec rm -rf {} \;


也可以使用xargs来对查找到的文件进一步操作:
参看:C语言再学习 -- Xargs用法详解

root@zslf-virtual-machine:/mnt/test# ls -la总用量 8drwxr-xr-x 2 zslf zslf 4096 11月 25 10:17 .drwxr-xr-x 5 root root 4096 11月 24 17:20 ..-rwxrwxrwx 1 zslf zslf    0 11月 25 09:42 a.sh-rwxr-xr-x 1 root root    0 11月 25 10:17 a.sh.old-rwxrwxrwx 1 zslf zslf    0 11月 25 09:42 b.sh-rwxr-xr-x 1 root root    0 11月 25 10:17 b.sh.old-rwxrwxrwx 1 zslf zslf    0 11月 25 09:42 c.sh-rwxr-xr-x 1 root root    0 11月 25 10:17 c.sh.old-rwxrwxrwx 1 root root    0 11月 25 09:45 f.sh-rwxr-xr-x 1 root root    0 11月 25 10:17 f.sh.old-rwxrwxrwx 1 root root    0 11月 25 09:45 g.sh-rwxr-xr-x 1 root root    0 11月 25 10:17 g.sh.old

修改权限root@zslf:/mnt/test# find ./ -name "*.old" | xargs chmod 700root@zslf:/mnt/test# ls -la总用量 8drwxr-xr-x 2 zslf zslf 4096 11月 25 10:17 .drwxr-xr-x 5 root root 4096 11月 24 17:20 ..-rwxrwxrwx 1 zslf zslf    0 11月 25 09:42 a.sh-rwx------ 1 root root    0 11月 25 10:17 a.sh.old-rwxrwxrwx 1 zslf zslf    0 11月 25 09:42 b.sh-rwx------ 1 root root    0 11月 25 10:17 b.sh.old-rwxrwxrwx 1 zslf zslf    0 11月 25 09:42 c.sh-rwx------ 1 root root    0 11月 25 10:17 c.sh.old-rwxrwxrwx 1 root root    0 11月 25 09:45 f.sh-rwx------ 1 root root    0 11月 25 10:17 f.sh.old-rwxrwxrwx 1 root root    0 11月 25 09:45 g.sh-rwx------ 1 root root    0 11月 25 10:17 g.sh.old


0 0
原创粉丝点击