find 的使用及练习

find是个使用频率比较高的命令。常常用它在系统特定目录下,查找具有某种特征【名字类型属主权限等】的文件。
find命令的格式: find  [-path ..] -options [-print -exec -ok]

path:要查找的目录路径。

~ 表示$HOME目录

. 表示当前目录

/ 表示根目录

-print :表示将结果输出到标准输出
-exec :对匹配的文件执行该参数所给出的shell命令。形式为 command  {} \; ,注意{}与\; 之间有空格
-ok :与-exec作用相同,区别在于,在执行命令之前,都会给出提示,让用户确认是否执行

options常用的有下选项:

-name 按照名字查找

-perm 安装权限查找

-prune 不再当前指定的目录下查找

-user 文件属主来查找

-group 所属组来查找

-nogroup 查找无有效所属组的文件

-nouser 查找无有效属主的文件

-type 按照文件类型查找

下面通过一些简单的例子来介绍下find的常规用法:

1、按名字查找

在当前目录及子目录中,查找大写字母开头的txt文件

  1. [root@localhost ~]# find . -name ‘[A-Z]*.txt’ -print  

在/etc及其子目录中,查找host开头的文件

  1. [root@localhost ~]# find /etc -name ‘host*’ -print   

在$HOME目录及其子目录中,查找所有文件


  1. [root@localhost ~]# find ~ -name ‘*’ -print  

在当前目录及子目录中,查找不是out开头的txt文件


  1. [root@localhost .code]# find . -name “out*” -prune -o -name “*.txt” -print    

2、按目录查找

在当前目录除aa之外的子目录内搜索 txt文件


  1. [root@localhost .code]# find . -path “./aa” -prune -o -name “*.txt” -print  

在当前目录及除aa和bb之外的子目录中查找txt文件


  1. [root@localhost .code]# find . −path”./aa”−o−path”./bb” -prune -o -name “*.txt” -print     

在当前目录,不再子目录中,查找txt文件


  1. [root@localhost .code]# find .  ! -name “.” -type d -prune -o -type f -name “*.txt” -print  

3、按权限查找

在当前目录及子目录中,查找属主具有读写执行,其他具有读执行权限的文件


  1. [root@localhost ~]# find . -perm 755 -print  

4、按类型查找

在当前目录及子目录下,查找符号链接文件


  1. [root@localhost .code]# find . -type l -print  

5、按属主及属组

查找属主是www的文件


  1. [root@localhost .code]# find / -user www -type f -print  

查找属主被删除的文件


  1. [root@localhost .code]# find /  -nouser -type f -print   

查找组mysql的文件


  1. [root@localhost .code]# find /  -group mysql -type f  -print  

查找用户组被删掉的文件


  1. [root@localhost .code]# find /  -nogroup -type f  -print     

6、按时间查找

查找2天内被更改过的文件


  1. [root@localhost .code]# find . -mtime -2 -type f -print   

查找2天前被更改过的文件


  1. [root@localhost .code]# find . -mtime +2 -type f -print  

查找一天内被访问的文件


  1. [root@localhost .code]# find . -atime -1 -type f -print   

查找一天前被访问的文件


  1. [root@localhost .code]# find . -atime +1 -type f -print   


查找一天内状态被改变的文件


  1. [root@localhost .code]# find . -ctime -1 -type f -print   

查找一天前状态被改变的文件


  1. [root@localhost .code]# find . -ctime +1 -type f -print  

查找10分钟以前状态被改变的文件


  1. [root@localhost .code]# find . -cmin +10 -type f -print   

7、按文件新旧

查找比aa.txt新的文件


  1. [root@localhost .code]# find . -newer “aa.txt”  -type f -print   

查找比aa.txt旧的文件


  1. [root@localhost .code]# find . ! -newer “aa.txt”  -type f -print    

查找比aa.txt新,比bb.txt旧的文件


  1. [root@localhost .code]# find . -newer ‘aa.txt’ ! -newer ‘bb.txt’ -type f -print   

8、按大小查找

查找超过1M的文件


  1. [root@localhost .code]# find / -size +1M -type f -print    

查找等于6字节的文件


  1. [root@localhost .code]# find . -size 6c -print  

查找小于32k的文件


  1. [root@localhost .code]# find . -size -32k -print  

9、执行命令

查找del.txt并删除,删除前提示确认


  1. [root@localhost .code]# find . -name ‘del.txt’ -ok rm {} \;  

查找aa.txt 并备份为aa.txt.bak


  1. [root@localhost .code]# find . -name ‘aa.txt’ -exec cp {} {}.bak \;   

查找aa.txt 归档压缩为aa.txt.tar.gz 并删除aa.txt


  1. find . -name “aa.txt” -type f -exec tar -zcvf {}.tar.gz {} \; -exec rm -rf {} \; > /dev/null  

原创文章,作者:sunhao,如若转载,请注明出处:http://www.178linux.com/77897

(0)
上一篇 2017-06-11 15:52
下一篇 2017-06-11 17:23

相关推荐

  • 8.5_Linux文本处理工具和grep之正则表达式的使用

    Linux文本处理工具使用 文件查看工具的使用 cat 命令的使用  此命令一般用于查看文件内容比较多 NAME        cat – concatenate files and print on the&…

    Linux干货 2016-08-08
  • 2016.08.04学习笔记

    文本管理工具     1.cat:查看文件,读取键盘输入         -A:查看文件所有信息         -n:显示行数   &nb…

    Linux干货 2016-08-08
  • N25第四周博客作业

    第四周博客作业 1、复制/etc/skel目录为/home/tuser1,要求/home/tuser1及其内部文件的属组和其它用户均没有任何访问权限。 2、编辑/etc/group文件,添加组hadoop。      3、手动编辑/etc/passwd文件新增一行,添加用户hadoop,其基本组ID为hadoop组的i…

    Linux干货 2016-12-19
  • 用户及相关权限命令

    2018-03-26

    2018-03-26
  • 私有IP网子网掩码划分原则及计算方法

    私有IP网子网掩码划分原则及计算方法 最好记下的二进制转换十进制 00000000 0 00000001 1 00000010 2 00000100 4 00001000 8 00010000 16 00100000 32 01000000 64 10000000 128 11000000 192 11100000 224 11110000 240 1111…

    Linux干货 2017-05-02
  • 系统启动和内核管理(一)

    系统启动和内核管理(一)   (以CentOS5/6为例) 一、Linux的组成: 1、Linux:kernel+rootfs kernel:进程管理、内存管理、网络管理、驱动程序、文件系统、安全功能。 rootfs:程序和glibc 库:函数集合,function,调用接口(头文件负责描述); 过程调用,procedure,无返回值; 函数调用,…

    Linux干货 2016-09-10

评论列表(1条)

  • 371987341
    371987341 2017-06-11 17:26

    双击评论 666