一、find搜索命令

  find命令用于按照指定条件查找文件。在系统工作中find命令是不可缺少的,我们一般要查找某个目录下的文件时,都是使用find命令查找,另外find命令也可以配合对查找出的文件进行删除或其他操作。他可以使用不同的文件特性作为查找的条件(文件名,大小,修改时间,权限等信息),匹配成功默认将信息显示在屏幕上。

格式:

find [查找路径] 查找条件 操作

常用参数:

-name name, -iname name : 文件名称符合 name 的文件。iname 会忽略大小写
-perm 匹配权限(mode完全匹配,-mode为包含即可)
  -perm 444 #查找文件权限
  -perm -444 # -表示并且;查找文件权限中u位有r权限,并且g位有r权限,并且o位有r权限的文件
  -perm /444 # /表示或者;查找文件权限中u位有r权限,或者g位有r权限,或者o位有r权限的文件
  -perm /777 # 777=rwx rwx rwx 即9个条件中满足任意一个即可
-user    匹配所有者
-nouser 匹配无所有者的文件
-group 匹配所有组
-nogroup 匹配无所有组的文件
-mtime -n,+n 匹配修改内容的时间(-n指n天以内,+n指n天以前)
-atime -n,+n 匹配访问文件的时间(-n指n天以内,+n指n天以前)
-ctime -n,+n 匹配修改文件权限时间(-n指n天以内,+n指n天以前)
-amin n : 在过去 n 分钟内被读取过
-cmin n : 在过去 n 分钟内被修改过
-size n : 匹配文件的大小(50KB查找50KB的文件,+50KB位查找超过50KB的文件,-50KB是查找小于50KB的文件)。文件大小 是 n 单位,b 代表 512 位元组的区块,c 表示字元数,k 表示 kilo bytes,w 是二个位元组。
--type c 指定类型,文件类型是 c 的文件。
d: 目录文件
c: 字符设备
b: 设备文件
p: 管道
f: 一般文件
l: 符号链接文件
-maxdepth : 查找最大深度,即目录层次
-mindepth : 查找最小深度
-newer f1 !f2 匹配比f1新但比f2旧的文件
-prune 忽略某个目录
-exec command {} \; 后接执行的命令
-ok command {} \; 后接执行命令,执行之前会进行询问是否执行该命令
-a  且(要同时满足)
-o  或(只需满足其中一个条件即可)

实例:

1)查找当前目录下最近20天之内更新的文件

# find . -ctime -20

2)查找/var/log目录中更改时间在7日以前的普通文件,并在删除之前询问

# find /var/log -type f -mtime +7 -ok rm {} \;

3)查找前目录中文件属主具有读、写权限,并且文件所属组的用户和其他用户具有读权限的文件(只匹配644)

# find . -type f -perm 644 -exec ls -l {} \;

4)模糊匹配644权限(包含644即可)

# find . -type f -perm -644 -exec ls -l {} \;

5)查找系统中所有文件长度为0的普通文件,并列出它们的完整路径

# find . -type f -size 0 -exec ls -l {} \;

6)-exec执行命令

# 删除没有属主的用户
# find / -nouser -exec rm -rf {} \; # 删除查找的文件
# find /home -name "*log" | xargs rm -rf {} \;

7)-a和-o参数使用

# 找到所有权限是644的普通文件
# find /usr/local/ -perm 644 -a -type f | head # 找到以a开头或以a结尾的普通文件
# find . -name "a*" -o -name "*a" -type f

PS:使用-a的话则需要同时满足

8)查询属主、属组

# 查询属主为root的文件
# find /etc/ -user root -type f | head -n 3 | xargs ls -l; # 查询属组为root的文件
# find /etc/ -group root -type f | head -n 3 | xargs ls -l; # 查询没有属主、属组的文件
# find /etc/ -nouser -type f | head -n 3 | xargs ls -l;
# find /etc/ -nogroup | head -n 3 | xargs ls -l;

9)在整个文件系统中找出归属于thy用户的文件复到指定目录

# 模拟一个用户,将备份的passwd文件的属主和属组改为thy
# useradd thy
# chown thy.thy /tmp/passwd
# ll /tmp/passwd
-rw-r--r-- 1 thy thy 1708 Nov 4 09:56 /tmp/passwd # 使用find命令将属主为thy的文件复制到指定目录
# find / -user thy -type f -exec cp -a {} /tmp/thy/ \;

10)综合案例:按文件所有者和所有组查找

# 创建文件
[root@localhost ~]# cd /tmp/test
[root@localhost test]# touch file{1..5}
[root@localhost test]# ls
file1 file2 file3 file4 file5 # 实时监听这个目录(可以另外开一个shell窗口)
[root@VM_0_10_centos ~]# watch -n 1 ls -l /tmp/test/

