一、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. Dockerfile优化

    总结: 1.编写.dockerignore文件 2.容器只运行单个应用 3.将多个RUN指令合并为一个 4.基础镜像的标签不要用latest 5.每个RUN指令后删除多余文件 6.选择合适的基础镜像( ...

  2. Java.awt.geom.AffineTransform 的使用

    https://docs.oracle.com/javase/8/docs/api/java/awt/geom/AffineTransform.html http://www.cjsdn.net/Do ...

  3. quarter软件的破解

    链接;http://www.openedv.com/forum.php?mod=viewthread&tid=275857&extra=page%3D1 这个是正点原子提供的破解方法, ...

  4. [专题总结]初探插头dp

    彻彻底底写到自闭的一个专题. 就是大型分类讨论,压行+宏定义很有优势. 常用滚动数组+哈希表+位运算.当然还有轮廓线. Formula 1: 经过所有格子的哈密顿回路数. 每个非障碍点必须有且仅有2个 ...

  5. DBCC TRACEON - 跟踪标志 (Transact-SQL)

    跟踪标志用于设置特定服务器特征或更改特定行为. 例如,跟踪标志 3226 是一种常用的启动跟踪标志,可取消显示错误日志中的成功备份消息. 跟踪标志经常用于诊断性能问题或调试存储过程或复杂的计算机系统, ...

  6. 如何开启php错误日志

    nginx与apache不一样,在apache中可以直接指定php的错误日志,那样在php执行中的错误信息就直接输入到php的错误日志中,可以方便查询. 在nginx中事情就变成了这样:nginx只对 ...

  7. laravel 5.5.39 升级到 5.5.45 出现 cookie 序列化异常问题的解决

    把项目里的 laravel 5.5.39 升级到 5.5.45 后,出现如下报错: ErrorExceptionopenssl_encrypt() expects parameter 1 to be ...

  8. ETCD:gRPC命名与发现

    原文地址:gRPC naming and discovery etcd提供一个gRPC解析器支持备用的命名系统,该命名系统从etcd获取主机以发现gRPC服务.以下机制基于监视对以服务名称为前缀的Ke ...

  9. SpringBoot2.0 基础案例(15):配置MongoDB数据库,实现增删改查逻辑

    本文源码:GitHub·点这里 || GitEE·点这里 一.NoSQL简介 1.NoSQL 概念 NoSQL( Not Only SQL ),意即"不仅仅是SQL".对不同于传统 ...

  10. c#中的Nullable(可空类型)

    在C#中使用Nullable类型(给整型赋null值的方法) 在C#1.x的版本中,一个值类型变量是不可以被赋予null值的,否则会产生异常.在C#2.0中,微软提供了Nullable类型,允许用它定 ...