find命令

find:查找目录下的文件

1:依据时间查找

[root@localhost ~]# find . -atime -2      #查找2天内受到访问的文件
[root@localhost ~]# find . -mtime -5       #查找5天内受到修改的文件
[root@localhost ~]# find . -mtime +5       #查找5天前受到修改的文件
#:  . 代表当前目录

2:-name 指定文件名或关键字查找

[root@localhost ~]# find . -name "i*" -perm 644
[root@localhost ~]# find . -name "i*" -perm 644 -print    #查找以i开头的文件名 并且权限是644的文件
./index.html
./index.html.1
./index.html.2

3:利用"!"反向查找

[root@localhost ~]# find /etc/networks -type d    #查找目录
[root@localhost ~]# find /etc/networks   !  -type d  #反向查找,不查找目录文件
/etc/networks

4:按大小查找

[root@localhost ~]# find /etc  -size +5M      

5:find正则表达式

[root@localhost ~]# find  /  -regex ".*find"     
/sys/kernel/debug/tracing/events/xfs/xfs_buf_find
/usr/bin/find
/usr/bin/oldfind

6:find的特殊操作

[root@localhost ~]# find /etc/  -name "rc*" -type f -exec ls -l {} ;
-rw-r--r--. 1 root root 1249 Mar 25  2014 /etc/bash_completion.d/rct
-rw-r--r--. 1 root root 477 Apr  2  2014 /etc/rc.d/rc.local

[root@localhost ~]# find /etc/  -name "p*" -type f -exec ls -l {} ;
[root@localhost ~]# find /var/log/  -mtime -1 -ok rm {} ;     
< rm ... /var/log/ > ? y
rm: cannot remove ‘/var/log/’: Is a directory
< rm ... /var/log/lastlog > ? y
# -exec直接操作
# -ok安全模式
# {} 指代前面find查找到的内容

7:ls -l 命令放在xargs后面

[root@localhost ~]# find . -type f| xargs ls -l
-rw-------. 1 root    root        851 Sep  5 19:44 ./anaconda-ks.cfg
-rw-------. 1 root    root      23096 Nov 11 19:14 ./.bash_history
-rw-r--r--. 1 root    root         18 Dec 28  2013 ./.bash_logout
-rw-r--r--. 1 root    root        176 Dec 28  2013 ./.bash_profile
-rw-r--r--. 1 root    root        176 Dec 28  2013 ./.bashrc

[root@localhost ~]# find . -name "in*" | xargs -p rm -f    #-p参数删除会提示,可以防止误删文件
rm -f ./index.txt ./index.html1 ./index.html2 ?...y
原文地址:https://www.cnblogs.com/zhoujun007/p/11845216.html