# 添加用户
[root@VM_0_10_centos test]# useradd teacher
[root@VM_0_10_centos test]# useradd student
[root@VM_0_10_centos test]# id teacher
uid=1008(teacher) gid=1011(teacher) groups=1011(teacher)
[root@VM_0_10_centos test]# id student
uid=1009(student) gid=1012(student) groups=1012(student)
# 修改用户主和属组
[root@VM_0_10_centos test]# chown teacher.teacher /tmp/test/file1
# chown和chgrp修改属组效果是一样的
[root@VM_0_10_centos test]# chown .teacher /tmp/test/file2
[root@VM_0_10_centos test]# chgrp student /tmp/test/file2
[root@VM_0_10_centos test]# chown teacher.student /tmp/test/file3

# 按文件所有者查找
[root@VM_0_10_centos test]# find / -user teacher -type f
/home/teacher/.bashrc
/home/teacher/.bash_logout
/home/teacher/.bash_profile
/var/spool/mail/teacher
/tmp/test/file3
/tmp/test/file1 # 按文件所有组查找
[root@VM_0_10_centos test]# find / -group teacher -xtype f
/home/teacher/.bashrc
/home/teacher/.bash_logout
/home/teacher/.bash_profile
/tmp/test/file1
[root@VM_0_10_centos test]# find / -group student -type f
/home/student/.bashrc
/home/student/.bash_logout
/home/student/.bash_profile
/tmp/test/file2
/tmp/test/file3 # 查找所有者为root,所属组为student的文件(默认表示且关系)
[root@VM_0_10_centos test]# find / -user root -group student -type f
/tmp/test/file2
等价于
[root@VM_0_10_centos test]# find / -user root -a -group student -type f
/tmp/test/file2 # 查找所有者为teacher或所属组为student的文件(表示或关系)
[root@VM_0_10_centos test]# find / -user teacher -o -group student -type f
/home/student/.bashrc
/home/student/.bash_logout
/home/student/.bash_profile
/home/teacher
/home/teacher/.bashrc
/home/teacher/.bash_logout
/home/teacher/.bash_profile
/var/spool/mail/teacher
/tmp/test/file2
/tmp/test/file3
/tmp/test/file1 # 查找所属者不是student的用户
[root@VM_0_10_centos test]# find /tmp/ -not -user root -type f -exec ls -l {} \;
-rw-r--r-- 1 thy thy 1708 Nov 4 09:56 /tmp/passwd
-rw-r--r-- 1 teacher student 0 Nov 5 11:16 /tmp/test/file3
-rw-r--r-- 1 teacher teacher 0 Nov 5 11:16 /tmp/test/file1

11)按文件所在深度(层次)查找

# 最大深度
[root@VM_0_10_centos test]# find /etc/ -maxdepth 2 | head -n 5
/etc/
/etc/group
/etc/rc6.d
/etc/audit
/etc/audit/audit.rules # 最小深度
[root@VM_0_10_centos test]# find /etc/ -maxdepth 1 | head -n 5
/etc/
/etc/group
/etc/rc6.d
/etc/audit
/etc/mailcap # 查找/etc目录下最少层次为1最多层次为2的以.conf结尾的文件(这里将最大和最小深度的位置对换就会查不出结果)
[root@VM_0_10_centos test]# find /etc/ -mindepth 1 -maxdepth 2 -name *.conf | head -n 3
/etc/audit/auditd.conf
/etc/sysctl.conf
/etc/depmod.d/kvdo.conf

12)对查找到的文件执行某些动作

# 对查询到的文件进行分权限操作
[root@VM_0_10_centos test]# find /tmp/test/ -perm 644 -exec chmod g+w {} \;
[root@VM_0_10_centos test]# ll
total 0
-rw-rw-r-- 1 teacher teacher 0 Nov 5 11:16 file1
-rw-rw-r-- 1 root student 0 Nov 5 11:16 file2 # 对查询的文件进行备份
[root@VM_0_10_centos test]# find /tmp/test/ -type f -exec cp {} /tmp/test/test/ \;
cp: ‘/tmp/test/test/file2’ and ‘/tmp/test/test/file2’ are the same file
cp: ‘/tmp/test/test/file3’ and ‘/tmp/test/test/file3’ are the same file
cp: ‘/tmp/test/test/file5’ and ‘/tmp/test/test/file5’ are the same file

13)find查询指定时间修改时间内的数据,排除要查询的文件,rm删除查询出来的文件

# find . -name "*.jpg" ! -name "old03*" ! -name "old06*" -mtime -1 -exec rm -rf {} \;

参考博客

https://blog.csdn.net/devwang_com/article/details/52457591

