文件查找

=====文件的查找:
命令文件: which ls  (echo $PATH)

任意文件:
locate test01   (updatedb更新locate数据库)

find
find [options] [path...] [expression]

按文件名:
[root@station230 ~]# find /etc -name "ifcfg-eth0"
[root@station230 ~]# find /etc -iname "ifcfg-eth0" //-i忽略大小写
[root@station230 ~]# find /etc -iname "ifcfg-eth*"

按大小:
[root@station230 ~]# find /etc -size +5M   //大于5M
[root@station230 ~]# find /etc -size 5M
[root@station230 ~]# find /etc -size -5M
[root@station230 ~]# find /etc -size +5M -ls  //-ls选项

按时间找(atime,mtime,ctime)
[root@station230 ~]# find /etc -mtime +5   //修改时间超过5天
[root@station230 ~]# find /etc -mtime 5   //修改时间等于5天
[root@station230 ~]# find /etc -mtime -5   //修改时间5天以内

按文件属主、属组找
[root@station230 ~]# find /home -user jack  //属主是jack的文件
[root@station230 ~]# find /home -group jack  //属组是jack组的文件

[root@station230 ~]# find /home -user jack -group alice   //等同于 -a
[root@station230 ~]# find /home -user jack -a  -group alice  // and
[root@station230 ~]# find /home -user jack -o  -group alice  // or
[root@station230 ~]# find /home -nouser
[root@station230 ~]# find /home -nogroup
[root@station230 ~]# find /home -ls -nouser -o -nogroup
[root@station230 ~]# find /home -nouser -o -nogroup -ls

按文件类型:
[root@station230 ~]# find /dev -type f  //f普通
[root@station230 ~]# find /dev -type d  //d目录
[root@station230 ~]# find /dev -type l  //l链接
[root@station230 ~]# find /dev -type b  //b块设备
[root@station230 ~]# find /dev -type c  //c字符设备
[root@station230 ~]# find /dev -type s  //s套接字
[root@station230 ~]# find /dev -type p  //p管道文件

-inum n
find / -inum 31064284           //根据inode 查找

按权限:
[root@station230 dir8]# find . -perm 644 -ls
[root@station230 dir8]# find . -perm -644 -ls
[root@station230 ~]# find /sbin -perm -4000 -ls  //包含set uid
[root@station230 ~]# find /sbin -perm -2000 -ls  //包含set gid
[root@station230 ~]# find /sbin -perm -1000 -ls  //包含sticky

找到后处理的动作:
[root@station230 ~]# find /etc -name "ifcfg-eth0" -exec cp -rf {} /tmp \;
[root@station230 dir8]# find . -name "file*" -exec rm -rvf {} \;

1. 将/etc/中的所有目录(仅目录)复制到/tmp下,目录结构不变
[root@station230 ~]# find /etc -type d -exec mkdir -p /tmp/{}  \;

2. 将/etc目录复制到/var/tmp/,
   将/var/tmp/etc中的所有目录设置权限777(仅目录)
   将/var/tmp/etc中所有文件权限设置为666
[root@station230 tmp]# find etc -type d -exec chmod 777 {} \;
[root@station230 tmp]# find etc -type f -exec chmod 666 {} \;
[root@station230 tmp]# find etc ! -type d -exec chmod 666 {} \;

3. 创建目录/dir1,在/dir1中创建文件file1 file2 file3...file100
  使用find查找并删除 除了file2以外的所有文件
[root@station230 ~]# mkdir /dir1   
[root@station230 ~]# touch /dir1/file{1..100}
[root@station230 etc]# find . ! -name file2 -exec rm -rf {} \;
[root@station230 dir1]# find . ! -name file50 -a ! -name file70 -exec rm -rf {} \;  删除file50和file70以外的所有文件
find . -mtime +30 -a -size +100M -a ! -name "alert_orcl_*" -exec rm -rf {} \;  查找修改时间30天以上,大于100M,名字不是alert_orcl_开头的文件,并删除
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值