shell编程四剑客之find

find工具语法格式

find path(路径) -optiom(参数) action(动作)

  • path路径:可以任意路径,绝对路径,相对路径
  • option参数:-name(名称)、-type(类型)、-mtime(时间)、-size(大小)、-perm(权限)等
  • action动作:exec(承接)、xargs(承接)、print打印等
  1. 查找linux系统下ens33的配置文件路径
   [root@wxj ~]# find / -name *ens160*
   *:表示任意字符
   [root@wxj ~]# find /etc -name *ens160*
   /etc/sysconfig/network-scripts/ifcfg-ens160
   /etc/sysconfig/network-scripts/ifcfg-ens160.bak
  1. 查找linux操作系统下所有的.rpm结尾的软件包所在的路径,并在其中查找libselinux开头的所有文件,并拷贝到/tmp目录下,
   [root@wxj ~]# find / -name "*.rpm" -name
   [root@wxj ~]# find / -name "*.rpm" -name libselinux*
   /mnt/Packages/libselinux-2.5-12.el7.x86_64.rpm
   /mnt/Packages/libselinux-utils-2.5-12.el7.x86_64.rpm
   /mnt/Packages/libselinux-python-2.5-12.el7.x86_64.rpm
   /mnt/Packages/libselinux-devel-2.5-12.el7.x86_64.rpm
   #利用$符号
   [root@wxj /]# cp $(find / -name *.rpm  -name libselinux* ! -name libselinux-utils*) /tmp   
 #利用`符号    
[root@wxj /] # cp `find / -name *.rpm  -name libselinux* ! -name libselinux-utils*` /rpm  
  #利用变量     
[root@wxj /]# for i in `find / -name *.rpm  -name libselinux* ! -name libselinux-utils*` ;do cp $i /rpm/ ;done 
#利用exec参数复制
[root@wxj /]# find / -name *.rpm  -name libselinux* ! -name libselinux-utils* -exec cp {} /tmp/ \;
 #利用exec参数授权 
[root@wxj /]# find / -name *.rpm  -name libselinux* ! -name libselinux-utils* -exec chmod 600 {} \;
 #利用管道符xargs删除查找到的文件 
[root@wxj /]# find / -name *.rpm  -name libselinux* ! -name libselinux-utils* |xargs  rm -rf 
#利用管道符xargs去复制 
[root@wxj /]# find / -name *.rpm  -name libselinux* ! -name libselinux-utils* |xargs -i cp {} /tmp/ \;
  • exec和xargs两个参数不同点:exec一次查找一个然后传递(),xargs全部找到在统一传递
查找到的文件查看权限   
[root@wxj /]# find / -name "*.rpm" -name "openssl*" -type f -exec  ls -l {} \;
   -rw-r--r-- 1 root root 504996 8月  23 05:37 /var/cache/yum/x86_64/7/base/packages/openssl-1.0.2k-19.el7.x86_64.rpm
   -rw-r--r-- 1 root root 1581408 8月  23 05:37 /var/cache/yum/x86_64/7/base/packages/openssl-devel-1.0.2k-19.el7.x86_64.rpm
   -rw-r--r-- 1 root root 1254680 8月  23 05:37 /var/cache/yum/x86_64/7/base/packages/openssl-libs-1.0.2k-19.el7.x86_64.rpm

查看30天以前的文件

   [root@wxj /]# find / -name "*.rpm" -name "openssl*" -type f -mtime +30
  1. 权限恢复
   [root@wxj data]# find . -type d -exec chmod 755 -R {} \;
[root@wxj data]# find . -type f -exec chmod 644 {} \;

发表评论

后才能评论