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 15:52
下一篇 2017-06-11 17:23

相关推荐

  • 第七周 练习

    1. 请描述网桥、集线器、二层交换机、三层交换机、路由器的功能、使用场景与区别。 集线器:集线器的主要功能是对接收到的信号进行再生整形放大,以扩大网络的传输距离网桥:是早期的两端口二层网络设备,用来连接不同网段。网桥的两个端口分别有一条独立的交换信道,      不是共享一条背板总线,可隔…

    Linux干货 2016-12-13
  • 正则表达式

    ##**正则表达式**– 正则表达式是文本处理极为重要的技术,用它可以对字符串按照某种规则进行检索、替换– 分类:– BRE:基本正则表达式,grep sed vi等软件支持。vim有扩展– ERE:扩展正则表达式,egrep(grep -E) sed -r等– PCRE:几乎所有高级语言都是PCR…

    Linux干货 2017-11-07
  • CentOS 7 忘记root密码的解决之道

    1、启动时任意键暂停启动,会出现如下界面: 2、按e键进入编辑模式,将光标移动linux16开始的行,添加内核参数 rd.break 3、按 ctrl+x 组合键启动 4、查看各分区和文件系统的挂载情况,需要将 / 的模式改为 rw 5、当前系统上的 / 是在光盘上的,切换至挂载目录下的 / ,才可以修改密码 6、在 / 目录下创建autorela…

    Linux干货 2016-09-26
  • ftp服务器介绍与配置

    一、 ftp服务,File Transfer Protocal 文件传输协议      1. ftp基于tcp协议,C/S架构          Server: wu-ftpd, proftpd ,pureftp,vsf…

    Linux干货 2015-06-21
  • sed和awk的用例及pam安全措施-第十五周

    总结sed和awk的详细用法; sed用法 sed(Stream EDitor)是一款流编辑器,用来对文本进行过滤与替换操作。其原理是:通过文件或管道读取文件内容,但是sed默认并不直接修改源文件,而是一次仅读取文件的一行至模式空间(pattern space)根据sed指令进行编辑并输出结果后清除模式空间,即所有的操作都是在模式空间中进行的。 语法格式 s…

    2017-06-05
  • test

    test

    Linux干货 2017-11-26

评论列表(1条)

  • 371987341
    371987341 2017-06-11 17:26

    双击评论 666