20181216文件查找

文件查找

grep: 文件内容过滤
find: 文件查找,针对文件名
一、命令文件
which ls //从 PATH 环境变量 (echo $PATH)

[[email protected] ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/usr/local/htop/bin/:/root/bin
二、任意文件
(一). locate (查询的数据库: /var/lib/mlocate/mlocate.db)
计划任务:每天自动更新数据库 /etc/cron.daily/mlocate.cron
手动更新数据库:updatedb 在查找之前,先要更新数据库
locate ifcfg-eth0
locate ifcfg-enp0s25

(二)find(尽量缩小找的范围)
find [options] [path...] [expression] [action]
1.按文件名
[[email protected] ~]# find /etc -name "ifcfg-eth0"
[[email protected] ~]# find /etc -iname "ifcfg-eth0" //-i 忽略大小写
[[email protected] ~]# find /etc -iname "ifcfg-eth*" //借助通配符

2.按文件大小:
[[email protected] ~]# find /etc -size +5M //大于 5M
[[email protected] ~]# find /etc -size 5M //等于 5M
[[email protected] ~]# find /etc -size -5M //小于 5M
[[email protected] ~]# find /etc -size +5M -ls //-ls 找到的处理动作

3.指定查找的目录深度:
-maxdepth levels
-mindepth levels
[[email protected] ~]# find / -maxdepth 3 -a -name "ifcfg-eth0"

4.按时间找(atime,mtime,ctime):
[[email protected] ~]# ll -t /etc |head 查看最新修改的文件
-rw-r--r--. 1 root root 1309 12月 16 10:01 tpvmlp.conf
-rw-r--r--. 1 root root 239 12月 16 10:01 resolv.conf
-rw-r--r--. 1 root root 576 12月 16 10:01 mtab

[[email protected] ~]# ll -t /etc |tail 最旧的文件
-rw-r--r--. 1 root root 233 1月 12 2010 printcap
-rw-r--r--. 1 root root 6455 1月 12 2010 protocols
-rw-------. 1 root root 122 1月 12 2010 securetty

[[email protected] ~]# find /etc -mtime +5 //修改时间超过 5 天(120小时)
[[email protected] ~]# find /etc -mtime 5 //修改时间等于 5 天
[[email protected] ~]# find /etc -mtime -5 //修改时间 5 天以内

5.按文件属主、属组找:
[[email protected] ~]# find /home -user jack //属主是 jack 的文件
[[email protected] ~]# find /home -group hr //属组是 hr 组的文件
[[email protected] ~]# find /home -user jack -group hr //属主是jack,属组是group
[[email protected] ~]# find /home -user jack -a -group hr //和
[[email protected] ~]# find /home -user jack -o -group hr //或
[[email protected] ~]# find /home -nouser
[[email protected] ~]# find /home -nogroup
[[email protected] ~]# find /home -nouser -o -nogroup

6.按文件类型:
[[email protected] ~]# find /dev -type f //f 普通
[[email protected] ~]# find /dev -type d //d 目录
[[email protected] ~]# find /dev -type l //l 链接
[[email protected] ~]# find /dev -type b //b 块设备
[[email protected] ~]# find /dev -type c //c 字符设备
[[email protected] ~]# find /dev -type s //s 套接字
[[email protected] ~]# find /dev -type p //p 管道文件

7.按文件权限
[[email protected] ~]# find . -perm 644 -ls //当前目录下查找权限是644的
[[email protected] ~]# find . -perm -644 -ls //当前目录下查找权限是包含644的
[[email protected] ~]# find . -perm -600 -ls // 只要所有者有读写就可以
[[email protected] ~]# find . -perm -222 -ls //全局可写
[[email protected] ~]# find /usr/bin /usr/sbin -perm -4000 -ls //包含 set uid
[[email protected] ~]# find /usr/bin /usr/sbin -perm -2000 -ls //包含 set gid
[[email protected] ~]# find /usr/bin /usr/sbin -perm -1000 -ls //包含 sticky

8.按正则表达式:
-regex pattern
[[email protected] ~]# find /etc -regex ‘.ifcfg-eth[0-9]‘
.
任意多个字符
[0-9] 任意一个数字
[[email protected] ~]# find /etc -regex ‘.ifcfg-enp0s25‘
/etc/sysconfig/network-scripts/ifcfg-enp0s25
[[email protected] ~]# find /etc -regex ‘.
ifcfg-enp0s[0-9]+‘
/etc/sysconfig/network-scripts/ifcfg-enp0s25

9.找到后处理的动作 ACTIONS: (默认动作-print)
-ls
-delete
-exec 后面跟自定义的 shell 命令
-ok 后面跟自定义的 shell 命令
[[email protected] ~]# find /etc -name "ifcfg" -print[[email protected] ~]# find /etc -name "ifcfg" -ls
[[email protected] ~]# find /etc -name "ifcfg" -exec cp -rvf {} /tmp \; //{}表示前面查找到的结果
[[email protected] ~]# find /etc -name "ifcfg
" -ok cp -rvf {} /tmp \; //+v查看效果,OK会有提示
[[email protected] ~]# find /etc -name "ifcfg" -exec rm -rf {} \;
[[email protected] ~]# find /etc -name "ifcfg
" -delete

扩展知识:find 结合 xargs
[[email protected] ~]# find . -name "yang*.txt" |xargs rm -rf / /参数的传递xargs
[[email protected] ~]# find /etc -name "ifcfg-eth0" |xargs -I {} cp -rf {} /var/tmp

原文地址:http://blog.51cto.com/8450442/2331167

时间: 2024-08-18 19:23:05

20181216文件查找的相关文章

文件查找

grep, egrep, fgrep: 文本查找 文件查找: locate: 非实时,模糊匹配,查找是根据全系统文件数据库进行的: # updatedb, 手动生成文件数据库 速度快 find: 实时 精确 支持众多查找标准 遍历指定目录中的所有文件完成查找,速度慢: find 查找路径 查找标准 查找到以后的处理运作 查找路径:默认为当前目录 查找标准:默认为指定路径下的所有文件 处理运作:默认为显示 匹配标准: -name 'FILENAME':对文件名作精确匹配 文件名通配: *:任意长度

find、locate文件查找命令详解

一.locate:根据键值数据库模糊匹配,找路径 1.命令工作模式 (1)模糊查找 依赖于事先构建好的索引库,索引构建过程需要遍历整个根文件系统,占CPU使用资源 (2)无法实施更新 查找的是过去某一时刻更新的数据库文件,查找路径 系统自动更新:一般为每日系统例行性任务完成更新 手动更新数据库:uodatedb (3)查找速度快 通过对创建的索引库的匹配遍历查找文件路径,查询速度哦哒哒增加 2.格式:locate   [OPTION]...  PATTERN- -b:只匹配路径中的基名 -c:统

Linux文件查找之find&locate

Linux文件查找之find&locate 一.概述 Linux系统核心的思想之一"一切皆文件",对于这么多的文件,如何快速查找过滤呢?下面我们就看看系统提供的文件查找命令find和locat,熟练使用find命令是运维人员的必经之路 二.find的用法及示例 1.find特点 查找速度略慢 精确查找 实时查找 只能搜索有读取和执行权限的目录 2.find用法 用法:find  [options]  [查找路径]  [ 查找条件]  [处理动作] 查找条件: 根据文件类型查找

Linux基础之文件查找:locate、find

引言: 在学习Linux中的文件查找时,突然联想到平时用的搜索引擎,在生活中我们想获取什么信息,在google等搜索引擎里面敲入就能列出符合我们条件的相关信息.如果我们不满意搜索结果可以进一步精确我们想查找内容的搜索内容,这在Linux的文件查找中称为精确匹配,但是如果我们虽然知道我们想要的内容,但是找不到特别精确的言辞来形容它因此只能输入描述性内容为搜索条件,这种在Linux的文件查找中可以称为模糊匹配.本文的初衷是希望这篇文章可以解决大家在使用Linux的过程中不至于因为查找某个文件找不到而

关于文件查找和解压缩

文件查找和解压缩在文件系统上查找符合条件的文件,文件查找的工具有两个,locate 和 find文件查找分为:            locate      非实时查找 (在数据库查找)             updatedb   更新数据库            经常用于搜索稳定的文件,比如配置文件            var/lib/mlocate/mlocate.db 数据库路径             find     实时查找 locate :       查询系统上预建的文件索引

linux系统下文件查找

在我们实际应用中,经常需要查找某个特定的文件,或者根据文件的某个特定属性进行查找,今天小菜就给大家分享一下,linux系统下文件查找的两大利器: 1,locate:非实时查找(基于预先生成的数据库查找):模糊匹配:速度快 2,find:实时查找(遍历目录中的所有文件完成查找):精确匹配,支持众多查找标准:速度慢 一.locate 查询系统上预先生成的文件索引数据库:/var/lib/mlocate/mlocate.db 依赖于事先构建的索引:索引的构建是在系统较为空闲时自动进行(周期性任务) 管

Linux文件查找及压缩常用知识总结

一.文件查找 1.locate命令: locate KEYWORD 常用选项:     -i 执行区分大小写的搜索     -n  N只列举前N个匹配项目 查询系统上预建的文件索引数据库在:/var/lib/mlocate/mlocate.db上,由于事先建立索引,所以查找速度快. 2.find命令: 实时查找工具,通过遍历指定路径完成文件查找,查询的速度稍微慢点,精确查找,实时查找.可能只搜索用户具备读取和执行权限的目录. find - search for files in a direct

条件判断之if、case语句和文件查找命令

一.脚本编程 1.if语句怎样用 人生面临许多选择,在编程世界里同样也有许多选择.同其他编程语言一样,当我们想写一个功能健壮的脚本时,通过条件判断来选择适合的操作尤为重要.在我们执行某些重要的操作之前,判断当前环境是否适合执行这一操作是非常重要的.我们可以用&&和||来做简单的判断,不过shell有更用的语句.shell有两种常见的条件选择语句if和case.我们先来看一下if该怎样用吧. if语句的单分支语法: if 条件判断 ;then;执行命令:fi [[email protecte

linux笔记八---------文件查找

1.find文件查找指令 > find  目录  参数 参数值,参数 参数值.....    > find  /  -name  passwd   //从系统根目录开始递归查找name=passwd的文件    参数        -maxdepth  n  查找目录最深层次        -mindepth  n  查找目录最浅层次        -name  filename  根据文件名字查找        -size  大小    根据文件大小进行查找            大小单位5