1、了解find的含义  

  1. find:  

  2.   实时查找  

  3.   精确  

  4.   支持众多查找标准  

  5.   遍历指定目录中的所有文件查找,速度慢;  

  6. find 查找路径 查找标准 查找到以后的处理动作  

  7. 查找路径:默认为当前目录(查找路径可省略)  

  8. 查找标准: 默认为指定路径下的所有文件  

  9. 处理运作:默认为显示  

2、find的匹配标准:

  1.   -name'FILENAME':对文件名作精确匹配  

  2.   eg:find /etc -name'passwd'

  3.   文件名通配:  

  4.   * :任意长度的任意字符  

  5.   ?:匹配其前的字符可有可无  

  6.   []:匹配指定范围内的任意单个字符  

  7.   eg:find /etc -name'passwd*' 查找以passwd开头的文件  

  8.   eg:find /etc -name'*passwd' 查找以passwd结尾的文件  

  9.   eg:find /etc -name'*passwd*' 查找包含passwd字符串的文件  

  10.   -iname 'FILENAME': 文件名匹配时不区分大小写  

  11.   -regex PATTERN : 基于正则表达式进行文件匹配  

  12.   -user USERNAME : 根据属主查找  

  13.   eg:find /tmp -user student  

  14.   -group GROUPNAME: 根据属组查找  

  15.   -uid UID:根据UID查找  

  16.   eg:find /tmp -uid 2003  

  17.   -gid GID:根据GID查找  

  18.   -nouser : 查找没有属主的文件  

  19.   -nogroup : 查找没有属组的文件  

  20. -type(根据文件类型查找)  

  21.     f: 普通文件  

  22.     d: 目录  

  23.     c:字符设备  

  24.     b:块设备  

  25.     l: 链接文件  

  26.     p: 管道设备  

  27.     s: 套接字设备  

  28.     eg:find /tmp -type s  

  29. -size[+|-](根据文件大小查找)  

  30.     #k:  (默认单位是字节)  

  31.     eg:+10k 意思是大于10k   -10k 意思是小于10k  

  32.     #M   

  33.     eg:find /etc -size 1M   找到etc下大小为1M的文件(所有小于1M的也会显示)  

  34.     eg:find /etc -size 10k -ls  可显示出文件的大小  

  35.     #G  

3、find的组合条件

  1.   -a:与逻辑,要求两个条件同时满足才显示  

  2.   eg:find /tmp -nouser -a -type d 查找tmp文件下没有属主并且没有目录的文件  

  3.   eg:find /tmp -nouser -a -type d -ls 查找tmp文件下没有属主并且没有目录的文件个数  

  4.   -o:或逻辑,满足一个条件即可  

  5.   eg:find /tmp -nouser -a -type d 查找tmp文件下没有属主或者没有目录的文件  

  6.   -not:非目录类型的文件  

  7.   eg:find /tmp -not -type d  查找tmp文件下非目录类型的文件  

  8.   eg:find ./ -not -user user1 -o -not -type d 查找当前文件下属主不是user1或空目录的文件  

  9.   eg:find ./ -not \(user user1 -o -not -type d \) 查找当前文件下属主不是user1或空目录的文件  

  10.    /tmp目录,不是目录,并且还不能套接字类型的文件  

  11.    /tmp/test目录下,属主不是user1,也不是user2的文件  

  12.    find /test -not -user user1 -a -not -user user2  

  13.    find /test -not \ ( -user user1 -o -user -user2 \)   

4、根据文件的时间戳查找 

  1.   -mtime [+|-]#:修改了的时间(单位为天)  

  2.   -ctime [+|-]#:改变的时间  

  3.   -atime [+|-]#: 访问的时间  

  4.  eg:-atime 5 ; 离现在刚好在5天访问  

  5.      -atime +5; 至少5天没访问了  

  6.      -atime -5; 最近5天内访问了  

  7.   -mmin [+|-] #: 修改了的时间(单位为分钟)  

  8.   -cmin [+|-] #;改变的时间  

  9.   -amin [+|-] #: 访问的时间  

  10.   eg:find ./ -amin -5  最近5分钟之内访问过的文件  

  11.   eg:find ./ -amin +5  离现在5分钟之前访问过的文件  

  12.   eg:find ./ -amin 5   离现在刚好5分钟的那一刻访问过的文件  

  13.   eg:find /tmp -atime +7  离现在至少7天未访问过的文件  

  14.   eg:find /tmp -atime +30 离现在至少一个月未访问过的文件  

  15. -perm MODE:精确匹配(根据文件权限查找)  

  16.   /MODE:任意一位匹配即满足条件  

  17.    eg:find ./ -perm 644  查找当前文件下为644的文件(直接跟权限作精确匹配)  

  18.    eg:find ./ -perm /644  只要有一位匹配即可(644意为属主是读写644:rw-r--r--) 

  19.   -MODE: 文件权限能完全包含此MODE时才符合条件  

  20.    eg:find ./ -perm -001  查找其它用户有执行权限  

  21.    eg:find ./ -perm -022  组有写权限,其他人也有写权限  

  22.    eg:find ./ -perm /022  组有写权限,或其他人有写权限  

  23.    eg: find ./ -perm -007  其他用户能读能写能执行  

  24.  动作:  

  25.     -print:显示  

  26.     -ls:类似ls-l的形式显示每一个文件的详细  

  27.     -ok COMMAND {} \;  :每一次操作都需要用户确认  

  28.     eg:find ./ -type d -ok chmod +x {} \;  让他的属主属组都能够执行权限  

  29.     -exec COMMAND {} \;  :  

  30.     eg:find ./ -perm -006 -exec chmod o-w {} \; 把其他用户的写权限去掉  

  31.     eg:find ./ -perm -020 -exec mv {} {}.new \; 查找到属组有写权限的文件,把他原来的文件名后面加上.new  

  32.     只要引用文件的文件名就使用{}  

  33.     找到目录下文件名是以.sh结尾的,所有用户都具有执行权限,把其他用户的权限去掉  

  34.     find ./ -name"*.sh" -a -perm -111 -exec chmod o-x {} \;  

  35.     xargs 从标准输入接收进来命令并执行  

  36.     eg:find /etc/ -size +1M | xargs echo {} >> /tmp/etc.largefiles     

