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)
sunhaosunhao
上一篇 2017-06-11
下一篇 2017-06-11

相关推荐

  • linux防火墙介绍

    一、前言firewall(防火墙):工作在网络进入或者流包,进出的网络数据包进行一定的规则进行检查过滤系统。包括iptables和netfilter组件。iptables 是与 Linux 内核集成的 IP 信息包过滤系统。如果 Linux 系统连接到因特网或 LAN、服务器或连接 LAN 和因特网的代理服务器, 则该系统系统中更好地控制 IP 信息包过滤和…

    2017-04-30
  • SElinux 管理

    SELinux: Secure Enhanced Linux(安全强化的linux) SElinux安全上下文是由五个元素组成的: ①User:指示登录系统的用户类型,如root,user_u,system_u,多数本地进程都属于自由(unconfined)进程 ②Role:定义文件,进程和用户的用途:文件:object_r,进程和用户:syste…

    Linux干货 2016-09-26
  • ContOS 7 安装

    在虚拟机中安装ContOS 7,自定义4个分区
    虚拟机使用ISO镜像文件安装,网络使用桥接模式,内存大小设置为1224MB(安装桌面比较大)

    2018-03-13
  • 软件包管理

    rpm 与 yum 的用法 源代码:name-VERSION.tar.gz|bz2|xz VERSION: major.minor.release rpm包命名方式: name-VERSION-release.arch.rpm 例:bash-4.2.46-19.el7.x86_64.rpm VERSION: major.minor.release rele…

    Linux干货 2016-09-01
  • linux基础知识:计算机的组成及其功能

    摘要:
    1. 描述计算机的组成及其功能。
    2. 按系列罗列Linux的发行版,并描述不同发行版之间的联系与区别。
    3. 描述Linux的哲学思想,按照自己的理解对其解释性描述。
    4. 说明Linux系统上命令的使用格式;详细介绍ifconfig、echo、……等命令使用,配合相应实例阐述。
    5. 如何获取帮助信息,描述man文档章节划分。
    6. 罗列发行版基础目录名称命名法则及功用规定。

    2017-12-03
  • 马哥教育网络班22期+第1周课程练习

    1、描述计算机的组成及其功能。     计算机由五大功能部分组成(冯诺依曼体系):运算器、控制器、储存器、输入设备、输出设备,对应到具体的硬件设备如下:     CPU    :    包含运算器、控…

    Linux干货 2016-08-15

评论列表(1条)

  • 371987341
    371987341 2017-06-11 17:26

    双击评论 666