其实最近一直没闲着,学校的硬件课学的比较纠结,所以把人学的很恶心。但是闲下来还是决定多少在写点东西
find的命令----作为linux下一个有效的工具,可以遍历当前目录甚至整个文件系统来查询某些文件,但是非常消耗资源,如果遍历的文件系统比较大的话可以&打到后台运行。
find命令的基本格式
find pathname -options [-print -exec -ok]
1.pathname-----路径名称
2.-print-----find命令将匹配的文件输出到标准输出
3.-exec-----find命令对匹配的文件执行该参数所给出的shell命令
-exec COMMAND { } \;--------{ }表示find所匹配出的内容
4.-ok 和-exec的作用是一样的
find的命令选项
-name 按照文件名查找
[root@server37 ~]# find /mnt/shell -name *.sh /mnt/shell/sum.sh /mnt/shell/User_check.sh /mnt/shell/check_blank.sh /mnt/shell/mage/check_disk.sh /mnt/shell/mage/cpcommand.sh /mnt/shell/mage/test_shift.sh /mnt/shell/mage/networkcheck.sh
-perm 按照文件权限查找
[root@server37 ~]# find /etc -perm 640 /etc/logrotate.d/named /etc/sysconfig/auditd /etc/audisp/audispd.conf /etc/audisp/plugins.d/af_unix.conf /etc/audisp/plugins.d/syslog.conf /etc/libaudit.conf /etc/rndc.key /etc/audit/auditd.conf /etc/audit/audit.rules /etc/named.conf /etc/named.rfc1912.zones
-iname filename 精确文件查找-----不区分大小写
[root@server37 ~]# find /mnt/shell -name add2.sh /mnt/shell/play/add2.sh
-prune 忽略某个目录查找
-user Username 根据属主查找-----同理有-group groupname按照属组查询
[root@server37 ~]# find /mnt/shell -name *.sh "/mnt/shell/mage/" -prune -o -print /mnt/shell /mnt/shell/sum.sh /mnt/shell/fstab /mnt/shell/grep.txt /mnt/shell/User_check.sh /mnt/shell/user.txt /mnt/shell/functions /mnt/shell/history.txt
[root@server37 ~]# find / -user named /var/named/dynamic /var/named/dynamic/managed-keys.bind.jnl /var/named/dynamic/managed-keys.bind /var/named/chroot/var/run/named /var/named/chroot/var/log /var/named/chroot/var/tmp /var/named/data
-not -user 查找不是该用户的文件-----同理有-not -group 不是该属组的文件
-gid 和-uid是按照文件的gid和uid来查找
-nogroup和-nouser查找无有效属组合有效用户的文件
find的文件属性查找
-type
b 块设备
d 目录
c 字符设备文件
p 管道文件
l 符号链接文件
f 普通文件
-size 查找文件长度n块的文件
[root@server37 ~]# find /mnt -size 100 /mnt/httpd-2.2.26/modules/aaa/mod_authnz_ldap.c /mnt/httpd-2.2.26/modules/experimental/mod_example.c
查找更改时间在5日以内的文件
当然时间的查找还有
-atime
-ctime
这3个时间戳选项都是按照touch上所显示的时间戳查看的
find / -mtime -5 -print /sys/module/nf_conntrack/sections/__ksymtab_gpl /sys/module/nf_conntrack/sections/__kcrctab_gpl /sys/module/nf_conntrack/sections/__param /sys/module/nf_conntrack/sections/__ksymtab_strings /sys/module/nf_conntrack/sections/__mcount_loc
说一下-exec把,个人觉得是个非常好用的命令,当然-ok和xargs和他有同样的功能
今天看到一个-exec的好例子分享一下吧
查看某个目录下的文件就以sh结尾为例吧,把查找的每个文件的第一行都放到一个new文件里面去
find /mnt/ -name *sh -type f -exec head -1 {} >/tmp/new \;
这一条命令让我体会到-exec这个命令着实好用呀,因为{}内得到的是一个队列,一组数据,通过-exec处理,是一行行处理的,这个就可以不用循环一个个在队列里处理了