5、find命令的实例分析

  1. 1、查找/var目录下属主为root并且属组为mail的所有文件;  

  2. find /var -user root -group mail  

  3. 2、查找/usr目录下不属于root,bin,或student的文件;  

  4. find /usr -not -user root -a -not -user bin -a -not -user student  

  5. 或find /usr -not \( -user root -o -user bin -o -user student \)  

  6. 3、查找/etc目录下最近一周内内容修改过且不属于root及student用户的文件;  

  7. find /etc -mtime -7 -not \ ( -user root -o -user student \)  

  8. 或find /etc -mtime -7 -not -user root -a -not -user student  

  9. 4、查找当前系统上没有属主或属组且最近1天内曾被访问过的文件,并将其属主属组均修改为root;  

  10. find / \( -nouser -o -nogroup \) -a -atime -1 -exec chown root:root {} \;   

  11. 5、查找/etc目录下大于1M的文件,并将其文件名写入/tmp/etc.largefiles文件中;  

  12. find /etc -size +1M >> /tmp/etc.largefiles  

6、特殊权限  

  1.   passwd:s  

  2. SUID:运行某程序时,相应进程的属主是程序文件自身的属主,而不是启动者;    

  3.      chmod u+s FILE  

  4.      chmod u-s FILE  

  5.      如果FILE本身原来就有执行权限,则SUID显示为s,否则显示为S  

  6. SGID:运行某程序时,相应进程的属组是程序文件自身的属组,而不是启动者所属的基本组;  

  7.      chmod g+s FILE  : 加上这个位  

  8.      chmod g-s FILE  : 减去这个位  

  9.      develop team,hadoop,hbase,hive  

  10.      /tmp/project/  

  11.        develop  

  12. Sticky:在一个公共目录,每个都可以创建文件,删除自己的文件,但不能删除别人的文件;  

  13.      chmod o+t DIR   

  14.      chmod o-t DIR  

  15.      000:表示没有SUID、SGID、Sticky  

  16.      001:表示没有SUID、SGID 有Sticky  

  17.      ...       

  18.      110:表示有SUID、SGID 没有Sticky  

  19.      111:表示有SUID、SGID、Sticky  

  20.      chmod 1755 /backup/test 表示此文件是Sticky755的文件  

  21.      chmod 3755 /backup/test 表示此文件是SGID、Sticky755的文件  

7、find的高级实例  

  1. 写一个脚本,显示当前系统上shell为-s指定类型的用户,并统计其用户总数。  

  2. -s选项后面跟的参数必须是/etc/shells文件中存在的shell类型,否则不执行  

  3. 此脚本。另外,此脚本还可以接受--help选项,以显示帮助信息。脚本执行形 

  4. 如:./showshells.sh -s bash  

  5. 显示结果形如:  

  6. BASH,3users,they are:  

  7. root,redhat,gentoo  

  8. 脚本内容:  

  9. #!/bin/bash  

  10. #  

  11. if [ $1 == '-s' ]; then      (如果参数1等于-s)  

  12.   ! grep "${2}$" /etc/shells &> /dev/null && echo "Invalid shell." && exit 7 ( 如果!grep "${2}$" /etc/shells 执行结果为真则继续执行 echo "Invalid shell.",否则停止执行结果把其内容追加到/dev/null中)  

  13. elif [ $1 == '--help' ];then  (如果参数1等于--help)  

  14.   echo "Usage: showshells.sh -s SHELL | --help"(显示参数1执行后的结果)  

  15.   exit 0  

  16. else

  17.   echo "Unknown Options."    (显示未知用户的项)  

  18.   exit 8  

  19. fi  

  20. NUMOFUSER=`grep "${2}$" /etc/passwd | wc -l`   (显示用户总数)  

  21. SHELLUSERS=`grep "${2}$" /etc/passwd | cut -d: -f1`  (显示符合条件的用户)  

  22. SHELLUSERS=`echo $SHELLUSERS | sed 's@[[:space:]]@,@g'`( 显示shell用户并把各个用户数之间的空格换成",")  

  23.   echo -e "$2, $NUMOFUSER users, they are: \n$SHELLUSERS"    (显示最终的格式 )