|
文件查找
==============================================================================
概述:
本章将主要介绍在Linux中怎样查找文件和解压缩。需要我们掌握的知识有:locate和find 命令的使用,以及如何使用压缩和解压缩工具。
==============================================================================
Linux文件查找
1.文件查找分类及工具
★文件查找
- 在文件系统上常常需要根据文件的各种属性去查找符合条件的文件,此前使用的grep,egrep,fgrep属于文本过滤、文本搜索工具;而文件查找工具有两个:locate 和 find
★文检查找分为两类:
☉实时查找:
☉非实时查找:
2.locate
★工作特性:
- locate属于非实时查找,查询系统上预建的文件索引数据库(/var/lib/mlocate/mlocate.db)
- 依赖于事先构建的索引;
- 索引的创建实在系统空闲时由系统自动进行(周期性任务);
- 管理员手动更新数据库使用 updatedb 命令;
- 索引构建过程需要遍历整个根文件系统,极消耗资源;
★工作特点:
- 查找速度快;
- 模糊查找;
- 非实时查找;
- 搜索的是文件的全路径,不仅仅是文件名;
- 可能只搜索用户具备读取和执行权限的目录;
★用法:
- locate [OPTION]... PATTERN...
☉选项:
- -b:只匹配路径中的基名;
- -c:统计出共有多少个符合条件的文件;
- -r:使用BRE(基本正则表达式)
- -i:执行区分大小写的搜索;
注意:
- locate的用法非常简单,即:locate KEYWORD,在locate后面跟上所要查找的关键字即可,没有什么难度,但依赖数据库。
演示:
# 搜索名称或路径中带有“passwd”的文件
[root@centos7 ~]# locate passwd
/etc/passwd
/etc/passwd-
/etc/pam.d/passwd
/etc/security/opasswd
/usr/bin/gpasswd
/usr/bin/grub2-mkpasswd-pbkdf2
/usr/bin/htpasswd
/usr/bin/kdepasswd
/usr/bin/lppasswd
/usr/bin/passwd
/usr/bin/smbpasswd
/usr/bin/vncpasswd
/usr/lib/firewalld/services/kpasswd.xml
/usr/lib64/kde4/kded_kpasswdserver.so
/usr/lib64/samba/libsmbpasswdparser-samba4.so
/usr/lib64/samba/pdb/smbpasswd.so
/usr/lib64/security/pam_unix_passwd.so
/usr/sbin/chpasswd
/usr/sbin/lpasswd
/usr/sbin/saslpasswd2
# 加-b选项,只匹配路径中的基名包含passwd
[root@centos7 ~]# locate -b passwd
/etc/passwd
/etc/passwd-
/etc/pam.d/passwd
/etc/security/opasswd
/usr/bin/gpasswd
/usr/bin/grub2-mkpasswd-pbkdf2
/usr/bin/htpasswd
/usr/bin/kdepasswd
/usr/bin/lppasswd
/usr/bin/passwd
/usr/bin/smbpasswd
/usr/bin/vncpasswd
/usr/lib/firewalld/services/kpasswd.xml
/usr/lib64/kde4/kded_kpasswdserver.so
/usr/lib64/samba/libsmbpasswdparser-samba4.so
# 统计出共有多少个符合条件的文件;
[root@centos7 ~]# locate -c passwd
140
[root@centos7 ~]# locate -c -b passwd
132
[root@centos7 ~]# locate -r '\.foo$'
[root@centos7 ~]# locate -cr '\.foo$'
0
Linux文件查找:find
1.工作方式特点及用法
★工作方式
- 实时查找工具,通过遍历指定起始路径下系统层级结构完成文件查找;
★工作特点:
- 查找速度略慢;
- 精确查找;
- 实时查找;
- 可能只搜索用户具备读取和执行权限的目录
★用法:
- find [OPTION]... [查找路径] [查找条件] [处理动作]
☉查找路径:
☉查找条件:
- 指定的查找标准,可以根据文件名、大小、类型、权限等标准进行;默认为找出指定路径下的所有文件
☉处理动作:
- 对符合条件的文件做操作,例如删除等操作;默认输出至标准输出(屏幕);
2.查找条件
★表达式:选项和测试
测试:测试结果通常为布尔型(真:true;假:false)
☉根据文件名和inode查找:
- -name "partern":
- -iname "partern":不区分字母大小写;
支持使用glob 风格的通配符: *, ?, [], [^] 文件名称一定加引号!;
- -inumn:按inode号查找;
- -samefilename:相同inode号的文件;
- -links n:链接数为n的文件;
- -regex "PATTERN":以PATTERN匹配整个文件路径字符串,而不仅仅是文件名称
演示:
1.-name "partern"和-iname "partern",支持使用glob风格第通配符
[root@centos7 ~]# mkdir /etc/test
[root@centos7 ~]# touch /etc/test/Passwd
[root@centos7 ~]# touch /etc/test/MPASSWD
[root@centos7 ~]# touch /etc/test/MPASSWD.txt
[root@centos7 ~]# find /etc -name passwd
/etc/passwd
/etc/pam.d/passwd
[root@centos7 ~]# find /etc -iname passwd
/etc/passwd
/etc/pam.d/passwd
/etc/test/Passwd
# 支持使用glob风格的通配
[root@centos7 ~]# find /etc/ -iname "passwd*"
/etc/passwd
/etc/passwd-
/etc/pam.d/passwd
/etc/test/Passwd
[root@centos7 ~]# find /etc/ -iname "*passwd"
/etc/passwd
/etc/pam.d/passwd
/etc/security/opasswd
/etc/test/Passwd
/etc/test/MPASSWD
[root@centos7 ~]# find /etc/ -iname "passwd?"
/etc/passwd-
[root@centos7 ~]# find /etc/ -iname "?passwd"
/etc/security/opasswd
/etc/test/MPASSWD
[root@centos7 ~]# touch /etc/test/passwdx
[root@centos7 ~]# find /etc/ -iname "passwd?"
/etc/passwd-
/etc/test/passwdx
[root@centos7 ~]# find /etc/ -iname "passwd[[:alnum:]]"
/etc/test/passwdx
★根据属主、属组查找:
- -user USERNAME:查找属主为指定用户的所有文件;
- -group GRPNAME:查找属组为指定组的所有文件;
- -uid UID:查找属主为指定的UID号的所有文件;
- -gid GID:查找属组为指定的GID号的所有文件;
- -nouser:查找没有属主的文件;
- -nogroup:查找没有属组的文件
演示:
1.根据属主,属组查找
[root@centos7 ~]# ll /home
总用量 0
drwx------ 5 arclinux arclinux 121 2月 10 23:32 arclinux
drwx------ 6 centos centos 134 2月 11 01:28 centos
drwx------ 3 mage mage 74 2月 14 19:35 mage
drwx------. 3 mageedu mageedu 74 11月 6 18:31 mageedu
# 根据属主查找
[root@centos7 ~]# find /home -user centos
/home/centos
/home/centos/.mozilla
/home/centos/.mozilla/extensions
/home/centos/.mozilla/plugins
/home/centos/.bash_logout
/home/centos/.bash_profile
/home/centos/.bashrc
/home/centos/.cache
/home/centos/.cache/abrt
/home/centos/.cache/abrt/lastnotification
/home/centos/.cache/dconf
/home/centos/.cache/dconf/user
/home/centos/.config
/home/centos/.config/abrt
/home/centos/.bash_history
/home/centos/.local
/home/centos/.local/share
/home/centos/.local/share/keyrings
/home/centos/.local/share/keyrings/login.keyring
/home/centos/.local/share/keyrings/user.keystore
# 根据属组查找
[root@centos7 ~]# find /home -group arclinux
/home/arclinux
/home/arclinux/.mozilla
/home/arclinux/.mozilla/extensions
/home/arclinux/.mozilla/plugins
/home/arclinux/.bash_logout
/home/arclinux/.bash_profile
/home/arclinux/.bashrc
/home/arclinux/.cache
/home/arclinux/.cache/abrt
/home/arclinux/.cache/abrt/lastnotification
/home/arclinux/.config
/home/arclinux/.config/abrt
/home/arclinux/.bash_history 2.根据UID和GID查找
[root@centos7 ~]# id centos
uid=1001(centos) gid=1001(centos) 组=1001(centos)
[root@centos7 ~]# id arclinux
uid=1002(arclinux) gid=1002(arclinux) 组=1002(arclinux),1003(mygrp)
[root@centos7 ~]# find /home -uid 1001
/home/centos
/home/centos/.mozilla
/home/centos/.mozilla/extensions
/home/centos/.mozilla/plugins
/home/centos/.bash_logout
/home/centos/.bash_profile
/home/centos/.bashrc
/home/centos/.cache
/home/centos/.cache/abrt
/home/centos/.cache/abrt/lastnotification
/home/centos/.cache/dconf
/home/centos/.cache/dconf/user
/home/centos/.config
/home/centos/.config/abrt
/home/centos/.bash_history
/home/centos/.local
/home/centos/.local/share
/home/centos/.local/share/keyrings
/home/centos/.local/share/keyrings/login.keyring
/home/centos/.local/share/keyrings/user.keystore
[root@centos7 ~]# find /home -gid 1002
/home/arclinux
/home/arclinux/.mozilla
/home/arclinux/.mozilla/extensions
/home/arclinux/.mozilla/plugins
/home/arclinux/.bash_logout
/home/arclinux/.bash_profile
/home/arclinux/.bashrc
/home/arclinux/.cache
/home/arclinux/.cache/abrt
/home/arclinux/.cache/abrt/lastnotification
/home/arclinux/.config
/home/arclinux/.config/abrt
/home/arclinux/.bash_history3.查找没有属主和属组的文件
[root@centos7 ~]# ll /home
总用量 0
drwx------ 5 arclinux arclinux 121 2月 10 23:32 arclinux
drwx------ 6 centos centos 134 2月 11 01:28 centos
drwx------ 3 mage mage 74 2月 14 19:35 mage
drwx------. 3 mageedu mageedu 74 11月 6 18:31 mageedu
# 删除一个用户,保留其家目录
[root@centos7 ~]# userdel mage
[root@centos7 ~]# ll /home
总用量 0
drwx------ 5 arclinux arclinux 121 2月 10 23:32 arclinux
drwx------ 6 centos centos 134 2月 11 01:28 centos
drwx------ 3 1003 1004 74 2月 14 19:35 mage # 删除用户后的目录,没有属主和属组
drwx------. 3 mageedu mageedu 74 11月 6 18:31 mageedu
# 查找没有属主的文件
[root@centos7 ~]# find /home -nouser
/home/mage
/home/mage/.mozilla
/home/mage/.mozilla/extensions
/home/mage/.mozilla/plugins
/home/mage/.bash_logout
/home/mage/.bash_profile
/home/mage/.bashrc
★根据文件类型查找
☉格式:-type TYPE
- f:普通文件
- d:目录文件
- l:符号链接文件
- s:套接字文件
- b:块设备文件
- c:字符设备文件
- p:管道文件
- s:套接字文件
演示:
# 查找/dev下所有的块设备
[root@centos7 ~]# find /dev -type b
/dev/sr0
/dev/sda5
/dev/sda4
/dev/sda3
/dev/sda2
/dev/sda1
/dev/sda
[root@centos7 ~]# find /dev -type b -ls
9712 0 brw-rw---- 1 root cdrom 11, 0 2月 20 09:07 /dev/sr0
9705 0 brw-rw---- 1 root disk 8, 5 2月 20 09:07 /dev/sda5
9704 0 brw-rw---- 1 root disk 8, 4 2月 20 09:07 /dev/sda4
9703 0 brw-rw---- 1 root disk 8, 3 2月 20 09:07 /dev/sda3
9702 0 brw-rw---- 1 root disk 8, 2 2月 20 09:07 /dev/sda2
9701 0 brw-rw---- 1 root disk 8, 1 2月 20 09:07 /dev/sda1
9671 0 brw-rw---- 1 root disk 8, 0 2月 20 09:07 /dev/sda
★组合条件
- 与:-a;默认组合逻辑
- 或:-o;
- 非:-not, !
☉德·摩根定律:
- (非P) 或(非Q)= 非(P 且Q)
- (非P) 且(非Q) = 非(P 或Q)
示例:
- !A -a !B = !(A -o B)
- !A -o !B = !(A -a B)
演示:
# 与逻辑
[root@centos7 ~]# find /home -nouser -type f
/home/mage/.bash_logout
/home/mage/.bash_profile
/home/mage/.bashrc
[root@centos7 ~]# find /home -nouser -type f -ls
67110217 4 -rw-r--r-- 1 1003 1004 18 11月 20 2015 /home/mage/.bash_logout
67110218 4 -rw-r--r-- 1 1003 1004 193 11月 20 2015 /home/mage/.bash_profile
67110219 4 -rw-r--r-- 1 1003 1004 231 11月 20 2015 /home/mage/.bashrc
[root@centos7 ~]# find /home -nouser -a -type f -ls
67110217 4 -rw-r--r-- 1 1003 1004 18 11月 20 2015 /home/mage/.bash_logout
67110218 4 -rw-r--r-- 1 1003 1004 193 11月 20 2015 /home/mage/.bash_profile
67110219 4 -rw-r--r-- 1 1003 1004 231 11月 20 2015 /home/mage/.bashrc
# 或逻辑
[root@centos7 ~]# find /home -nouser -o -type f -ls
67332181 4 -rw-r--r-- 1 mageedu mageedu 18 11月 20 2015 /home/mageedu/.bash_logout
67332182 4 -rw-r--r-- 1 mageedu mageedu 193 11月 20 2015 /home/mageedu/.bash_profile
67332183 4 -rw-r--r-- 1 mageedu mageedu 231 11月 20 2015 /home/mageedu/.bashrc
67112341 4 -rw-r--r-- 1 centos centos 18 11月 20 2015 /home/centos/.bash_logout
67112342 4 -rw-r--r-- 1 centos centos 193 11月 20 2015 /home/centos/.bash_profile
67112343 4 -rw-r--r-- 1 centos centos 231 11月 20 2015 /home/centos/.bashrc
134353167 4 -rw------- 1 centos centos 11 2月 13 11:29 /home/centos/.cache/abrt/lastnotification
134361164 4 -rw------- 1 centos centos 2 2月 11 01:32 /home/centos/.cache/dconf/user
67112299 4 -rw------- 1 centos centos 1257 2月 11 16:00 /home/centos/.bash_history
67114412 4 -rw------- 1 centos centos 105 2月 11 01:28 /home/centos/.local/share/keyrings/login.keyring
67114411 0 -rw------- 1 centos centos 0 2月 11 01:28 /home/centos/.local/share/keyrings/user.keystore
67110210 4 -rw-r--r-- 1 arclinux arclinux 18 11月 20 2015 /home/arclinux/.bash_logout
67110211 4 -rw-r--r-- 1 arclinux arclinux 193 11月 20 2015 /home/arclinux/.bash_profile
67110212 4 -rw-r--r-- 1 arclinux arclinux 231 11月 20 2015 /home/arclinux/.bashrc
201391275 4 -rw------- 1 arclinux arclinux 11 2月 11 14:35 /home/arclinux/.cache/abrt/lastnotification
67110214 4 -rw------- 1 arclinux arclinux 217 2月 11 14:35 /home/arclinux/.bash_history
#非操作
[root@centos7 ~]# find /home -not -nouser
/home
/home/mageedu
/home/mageedu/.mozilla
/home/mageedu/.mozilla/extensions
/home/mageedu/.mozilla/plugins
/home/mageedu/.bash_logout
/home/mageedu/.bash_profile
/home/mageedu/.bashrc
/home/centos
/home/centos/.mozilla
/home/centos/.bash_history
/home/centos/.local
/home/centos/.local/share
/home/centos/.local/share/keyrings
/home/centos/.local/share/keyrings/login.keyring
/home/centos/.local/share/keyrings/user.keystore
/home/arclinux
/home/arclinux/.mozilla
/home/arclinux/.mozilla/extensions
/home/arclinux/.mozilla/plugins
/home/arclinux/.bash_logout
/home/arclinux/.bash_profile2.找出/tmp目录下,属主不是root,且文件名不以f开头的文件
# 组合条件查找时,括号要转义
find /tmp \( -not -user root -a -not -name 'f*' \) -ls
# 或者
find /tmp -not \( -user root -o -name 'f*' \) –ls
★根据文件大小来查找
☉格式:-size [+|-] #UNIT(单位)
☉常用单位:
☉区间:包含和不包含的关系
- #UNIT:(#-1, #] 如:6k 表示(5k,6k];
- -#UNIT:[0,#-1] 如:-6k 表示[0,5k];
- +#UNIT:(#,∞) 如:+6k 表示(6k,∞)
★根据时间戳查找
☉以“天”为单位;(只能是过去的时间轴)
◆-atime [+|-]#,
- #:[#,#+1) 如: 7 表示[7,8);表示从当前时刻起,过去第7天访问的文件(大于等于7小于第8天)
- +#:(#+1,∞] 如: +7 表示 [8,∞); 至少有7天没有被访问过了
- -#:[0,#) 如: -7 表示 [0,7) 表示7天内访问过的文件
◆-mtime
◆-ctime
☉以“分钟”为单位:
◆-amin
◆-mmin
◆-cmin
演示:
# 查找/etc目录下至少已经一周没有被访问过的文件,+7 为大于7天
[root@centos7 ~]# find /etc -atime +7 -ls
201367050 4 -rw-r--r-- 1 root root 3562 11月 6 16:36 /etc/keepalived/keepalived.conf.bak
201367053 4 -rw-r--r-- 1 root root 1555 11月 6 20:29 /etc/keepalived/keepalived.conf
134307865 4 -rw-r--r-- 1 root root 577 8月 6 2015 /etc/odbcinst.ini
1049 4 -rw-r--r-- 1 root root 1517 4月 20 2016 /etc/zabbix/zabbix_agentd.d/userparameter_mysql.conf
201366403 12 -rw-r--r-- 1 root root 10341 11月 13 10:44 /etc/zabbix/zabbix_agentd.conf.bak
201366425 12 -rw-r--r-- 1 root root 10575 11月 16 09:17 /etc/zabbix/zabbix_agentd.conf
201366446 16 -rw-r----- 1 root zabbix 15925 4月 20 2016 /etc/zabbix/zabbix_proxy.conf
134397465 20 -rw------- 1 root root 18861 11月 21 2015 /etc/snmp/snmpd.conf
134397466 4 -rw------- 1 root root 220 11月 21 2015 /etc/snmp/snmptrapd.conf
201643294 12 -rw-r--r-- 1 root root 10017 12月 12 15:02 /etc/php-fpm.d/www.conf
134489903 4 -rw-r--r-- 1 root root 2881 11月 21 2015 /etc/corosync/corosync.conf.example
134489904 4 -rw-r--r-- 1 root root 767 11月 21 2015 /etc/corosync/corosync.conf.example.udpu
134489905 4 -rw-r--r-- 1 root root 3278 11月 21 2015 /etc/corosync/corosync.xml.example
134489916 4 -rw-r--r-- 1 root root 3031 12月 7 16:41 /etc/corosync/corosync.conf
# 查找/etc目录下一天以内修改的文件
[root@centos7 ~]# find /etc -mtime -1 -ls
134299777 12 drwxr-xr-x 134 root root 8192 2月 20 10:45 /etc
134299808 4 -rw-r--r-- 1 root root 80 2月 20 09:08 /etc/resolv.conf
134353171 4 -rw-r--r-- 1 root root 1021 2月 20 10:36 /etc/group
134353172 4 ---------- 1 root root 824 2月 20 10:36 /etc/gshadow
134361159 4 -rw-r--r-- 1 root root 2467 2月 20 10:36 /etc/passwd
134353168 4 ---------- 1 root root 1431 2月 20 10:36 /etc/shadow
67109024 4 drwxr-xr-x 6 root root 4096 2月 20 2017 /etc/sysconfig
35649 4 drwxr-xr-x 2 root root 4096 2月 20 2017 /etc/sysconfig/network-scripts
11249 4 -rw-r--r-- 1 root root 14 2月 20 09:08 /etc/tuned/active_profile
[root@centos7 ~]# stat /etc/passwd
文件:"/etc/passwd"
大小:2467 块:8 IO 块:4096 普通文件
设备:802h/2050dInode:134361159 硬链接:1
权限:(0644/-rw-r--r--) Uid:( 0/ root) Gid:( 0/ root)
最近访问:2017-02-20 10:36:29.693358124 +0800
最近更改:2017-02-20 10:36:24.047176802 +0800
最近改动:2017-02-20 10:36:24.048176834 +0800
创建时间:-
★根据权限查找
☉格式:-perm [/|-] MODE
- MODE:精确权限匹配;
- /MODE:任何一类(u,g,o)对象的权限中的任何一位(r,w,x)符合条件既满足,9位权限之间为或关系;
- -MODE:每一类用户(u,g,o)的权限中的每一位(r,w,x)同时符合条件既满足,9位权限之间为与关系;
- 0 :表示不关注
示例:
- find -perm 755会匹配权限模式恰好是755的文件;
- 只要当任意人有写权限时,find -perm +222就会匹配;
- 只有当每个人都有写权限时,find -perm -222才会匹配;
- 只有当其它人(other)有写权限时,find -perm -002才会匹配
演示:
[root@centos7 ~]# mkdir /tmp/test
[root@centos7 ~]# cd /tmp/test
[root@centos7 test]# touch a b c d e f g
[root@centos7 test]# ll
总用量 0
-rw-r--r-- 1 root root 0 2月 20 13:41 a
-rw-r--r-- 1 root root 0 2月 20 13:41 b
-rw-r--r-- 1 root root 0 2月 20 13:41 c
-rw-r--r-- 1 root root 0 2月 20 13:41 d
-rw-r--r-- 1 root root 0 2月 20 13:41 e
-rw-r--r-- 1 root root 0 2月 20 13:41 f
-rw-r--r-- 1 root root 0 2月 20 13:41 g
[root@centos7 test]# chmod 640 a
[root@centos7 test]# chmod 666 b
[root@centos7 test]# chmod 440 c
[root@centos7 test]# chmod 775 d
[root@centos7 test]# chmod 777 e
[root@centos7 test]# ll
总用量 0
-rw-r----- 1 root root 0 2月 20 13:41 a
-rw-rw-rw- 1 root root 0 2月 20 13:41 b
-r--r----- 1 root root 0 2月 20 13:41 c
-rwxrwxr-x 1 root root 0 2月 20 13:41 d
-rwxrwxrwx 1 root root 0 2月 20 13:41 e
-rw-r--r-- 1 root root 0 2月 20 13:41 f
-rw-r--r-- 1 root root 0 2月 20 13:41 g
# 精确匹配
[root@centos7 test]# find /tmp/test/ -perm 644 -ls
134361180 0 -rw-r--r-- 1 root root 0 2月 20 13:41 /tmp/test/f
134361181 0 -rw-r--r-- 1 root root 0 2月 20 13:41 /tmp/test/g
# 任何一类用户的任何一位权限符合条件即可
[root@centos7 test]# find /tmp/test/ -perm /666 -ls
134361154 0 drwxr-xr-x 2 root root 62 2月 20 13:41 /tmp/test/
134361160 0 -rw-r----- 1 root root 0 2月 20 13:41 /tmp/test/a
134361161 0 -rw-rw-rw- 1 root root 0 2月 20 13:41 /tmp/test/b
134361166 0 -r--r----- 1 root root 0 2月 20 13:41 /tmp/test/c
134361178 0 -rwxrwxr-x 1 root root 0 2月 20 13:41 /tmp/test/d
134361179 0 -rwxrwxrwx 1 root root 0 2月 20 13:41 /tmp/test/e
134361180 0 -rw-r--r-- 1 root root 0 2月 20 13:41 /tmp/test/f
134361181 0 -rw-r--r-- 1 root root 0 2月 20 13:41 /tmp/test/g
# 查找至少有一类用户有写权限
[root@centos7 test]# find /tmp/test/ -perm /222 -ls
134361154 0 drwxr-xr-x 2 root root 62 2月 20 13:41 /tmp/test/
134361160 0 -rw-r----- 1 root root 0 2月 20 13:41 /tmp/test/a
134361161 0 -rw-rw-rw- 1 root root 0 2月 20 13:41 /tmp/test/b
134361178 0 -rwxrwxr-x 1 root root 0 2月 20 13:41 /tmp/test/d
134361179 0 -rwxrwxrwx 1 root root 0 2月 20 13:41 /tmp/test/e
134361180 0 -rw-r--r-- 1 root root 0 2月 20 13:41 /tmp/test/f
134361181 0 -rw-r--r-- 1 root root 0 2月 20 13:41 /tmp/test/g
# 查找其他用户有写权限的文件
[root@centos7 test]# find /tmp/test/ -perm /002 -ls
134361161 0 -rw-rw-rw- 1 root root 0 2月 20 13:41 /tmp/test/b
134361179 0 -rwxrwxrwx 1 root root 0 2月 20 13:41 /tmp/test/e
#==================================================================================
# 查找3类用户同时拥有写权限的文件
[root@centos7 test]# find /tmp/test/ -perm -222 -ls
134361161 0 -rw-rw-rw- 1 root root 0 2月 20 13:41 /tmp/test/b
134361179 0 -rwxrwxrwx 1 root root 0 2月 20 13:41 /tmp/test/e
# 表示至少有一类用户没有写权限
[root@centos7 test]# find /tmp/test/ -not -perm -222 -ls
134361154 0 drwxr-xr-x 2 root root 62 2月 20 13:41 /tmp/test/
134361160 0 -rw-r----- 1 root root 0 2月 20 13:41 /tmp/test/a
134361166 0 -r--r----- 1 root root 0 2月 20 13:41 /tmp/test/c
134361178 0 -rwxrwxr-x 1 root root 0 2月 20 13:41 /tmp/test/d
134361180 0 -rw-r--r-- 1 root root 0 2月 20 13:41 /tmp/test/f
134361181 0 -rw-r--r-- 1 root root 0 2月 20 13:41 /tmp/test/g
[root@centos7 test]# find /tmp/test/ -perm -002 -ls
134361161 0 -rw-rw-rw- 1 root root 0 2月 20 13:41 /tmp/test/b
134361179 0 -rwxrwxrwx 1 root root 0 2月 20 13:41 /tmp/test/e
# 表示属组和其他只要有写权限即可
[root@centos7 test]# find /tmp/test/ -perm /022 -ls
134361161 0 -rw-rw-rw- 1 root root 0 2月 20 13:41 /tmp/test/b
134361178 0 -rwxrwxr-x 1 root root 0 2月 20 13:41 /tmp/test/d
134361179 0 -rwxrwxrwx 1 root root 0 2月 20 13:41 /tmp/test/e
# 表示属组和其他都必须有写权限才满足
[root@centos7 test]# find /tmp/test/ -perm -022 -ls
134361161 0 -rw-rw-rw- 1 root root 0 2月 20 13:41 /tmp/test/b
134361179 0 -rwxrwxrwx 1 root root 0 2月 20 13:41 /tmp/test/e
3.处理动作
★处理动作:
- -print:输出至标准输出,默认的动作;
- -ls:类似于对查找到的文件执行“ls -l”命令,输出文件的详细信息;
- -delete:删除查找到的文件;(不建议使用);
- -fls /PATH/TO/SOMEFILE:查找到的所有文件的长格式信息保存至指定文件中(相当于重定向);
- -ok COMMAND {} \; :(固定格式) 对查找到的每个文件执行由COMMAND指定的命令;(对于每个文件执行命令之前,都会交互式要求用户确认(-exec 不用确认,直接操作)
- -exec COMMAND {} \; :对查找到的每个文件执行由COMMAND指定的命令
注意:
- find 传递查找到的文件至后面指定的命令时,查找到所有符合条件的文件一次性传递给后面的命令;
- 有些命令不能接受过多参数,此时命令执行可能会失败,下面方式可规避此问题:find | xargs COMMAND
演示:
[root@centos7 test]# chown arclinux.arclinux c d
[root@centos7 test]# ll
总用量 0
-rw-r----- 1 root root 0 2月 20 13:41 a
-rw-rw-rw- 1 root root 0 2月 20 13:41 b
-r--r----- 1 arclinux arclinux 0 2月 20 13:41 c
-rwxrwxr-x 1 arclinux arclinux 0 2月 20 13:41 d
-rwxrwxrwx 1 root root 0 2月 20 13:41 e
-rw-r--r-- 1 root root 0 2月 20 13:41 f
-rw-r--r-- 1 root root 0 2月 20 13:41 g
[root@centos7 test]# userdel -r arclinux
[root@centos7 test]# ll
总用量 0
-rw-r----- 1 root root 0 2月 20 13:41 a
-rw-rw-rw- 1 root root 0 2月 20 13:41 b
-r--r----- 1 1002 1002 0 2月 20 13:41 c
-rwxrwxr-x 1 1002 1002 0 2月 20 13:41 d
-rwxrwxrwx 1 root root 0 2月 20 13:41 e
-rw-r--r-- 1 root root 0 2月 20 13:41 f
-rw-r--r-- 1 root root 0 2月 20 13:41 g
[root@centos7 test]# find ./ -nouser
./c
./d
[root@centos7 test]# find ./ -nouser -a -nogroup -ls
134361166 0 -r--r----- 1 1002 1002 0 2月 20 13:41 ./c
134361178 0 -rwxrwxr-x 1 1002 1002 0 2月 20 13:41 ./d
[root@centos7 test]# find ./ -nouser -a -nogroup -ok chown root.root {} \; # 注意空格
< chown ... ./c > ? y
< chown ... ./d > ? y
[root@centos7 test]# ll
总用量 0
-rw-r----- 1 root root 0 2月 20 13:41 a
-rw-rw-rw- 1 root root 0 2月 20 13:41 b
-r--r----- 1 root root 0 2月 20 13:41 c
-rwxrwxr-x 1 root root 0 2月 20 13:41 d
-rwxrwxrwx 1 root root 0 2月 20 13:41 e
-rw-r--r-- 1 root root 0 2月 20 13:41 f
-rw-r--r-- 1 root root 0 2月 20 13:41 g2.-exec COMMAND {} \;
[root@centos7 test]# find ./ -perm /002
./b
./e
# {}: 用于引用查找到的文件名称自身
[root@centos7 test]# find ./ -perm /002 -exec mv {} {}.danger \;
[root@centos7 test]# ll
总用量 0
-rw-r----- 1 root root 0 2月 20 13:41 a
-rw-rw-rw- 1 root root 0 2月 20 13:41 b.danger
-r--r----- 1 root root 0 2月 20 13:41 c
-rwxrwxr-x 1 root root 0 2月 20 13:41 d
-rwxrwxrwx 1 root root 0 2月 20 13:41 e.danger
-rw-r--r-- 1 root root 0 2月 20 13:41 f
-rw-r--r-- 1 root root 0 2月 20 13:41 g
练习:
# 备份配置文件,添加.orig这个扩展名,一定要注意格式,后面加斜杠和分号
find -name “*.conf” -exec cp {} {}.org \;
# 提示删除存在时间超过3天以上的属主为joe的临时文件
find /tmp -ctime +3 -user joe -ok rm {} \;
# 在你的主目录中寻找可被其它用户写入的文件,然后去掉写权限
find ~ -perm /002 -exec chmod o-w {} \;
# 找到/data 目录下所有文件名以.sh后缀,且权限为644的普通文件,然后把权限改为755
find /data –type f -perm 644 -name “*.sh” –exec chmod 755 {} \;
|
|
|