一、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. python 基础学习笔记(6)--函数(2)

                                                                                                        ...

  2. node_modules/.bin/babel : 无法加载文件 D:\node\node_project\es6\node_modules\.bin\babel.ps1,因为在此系统上禁止运行脚本。有关详细信息,请参阅 https:/go.microsoft.co m/fwlink/?LinkID=135170 中的 about_Execution_Policies。

    刚入门es6,遇到上面问题,然后 解决方案: 以管理员身份运行vs code执行:get-ExecutionPolicy,显示Restricted,表示状态是禁止的执行:set-ExecutionPo ...

  3. IDEA 工具自动生成JavaBean类

    1.先安装GsonFormat插件:File-->Setting-->Plugins-->GsonFormat-->OK 2.new 一个新的Class空文件,然后 Alt+I ...

  4. 生成对抗性网络GAN

    同VAE模型类似,GAN模型也包含了一对子模型.GAN的名字中包含一个对抗的概念,为了体现对抗这个概念,除了生成模型,其中还有另外一个模型帮助生成模型更好地学习观测数据的条件分布.这个模型可以称作判别 ...

  5. log4j日志打印的配置文件简单使用

    log4j.properties #将等级为DEBUG的日志信息输出到console和file这两个目的地,console和file的定义在下面的代码 log4j.rootLogger=DEBUG,c ...

  6. Python常用的正则表达式处理函数

    Python常用的正则表达式处理函数 正则表达式是一个特殊的字符序列,用于简洁表达一组字符串特征,检查一个字符串是否与某种模式匹配,使用起来十分方便. 在Python中,我们通过调用re库来使用re模 ...

  7. 基于docker-compose搭建本地sentry服务

    环境要求: centos 7 Docker 17.05.0+ Compose 1.19.0+ RAM 2400MB docker-compose 安装 ``$ curl -L https://get. ...

  8. 【洛谷5644】[PKUWC2018] 猎人杀(容斥+生成函数+分治NTT)

    点此看题面 大致题意: 有\(n\)个人相互开枪,每个人有一个仇恨度\(a_i\),每个人死后会开枪再打死另一个还活着的人,且第一枪由你打响.设当前剩余人仇恨度总和为\(k\),则每个人被打中的概率为 ...

  9. jQuery-跨域问题的处理

    调用登录接口时,后端一般会在调用登录接口成功后,在response中设置cookie,之后前端的每次请求都会自动地在请求头上加上后端设置好的cookie,这对前端来说是透明的. 当登录接口与登录后调用 ...

  10. 如何编写高质量的 JS 函数(4) --函数式编程[实战篇]

    本文首发于 vivo互联网技术 微信公众号 链接:https://mp.weixin.qq.com/s/ZoXYbjuezOWgNyJKmSQmTw作者:杨昆 [编写高质量函数系列],往期精彩内容: ...