shell三剑客之find
查找以ini结尾的文件
[root@iZj6cbstl2n6r280a27eppZ app]# find / -name "*.ini"
/app/myblog/config.ini
exec解释:
-exec 参数后面跟的是 command 命令,它的终止是以';'为结束标志的,所以这句命令后面的分号是不可缺少的,考虑到各个系统中分号会有不同的意义,所以前面加反斜杠。
{} 花括号代表前面find查找出来的文件名。
查找 /imes/ffdc文件下的txt文件,并以时间排序。
[root@tavli19 ~]# find /imes/ffdc -name "*.txt"|xargs ls -lta
-path 可以使用通配符来匹配文件路径。-name用给定的文件名进行匹配,-path则将文件路径作为一个整体进行匹配。
[root@SSAVL2734 ansible]# find /usr/lib/nagios/libexec/ -path "*/wechat_oms/*"
/usr/lib/nagios/libexec/wechat_oms/sendmail_weather.py
/usr/lib/nagios/libexec/wechat_oms/connectunix.py
/usr/lib/nagios/libexec/wechat_oms/checktablespace_multiple.py
/usr/lib/nagios/libexec/wechat_oms/connectoracle.py
/usr/lib/nagios/libexec/wechat_oms/.git
查找 /usr/lib/nagios/libexec下面 包含.py和.sh的文件
[root@SSAVL2734 ansible]# find /usr/lib/nagios/libexec/ -regex ".*\(\.py\|\.sh\)$"
/usr/lib/nagios/libexec/check_mysql_formal2.py
/usr/lib/nagios/libexec/check_oracle.py
/usr/lib/nagios/libexec/check_note_balance2.py
/usr/lib/nagios/libexec/check_note_balance.py
/usr/lib/nagios/libexec/checkbyjdbc/makefile.sh
/usr/lib/nagios/libexec/checkbyjdbc/Samples.sh
查找/usr/lib/nagios下面子目录下的包含py和sh的文件
[root@SSAVL2734 ansible]# find /usr/lib/nagios -maxdepth 2 -regex ".*\(\.py\|\.sh\)$"
/usr/lib/nagios/plugins/check_tomcat.py
/usr/lib/nagios/plugins/check_tomcat_threadpool_uname.py
/usr/lib/nagios/plugins/check_tomcatSessions.sh
/usr/lib/nagios/plugins/check_tomcat_memory.py
/usr/lib/nagios/plugins/check_tomcat_threadpool.py
/usr/lib/nagios/plugins/utils.sh
/usr/lib/nagios/libexec/check_procedure.py
/usr/lib/nagios/libexec/check_oracle_test.py
find 命令匹配到了当前目录下的所有普通文件,并在 -exec 选项中使用 ls -l 命令将它们列出。
[root@iZj6cbstl2n6r280a27eppZ tmp]# find . -type f -exec ls '{}' ';'
./pip-mQo5bs-unpack/uwsgi-2.0.15.tar.gz
./pip-VnYL06-unpack/Mezzanine-4.2.3-py2.py3-none-any.whl
./pip-RKCLec-unpack/Pygments-2.2.0-py2.py3-none-any.whl
[root@iZj6cbstl2n6r280a27eppZ tmp]# find . -type f -exec ls -l {} \;
-rw-r--r-- 1 root root 10240 9月 12 21:15 ./pip-mQo5bs-unpack/uwsgi-2.0.15.tar.gz
-rw-r--r-- 1 root root 194560 9月 12 20:30 ./pip-VnYL06-unpack/Mezzanine-4.2.3-py2.py3-none-any.whl
在目录中查找更改时间在5天以前后缀为pl的文件并删除
[root@iZj6cbstl2n6r280a27eppZ tmp]# find . -name "*.pl" -mtime +5 -exec rm {} \;
给出删之前的提示:
[root@iZj6cbstl2n6r280a27eppZ tmp]# find . -name "*.pl" -ok rm {} \;
查找/etc目录下的passwd文件,然后匹配文字中是否有root
[root@iZj6cbstl2n6r280a27eppZ tmp]# find /etc/ -name "passwd" -exec grep "root" {} ';'
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
匹配当前目录的log文件,然后将这些log文件拷贝到/app目录中
[root@iZj6cbstl2n6r280a27eppZ tmp]# find . -name "*.log" -exec cp {} /app \;
查找/etc目录下的文件包含127.0.0.1
[root@iZj6cbstl2n6r280a27eppZ app]# find /etc -name \* -type f -print |xargs grep "127.0.0.1"
[root@iZj6cbstl2n6r280a27eppZ app]# find /etc -type f -print |xargs grep "127.0.0.1"
/etc/ntp.conf:restrict 127.0.0.1
/etc/sysconfig/network-scripts/ifcfg-lo:IPADDR=127.0.0.1
/etc/security/access.conf:#+ : root : 127.0.0.1
/etc/postfix/main.cf:#debug_peer_list = 127.0.0.1
/etc/cloud/templates/hosts.redhat.tmpl:127.0.0.1 {{fqdn}} {{hostname}}
/etc/hosts:127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
[root@iZj6cbstl2n6r280a27eppZ tmp]# ls
Aegis-<Guid(5A2C30A2-A87D-490A-9281-6765EDAD7CBA)> pip-bVOjXv-unpack pip-Ya5KyM-unpack
a.txt pip-HOQ99u-unpack pythondy.log
b.txt
将当前目录下所有的.txt文件变为.txt_bak
[root@iZj6cbstl2n6r280a27eppZ tmp]# find . -name "*.txt" -exec mv {} {}_bak \;
[root@iZj6cbstl2n6r280a27eppZ tmp]# ls
Aegis-<Guid(5A2C30A2-A87D-490A-9281-6765EDAD7CBA)> pip-bVOjXv-unpack pip-Ya5KyM-unpack
a.txt_bak pip-HOQ99u-unpack pythondy.log
b.txt_bak
[root@iZj6cbstl2n6r280a27eppZ tmp]# find . -type -exec grep hello '{}' ';' -print