Linux文件查找

1.find查找概述

为什么要有文件查找,因为很多时候我们可能会忘了某个文件所在的位置,此时就需要通过find来查找。

find命令可以根据不同的条件来进行查找文件,例如:文件名称、文件大小、文件修改时间、属主属组、权限、等等方式。同时find命令是Linux下必须掌握的。

*find 命令的基本语法如下*

命令 路径 选项 表达式 动作
find [path…] [options] [expression] [action]
查找 地区 妹纸 18-25岁 约?

是linux里面的一个实时查找工具,通过制定路径完成文件查找

find [options] ..... [查找路径] [查找条件] [处理动作]

查找路径:查找的位置,默认是当前文件夹

查找条件:制定查找的标准,文件名、大小、类型、日期等等

处理动作:对符合条件的文件做什么操作,默认是输出到屏幕上

2.find查找示例

*以下列出所有find常用的选项*

1.find名称查找

#1.创建文件
touch /etc/sysconfig/network-scripts/{ifcfg-eth1,IFCFG-ETH1} #2.查找/etc目录下包含ifcfg-eth0名称的文件
[root@lqz ~]# find /etc -name "ifcfg-eth1" #3.-i 忽略大小写
[root@lqz ~]# find /etc -iname "ifcfg-eth1"
#查找/etc目录下包含ifcfg-eth名称所有文件
[root@lqz ~]# find /etc/ -name "ifcfg-eth*"
[root@lqz ~]# find /etc -iname "ifcfg-eth*"

2.find大小查找

#1.查找大于5M的文件
[root@lqz ~]# find /etc -size +5M #2.查找等于5M的文件
[root@lqz ~]# find /etc -size 5M #3.查找小于5M的文件
[root@lqz ~]# find /etc -size -5M

3.find类型查找

# f 文件
[root@lqz ~]# find /dev -type f
# d 目录
[root@lqz ~]# find /dev -type d
# l 链接
[root@lqz ~]# find /dev -type l
# b 块设备
[root@lqz ~]# find /dev -type b
# c 字符设备
[root@lqz ~]# find /dev -type c
# s 套接字
[root@lqz ~]# find /dev -type s
# p 管道文件
[root@lqz ~]# find /dev -type p

4.find时间查找

#1.创建测试文件(后期shell会讲)
[root@lqz ~]# for i in {01..28};do date -s 201904$i && touch file-$i;done #2.查找7天以前的文件(不会打印当天的文件)
[root@lqz ~]# find ./ -iname "file-*" -mtime +7 #3.查找最近7天的文件,不建议使用(会打印当天的文件)
[root@lqz ~]# find ./ -iname "file-*" -mtime -7 #4.查找第7天文件(不会打印当天的文件)
[root@lqz ~]# find ./ -iname "file-*" -mtime 7 #5.本地文件保留最近7天的备份文件, 备份服务器保留3个月的备份文件(实际使用方案)
find /backup/ -iname "*.bak" -mtime +7 -delete
find /backup/ -iname "*.bak" -mtime +90 -delete

5.find用户查找

#查找属主是jack
[root@lqz ~]# find /home -user jack
#查找属组是admin
[root@lqz ~]# find /home -group admin
#查找属主是jack, 属组是admin
[root@lqz ~]# find /home -user jack -group admin
#查找属主是jack, 并且属组是admin
[root@lqz ~]# find /home -user jack -a -group admin
#查找属主是jack, 或者属组是admin
[root@lqz ~]# find /home -user jack -o -group admin
#查找没有属主
[root@lqz ~]# find /home -nouser
#查找没有属组
[root@lqz ~]# find /home -nogroup
#查找没有属主或属组
[root@lqz ~]# find /home -nouser -o -nogroup

6.find权限查找

