设为首页 收藏本站
查看: 199|回复: 0

[经验分享] 文件查找find命令的使用

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2015-12-15 08:22:02 | 显示全部楼层 |阅读模式
本节所讲内容:
      文件查找find

find搜索文件系统、实时搜索
find [目录] [条件][动作]

[目录]
不输入代表当前目录
例:
find
find  /boot

[条件]
用户和组:-user -group
例:查找home目录下所有的属于追梦的文件
[iyunv@localhost ~]# find/home/  -user zhuimeng

类型:-type( f 文件 , d 目录 , l 连接 , p 管道 ,c 字符文件 ,b 块文件,s socket文件 )

[iyunv@localhost ~]# find/home/ -type f
[iyunv@localhost ~]# find/home/ -type d

名字:-name
[iyunv@localhost ~]# useradduser1
[iyunv@localhost ~]# useraddvipuser3
[iyunv@localhost ~]# touch/home/user1/aaausercccc
[iyunv@localhost ~]# find/home/ -name *user*

大小:-size     +NM 大于N兆   -NG 小于NGB
[iyunv@localhost ~]# find/boot/ -size +2M

时间:-mtime  -atime  -ctime

任务:百度一下如何针对分钟进行查找

扩展:linux中ctime,mtime,atime的区别
[iyunv@localhost ~]# touchyy.txt
[iyunv@localhost ~]# statyy.txt
  File: ‘yy.txt’
  Size: 0           Blocks:0          IO Block: 4096   regular empty file
Device: fd01h/64769d  Inode: 35698271    Links: 1
Access: (0644/-rw-r--r--)  Uid: (   0/    root)   Gid: (   0/    root)
Access: 2015-12-0820:59:28.293035474 +0800
Modify: 2015-12-0820:59:28.293035474 +0800
Change: 2015-12-0820:59:28.293035474 +0800

ctime:“改变时间(changetime)”
mtime :“修改时间(modificationtime)”
atime :“访问时间(accesstime)”

例:
改变和修改之间的区别在于是改文件的属性还是更改它的内容。
chmod a-w myfile,那么这是一个改变;改变的ctime
echo  aaa > bajie那么这是一个修改。mtime :“修改时间
atime,改变是文件的索引节点发生了改变;mtime 修改是文本本身的内容发生了变化。

[iyunv@localhost ~]#chmod u+x yy.txt
[iyunv@localhost ~]#stat yy.txt
  File: ‘yy.txt’
  Size: 0           Blocks:0          IO Block: 4096   regular empty file
Device: fd01h/64769d  Inode: 35698271    Links: 1
Access:(0744/-rwxr--r--)  Uid: (    0/   root)   Gid: (    0/   root)
Access: 2015-12-0820:59:28.293035474 +0800
Modify: 2015-12-0820:59:28.293035474 +0800
Change: 2015-12-08 21:02:06.612034204 +0800
可以看到,文件的ctime发生了改变

[iyunv@localhost ~]# statyy.txt
  File: ‘yy.txt’
  Size: 4           Blocks:8          IO Block: 4096   regular file
Device: fd01h/64769d  Inode: 35698271    Links: 1
Access: (0744/-rwxr--r--)  Uid: (   0/    root)   Gid: (   0/    root)
Access: 2015-12-0820:59:28.293035474 +0800
Modify: 2015-12-08 21:03:41.775033441 +0800
Change: 2015-12-08 21:03:41.775033441 +0800
可以看到文件的ctime和mtime发生了改变
结论:无论是文件的属性还是内容发生变化,文件的ctime都会发生改变,只有文件内容发生修改的时候,文件的mtime才会发生变化

[iyunv@localhost ~]# cat yy.txt
aaa
[iyunv@localhost ~]# statyy.txt
  File: ‘yy.txt’
  Size: 4           Blocks:8          IO Block: 4096   regular file
Device: fd01h/64769d  Inode: 35698271    Links: 1
Access: (0744/-rwxr--r--)  Uid: (   0/    root)   Gid: (   0/    root)
Access: 2015-12-0821:06:45.223031969 +0800
Modify: 2015-12-0821:03:41.775033441 +0800
Change: 2015-12-0821:03:41.775033441 +0800
可以看到文件的atime发生了改变
结论:只要对文件内容进行了查看,那么atime就会发生改变

ls(1) 命令可用来列出文件的 atime、ctime和 mtime。
ls -lc filename
      列出文件的 ctime     ll  -c
ls -lu filename
      列出文件的 atime          ll  -u
ls -l filename
      列出文件的 mtime     ll

[iyunv@localhost ~]# date-s  2015-12-11
Fri Dec 11 00:00:00 CST 2015
[iyunv@localhost ~]# find/root/  -mtime +1 | grep yy.txt
/root/yy.txt

[iyunv@localhost ~]# find/root/  -mtime 2 | grep yy.txt
/root/yy.txt

权限:-perm
[iyunv@localhost ~]# find/boot/ -perm 755                  #等于0775权限的文件或目录

4   SUID  2 SGID   1  sticky

[iyunv@localhost ~]# find /tmp/-perm -777                  #至少有777权限的文件或目录

例:
[iyunv@localhost ~]# mkdir ccc
[iyunv@localhost ~]# chmod 777ccc
[iyunv@localhost ~]# mkdir test
[iyunv@localhost ~]# chmod 1777test/
[iyunv@localhost ~]# touch b.sh
[iyunv@localhost ~]# chmod 4777b.sh

查找:

[iyunv@localhost ~]# find/root/  -perm 777
/root/ccc
[iyunv@localhost ~]# find/root/ -perm 1777
/root/test
[iyunv@localhost ~]# find/root/ -perm 4777
/root/b.sh
[iyunv@localhost ~]# find/root/ -perm -777
/root/ccc
/root/test
/root/b.sh

查找的目录深度:
[iyunv@localhost ~]# find/boot/ -maxdepth 2              #只查找目录第二层的文件和目录

多条件:
-a  -o  !   或  -and  -or -not
[iyunv@localhost ~]#find -type f -a -perm -002
[iyunv@localhost ~]#find -type f -and -perm /o+w
./b.sh

[iyunv@localhost ~]# find! -type f -and -perm -001

[动作]
-ls
-ok
-exec
-print
-printf

[iyunv@localhost ~]# cp/etc/passwd test2/
[iyunv@localhost ~]# cp-r /boot/  test2/
[iyunv@localhost ~]#find /root/test2/  -type f -exec rm {} \;
[iyunv@localhost ~]# lstest2/
boot
[iyunv@localhost ~]#find /root/test2/  -type f | xargs rm -rf
[iyunv@localhost ~]# lstest2/
boot

参数解释:
-exec             执行命令  
rm 要执行的命令
{} 表示find -type f 查找出来了文件内容
\;   {} 和 \;之间要有空格。 固定语法,就是以这个结尾


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-151257-1-1.html 上篇帖子: rhel6.5 case for while语句 下篇帖子: rhel7重定向和文件查找
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表