【文本处理命令】之find搜索命令的更多相关文章

  1. Linux经常使用命令002之搜索命令locate、whereis、which、find、grep

    Linux经常使用命令002之搜索命令locate.whereis.which.find.grep -20150811 经常使用搜索命令 -------文件搜索命令---------- -->l ...

  2. Linux常用命令3 文件搜索命令

    文件搜索非常占用资源,所以尽量不要使用这个命令 避免少用该命令最好的方式是设置好文件夹结构,文件不要乱放 1.文件搜索命令:find 命令名称:find 所在路径:/bin/find 执行权限:所有用 ...

  3. Shell 命令--文件创建、搜索命令--总结自《Linux Shell 脚本攻略》

    (一)文件创建命令 1.touch命令 比如:touch abc命令在本地文件夹中创建了一个名为abc的空文件 2.cp命令 cp命令同意我们把一个文件的内容拷贝到同名或不同名的文件里,复制得到的文件 ...

  4. Linux学习笔记(5)Linux常用命令之文件搜索命令

    (1)find find命令用于文件搜索,所在路径/bin/find,其语法格式为: find [搜索范围] [匹配条件] 1)-name:根据文件名搜索,如搜索/etc下的init文件 [root@ ...

  5. linux常用命令:3文件搜索命令

    文件搜索命令 1. 命令名:find 命令所在路径:/bin/find 执行权限:所有用户 语法:find  [搜索范围]  [匹配条件] 功能描述:文件搜索 文件搜索类型 通过文件名搜索 -name ...

  6. Linux常用命令之文件搜索命令

    目录 1.最强大的搜索命令:find2.在文件资料库中查找文件命令:locate 一.根据 文件或目录名称 搜索 二.根据 文件大小 搜索 三.根据 所有者和所属组 搜索 四.根据 时间属性 搜索 五 ...

  7. Linux系列教程(六)——Linux常用命令之文件搜索命令

    前一篇博客我们讲解了Linux链接命令和权限管理命令, 通过 ln -s  链接名 表示创建软链接,不加-s表示创建硬链接:还有三个更改权限的命令,chmod命令可以更改文件或目录权限,chown命令 ...

  8. linux 命令及配置文件搜索命令which、whereis

    which /usr/bin/which 搜索命令所在目录及别名信息 which lsalias ls='ls --color=auto'/usr/bin/ls which rmalias rm='r ...

  9. linux简单命令2---文件搜索命令

    1:文件搜索命令:locate 文件名搜索速度快,缺点不能复杂的搜索.在数据库(/var/lib/mlocate)查找.它是一天一更新.可以强制更新数据库:updatedb 2:搜索命令的命令:whe ...

  10. Linux命令大全之搜索命令

    文件搜索命令(只能搜索文件) locate 文件名 在后台数据库中按文件名搜索,搜索速度快      /var/lib/mlocate(locate文件数据库)    这个数据库默认一天更新一次,强制 ...

随机推荐

  1. 并发编程~~~协程~~~greenlet模块, gevent模块

    一 协程 1. 协程: 单线程下的并发,又称微线程,纤程.协程是一种用户态的轻量级线程,即协程是由用户程序自己控制调度的. 并发真正的核心: 切换并且保持状态. 开启协程并发的执行,自己的程序把控着C ...

  2. Mysql增量备份之Mysqldump&Mylvmbackup

    简单介绍 备份类型 备份方式 热备份:备份期间不需要服务停机,业务不受影响: 温备份:备份期间仅允许读的请求: 冷备份:备份期间需要关闭Mysql服务或读写请求都不受影响: 完全备份:full bac ...

  3. BOM的初级理解

    1.什么是BOM,BOm有什么作用? BOM和DOM.ES是JavaScript的重要三个组成部分: 其中BOM是专门操作浏览器的API,其实他就是一种兼容性问题,这其中问题比较大就是IE浏览器,谁叫 ...

  4. IT兄弟连 HTML5教程 CSS3揭秘 CSS规则的组成

    CSS和HTML一样都是由W3C制定的标准,本章中介绍的特性和功能还是来源于CSS1和CSS2(CSS2是根据CSS1扩展的).W3C也有新的版本更新,称为CSS3.虽然浏览器已经准备开始实现CSS3 ...

  5. 看完这篇文章,我奶奶都知道什么是JVM中的内存模型与垃圾回收!

    扩展阅读:JVM从入门开始深入每一个底层细节 六.内存模型 6.1.内存模型与运行时数据区 Java虚拟机在执行Java程序的过程中会把它所管理的内存划分为若干不同数据区域. Java内存模型的主要目 ...

  6. HTML 本地存储

    HTML 本地存储:优于 cookies. 什么是 HTML 本地存储? 通过本地存储(Local Storage),web 应用程序能够在用户浏览器中对数据进行本地的存储. 在 HTML5 之前,应 ...

  7. 【转】Kotlin的inline内联函数

    原文链接:https://blog.csdn.net/Jaden_hool/article/details/78437947 方法调用流程 调用一个方法是一个压栈和出栈的过程,调用方法时将栈针压入方法 ...

  8. Thymeleaf常用语法:模板注释

    Thymeleaf模板注释分为标准HTML/XML注释.解析层注释.原型注释三种. 一.注释说明 1.标准HTML/XML注释 直接通过浏览器打开,不显示,Thymeleaf模板引擎解析也不处理,但查 ...

  9. 2019 DevOps 必备面试题——持续集成篇

    原文地址:https://medium.com/edureka/devops-interview-questions-e91a4e6ecbf3 原文作者:Saurabh Kulshrestha 翻译君 ...

  10. WPF-自定义控件引用外部样式+转换器

    引用单个外部样式 <UserControl.Resources> <ResourceDictionary Source="pack://application:,,,/XM ...