#精切匹配644权限
[root@lqz ~]# find . -perm 644 -ls #包含444权限即可
[root@lqz ~]# find . -perm -444 -ls
#查找全局可写(每位权限必须包含w)
[root@lqz ~]# find . -perm -222 -ls
#包含set uid
[root@lqz ~]# find /usr/sbin -perm -4000 -ls
#包含set gid
[root@lqz ~]# find /usr/sbin -perm -2000 -ls
#包含sticky
[root@lqz ~]# find /usr/sbin -perm -1000 -ls

查找条件

  • 根据文件名查找

    • -name 指定名称,可以使用正则

    • -iname 忽略大小写

    • -links n 引用次数为n的文件

    • -regex 后面跟完整路径,而不是文件名, 必须整个路径完全匹配

  • 制定搜索的层级

    • -maxdepth level 最大的搜索深度,指定的目录为第1层

    • -mindepth level 最小的搜索深度,包括level层

  • 根据属主、属组来查找

    • -user username 查找属主为username的文件

    • -group groupname 查找属组为groupname的文件

    • -uid id 查找属主为id的文件

    • -gid id 查找属组为id的文件

    • -nouser 查找没有属主的文件

    • -nogroup 查找没有属组的文件

m[root@192 test]#chown qiao b
m[root@192 test]#ll
total 0
drwxr-xr-x. 4 root root 24 Dec 4 22:50 a
-rw-r--r--. 1 qiao root 0 Dec 6 17:53 b
m[root@192 test]#chown :llx b
m[root@192 test]#ll
total 0
drwxr-xr-x. 4 root root 24 Dec 4 22:50 a
-rw-r--r--. 1 qiao llx 0 Dec 6 17:53 b
m[root@192 test]#find -group llx
./b
m[root@192 test]#id root
uid=0(root) gid=0(root) groups=0(root)
m[root@192 test]#id qiao
uid=1000(qiao) gid=1000(qiao) groups=1000(qiao)
m[root@192 test]#find -uid 1000
./b m[root@192 test]#useradd xiaobao
m[root@192 test]#chown xiaobao b
m[root@192 test]#ll
total 0
drwxr-xr-x. 4 root root 24 Dec 4 22:50 a
-rw-r--r--. 1 xiaobao llx 0 Dec 6 17:53 b
m[root@192 test]#userdel xiaobao
m[root@192 test]#ll
total 0
drwxr-xr-x. 4 root root 24 Dec 4 22:50 a
-rw-r--r--. 1 1002 llx 0 Dec 6 17:53 b
m[root@192 test]#find -nouser
./b # 全盘找
m[root@192 test]#find / -nouser
  • 根据文件类型 -type

    • d 目录

    • f 文件

    • l 符号链接

    • s 套接字

    • b 块设备

    • c 字符设备

    • p 管道文件

m[root@192 test]#find -type f
./b
  • 空文件或者空目录

    • -empty

m[root@192 test]#find -empty
  • 条件

    • 与 -a

    • 或 -o

    • 非 -not

m[root@192 test]#find -empty -o -type d
m[root@192 test]#find -empty -not -type d
./b
  • 摩根定律

    • 非(A或者B) 非A 且非B

    • 非(A且B)非A或非B

m[root@192 ~]#find !(-empty -a -tpye d)
  • 排除目录

    • -path

[root@localhost test]#find /etc -name *_config
/etc/ssh/ssh_config
/etc/ssh/sshd_config
[root@localhost test]#find /etc -path /etc/ssh -name *_config
  • 按照大小来查找

    • -size # (#-1,#] 不包括#-1,包括#

    • -size -# [0,#-1] 包括#-1

    • -size +# (#,......)

  • 按照时间来查找

    • -atime # [#,#+1)

    • -atime -# (0,#)

    • -atime +# [#+1,....]

    • 查找7天以后的文件 find -atime +7

    • -mtime

    • -ctime

    • 以分钟为单位

      • -amin

      • -mmin

      • -cmin

3 处理动作

find动作处理,比如查找到一个文件后,需要对文件进行如何处理, find的默认动作是 -print

1.find查找后的动作命令示例

动作 含义
-print 打印查找到的内容(默认)
-ls 以长格式显示的方式打印查找到的内容
-delete 删除查找到的文件(仅能删除空目录)
-ok 后面跟自定义 shell 命令(会提示是否操作)
-exec 后面跟自定义 shell 命令(标准写法 -exec

- -print 默认的处理动作,显示在屏幕上
- -ls 类似于ls -l 显示长格式
- -delete 删除查找到的文件
- -fls file 将查找的结果以长格式保存到文件中
- -ok command {} \; 对每一个查找到的文件执行command命令,在执行命令之前要先提示用户是否要执行

find -size 2M -ok rm -rf {} \;
找到2M的文件,删除,提示删除
  • -exec command {} \; 对查到的每一个文件执行command命令,不需要确认,一次性交给后面命令处理

find -size 2M -exec rm -rf {} \;
m[root@192 test]#find -size 2M -delete
#1.使用-print打印查找到的文件
[root@lqz ~]# find /etc -name "ifcfg*"
[root@lqz ~]# find /etc -name "ifcfg*" -print #2.使用-ls打印查找到的文件,以长格式显示
[root@lqz ~]# find /etc -name "ifcfg*" -ls #3.使用-delete删除文件,但仅能删除空目录
[root@lqz ~]# find /etc -name "ifcfg*" -delete #4.使用-ok实现文件拷贝,但会提示是否拷贝
[root@lqz ~]# find /etc -name "ifcfg*" -ok cp -rvf {} /tmp \; #5.使用-exec实现文件拷贝和文件删除。
[root@lqz ~]# find /etc -name "ifcfg*" -exec cp -rvf {} /tmp \;
[root@lqz ~]# find /etc -name "ifcfg*" -exec rm -f {} \;

2.使用find命令结合xargs

  • 有的命令不支持管道

  • 命令参数过长

  • xargs 将管道前面的内容一条一条的交给后面命令处理

echo file{1..50000}|xargs touch
  • 一般会跟find使用

#xargs将前者命令查找到的文件作为一个整体传递后者命令的输入
[root@lqz ~]# touch file.txt
[root@lqz ~]# find . -name "file.txt" |xargs rm -f
[root@lqz ~]# find . -name "file.txt" |xargs -I {} cp -rvf {} /var/tmp

3.find逻辑运算符

符号 作用
-a
-o
-not|!
#1.查找当前目录下,属主不是hdfs的所有文件
[root@lqz ~]# find . -not -user hdfs
[root@lqz ~]# find . ! -user hdfs #2.查找当前目录下,属主属于hdfs,且大小大于300字节的文件
[root@lqz ~]# find . -type f -a -user hdfs -a -size +300c #3.查找当前目录下的属主为hdfs或者以xml结尾的普通文件
[root@lqz ~]# find . -type f -a \( -user hdfs -o -name '*.xml' \)

4.find相关练习题

1.查找/tmp目录下,属主不是root,且文件名不以f开头的文件
2.查找/var目录下属主为root,且属组为mail的所有文件
3.查找/var目录下不属于root、lp、gdm的所有文件
4.查找/var目录下最近一周内其内容修改过,同时属主不为root,也不是postfix的文件
5.查找/etc目录下大于1M且类型为普通文件的所有文件
6.将/etc/中的所有目录(仅目录)复制到/tmp下,目录结构不变
7.将/etc目录复制到/var/tmp/,/var/tmp/etc的所有目录权限777/var/tmp/etc目录中所有文件权限666
8.保留/var/log/下最近7天的日志文件,其他全部删除
9.创建touch file{1..10}10个文件, 保留file9,其他一次全部删除
10.解释如下每条命令含义
mkdir /root/dir1
touch /root/dir1/file{1..10}
find /root/dir1 -type f -name "file5"
find /root/dir1 ! -name "file5"
find /root/dir1 -name "file5" -o -name "file9"
find /root/dir1 -name "file5" -o -name "file9" -ls
find /root/dir1 \( -name "file5" -o -name "file9" \) -ls
find /root/dir1 \( -name "file5" -o -name "file9" \) -exec rm -rvf {} \;
find /root/dir1 ! \( -name "file4" -o -name "file8" \) -exec rm -vf {} \;

linux三剑客

三剑客详解

grep

awk

sed

grep

grep [option] "模式" file

option

--color=auto 对匹配到的行添加颜色
-v 取反
-i 不区分大小写
-n 查找的内容增加行号
-c 打印匹配到的行数
-o 只显示匹配到的文字
-q 静默模式
-A # after 向下显示#行
-B # before 向上显示#行
—C # context 上下分别显示#行
-e 或者 grep -e 'user' -e 'root' passwd 或的意思
-E 扩展正则表达式
-F 不使用正则表达式
-r 递归
-w 匹配整个单词

正则表达式元字符

  • 字符匹配

    • . 任意单个字符

    • [] 匹配指定范围内的任意单个字符 [0-9] [a-z] [A-Z]

    • [^] 取反

    • [:upper:] 大写字母

    • [:lower:] 小写字母

    • [:alnum:] 字母和数字

    • [:alpha:] 大小写字母

    • [:digit:] 数字

m[root@192 test]#grep '[[:digit:]]\+' c
# 匹配数字
    • [:black:] 空白

    • [:punct:] 标点符号

  • 匹配次数

    • * 表示任意次数

    • .* 任意字符任意次数

    • \? 表示0或者1次

    • \+ 至少一次

    • \{n\} 表示n次 \转义字符

    • \{m,n\} 最少m次,最多n次

    • \{n,\} 至少n次

    • \{,n\} 至多n次

  • 位置锚定

    • ^ 开头

    • $结尾

    • ^$ 空行

grep -v "^#" /etc/ssh/sshd_config |grep -v "^$" 显示不以#开头并且不是空行
  • 分组
grep "\(c\|C\)at" a
  • 向后引用

    • \1 前面第一个括号出现的内容匹配完成之后再后面在出现一次

    • \2 前面第二个括号出现的内容匹配完成之后再后面在出现一次

grep "\(l..e\).*\1" c
love djfdjfd;d love
  • 扩展正则表达式

    • 与正则表达式的区别是不需要转义

grep -E "(c|C)at" a
不需要加\进行转义

Linux文件查找、三剑客、正则表达式的更多相关文章

  1. Linux文件查找.md

    Linux 文件查找 在Linux系统的查找相关的命令: which 查看可执行文件的位置 whereis 查看文件的位置 locate 配合数据库查看文件位置 find 实际搜寻硬盘查询文件名称 w ...

  2. Linux文件查找命令find用法整理(locate/find)

    Linux文件查找查找主要包括:locate和find 1.locate 用法简单,根据数据库查找,非实时,用法: locate FILENAME 手动更新数据库(时间可能较长) updatedb 2 ...

  3. linux文件查找find命令

    linux文件查找find命令 1.文件查找 基本介绍 在文件系统上查找符合条件的文件 linux上常见的文件查找工具:find命令 查找分类 实时查找 精确查找 基本语法 find  [option ...

  4. linux 文件查找,which,whereis,locate,find

    linux 文件查找,which,whereis,locate,find 一:which 主要用于查找可执行命令的所在位置: 如图,查找命令 ls的目录: 二:whereis 主要用于查找命令的帮助文 ...

  5. Linux文件查找命令find,xargs详述【转】

    转自:http://blog.csdn.net/cxylaf/article/details/4069595 转自http://www.linuxsir.org/main/?q=node/137 Li ...

  6. Linux文件查找工具之find “大宝剑”--转载

    原文地址:http://xinzong.blog.51cto.com/10018904/1749465 一.文件查找工具常用软件 locate: locate命令其实是find -name的另一种写法 ...

  7. Linux 文件查找

    在Linux系统的查找相关的命令: which 查看可执行文件的位置 whereis 查看文件的位置 locate 配合数据库查看文件位置 find 实际搜寻硬盘查询文件名称 whereis wher ...

  8. Linux文件查找find和locate

    目 录 第1章 locate文件查找    1 1.1 概述    1 1.2 locate文件查找的特性    1 第2章 文件查找概述    1 第3章    1 3.1 文件名查找    1 3 ...

  9. Linux文件查找与打包

    一.文件查找 locate与find是经常使用的Linux 命令,刚接触Linux时对这两个命令的使用傻傻的分不清.现在我们来对比一下两个命令到底有哪些区别. 1.1 locate locate让使用 ...

  10. Linux文件查找

    Linux下查找文件的命令有两个; locate: find : locate这个命令对其生成的数据库进行遍历(生成数据库的命令:updatedb),这一特性决定了查 找文件速度很快,但是locate ...

随机推荐

  1. SOCKS5协议解析

    socks的官方文档:https://www.ietf.org/rfc/rfc1928.txt 本文改变其他作者之手,在原文基础上加入客户端的编写,完善了服务端代码,原文是Linux端的程序代码,本文 ...

  2. 架构设计理念&模型

    理念 今天我们还需要关注 DDD 吗?https://www.infoq.cn/article/should-we-focus-on-ddd 事件风暴:https://en.wikipedia.org ...

  3. Socket.D 开源输传协议的集群转发特性

    1.简介 Socket.D 是基于"事件"和"语义消息""流"的网络应用层协议.底层可以依赖 TCP.UDP.KCP.WebSocket 等 ...

  4. BZOJ3156 防御准备 题解

    原题 令 \(S_{i} =\sum\limits_{j=1}^{i}j\) , \(f_{i}\) 为处理到第 \(i\) 个位置放置守卫塔的最小花费. 观察题意,容易得到在\((1 \le j \ ...

  5. NC14685 加边的无向图

    题目链接 题目 题目描述 给你一个 n 个点,m 条边的无向图,求至少要在这个的基础上加多少条无向边使得任意两个点可达~ 输入描述 第一行两个正整数 n 和 m . 接下来的m行中,每行两个正整数 i ...

  6. html知识概念(不含标签)

    CS架构与BS架构 C/S架构 Client(客户端) / Server(服务器) 需要安装客户端使用,哔哩哔哩客户端.腾讯视频客户端等 需求安装.需要更新.不跨平台 B/S架构 Browser(浏览 ...

  7. Mysql表读写、索引等操作的sql语句效率优化问题

    上次我们说到mysql的一些sql查询方面的优化,包括查看explain执行计划,分析索引等等.今天我们分享一些 分析mysql表读写.索引等等操作的sql语句. 闲话不多说,直接上代码: 反映表的读 ...

  8. 使用yum 报错 :This system is not registered with RHN

    解决办法:(假定你已安装yum,且网络畅通)更改yum的源, 即更换/etc/yum.repos.d/rhel-debuginfo.repo 这个文件.首先备份,如下所示: [root@localho ...

  9. Java集合框架学习(十一) Hashtable详解

    Hashtable介绍 1. 继承自Dictionary; 2. 线程安全: 3. 支持Iterator和Enumeration: 4. key和value都不可为null; 5. 一般用于多线程环境 ...

  10. win32 - 将线程重定向到另一个函数(附带Suspend的解释)

    Suspend: 挂起指定的线程 备注:不要永远挂起线程, 因为在Win32中,进程堆是线程安全的对象,并且由于在不访问堆的情况下很难在Win32中完成很多工作,因此在Win32中挂起线程极有可能使进 ...