Linux find命令详解

基础打印操作

find命令默认接的命令是-print,它默认以\n将找到的文件分隔。可以使用-print0来使用\0分隔,这样就不会分行了。但是一定要注意,-print0针对的是\n转\0,如果查找的文件名本身就含有空格,则find后-print0仍然会显示空格文件。所以-print0实现的是\n转\0的标记,可以使用其他工具将\0标记替换掉,如xargs,tr等。
注意:如果find命令后没有加命令,那么find查找当前所在的目录进行查找。

在这里插入图片描述
在这里插入图片描述

文件名搜索

常用的两个是-name和-path。
-name可以对文件的basename进行匹配,
-path可以对文件的dirname+basename。查找的文件名最好使用引号包围,可以配合通配符进行查找。

在这里插入图片描述

注意:但不能在-name的模式中使用”/“,除非文件名中包含了字符”/“,否则将匹配不到任何东西,因为-name只对basename进行匹配。例如,想要匹配/tmp目录下某包含字符a的目录下的log文件
在这里插入图片描述

注意,配合通配符[]时应该注意是基于字符顺序的,大小写字母的顺序是a-z –> A-Z,指定[a-z]表示小写字母a-z,同理[A-Z],而[a-zA-Z]和[a-Z]都表示所有大小写字母。当然还可以指定[a-A]表示a-z外加一个A。
字母的处理顺序较容易理解,关于数字的处理方法,见下面的示例

[root@bogon directry]# find -name "[1-4].log"
./1.log
./2.log
./3.log
./4.log
[root@bogon directry]# find -name "[1-20].log"
./1.log
./2.log
./0.log
[root@bogon directry]# find -name "[1-23].log"
./1.log
./2.log
./3.log
[root@bogon directry]# find -name "[1-22-3].log"
./1.log
./2.log
./3.log
从上面结果可以看出,其实[]只能匹配单个字符,[0-9]表示0-9的数字,[1-20]表示[1-2]外加一个0,[1-23]表示[1-2]外加一个3,[1-22-3]表示[1-2][2-3],迷惑点就是看上去是大于10的整数,其实是两个或者更多的单个数字组合体。也可以用这种方法表示多种匹配:[1-2,2-3]

根据文件类型搜索 -type

一般需要搜索的文件类型就只有普通文件(f),目录(d),链接文件(l)。
[root@bogon directry]# find -type f -name '*.log'
./1.log
./2.log
./3.log
./4.log
./5.log
./abc/haha.log
./0.log
[root@bogon directry]# find -type d -name 'a*'
./abc

根据文件时间戳搜索

最基础的时间戳包括:
-atime:显示的是文件中的数据最后被访问的时间,比如系统的进程直接使用或通过一些命令和脚本间接使用。(执行一些可执行文件或脚本)。
-mtime:显示的是文件内容被修改的最后时间,比如用vi编辑时就会被改变。(也就是Block的内容)
-ctime:显示的是文件的权限、拥有者、所属的组、链接数发生改变时的时间。当然当内容改变时也会随之改变(即inode内容发生改变和Block内容发生改变时)。
[root@bogon directry]# find -type f  -mtime -30 -name "*.log"
./1.log
./2.log
./3.log
./4.log
./5.log
./abc/haha.log
./0.log

根据文件大小搜索

例如搜索/directry下大于0K的.log文件
[root@bogon directry]# find -type f -size +0k -name '*.log'
./1.log
./0.log

根据权限搜索:-perm

[root@bogon directry]# find -type f -perm 0644 -name '*.log'
./2.log
./3.log
./4.log
./5.log
./abc/haha.log
./1.log
./0.log
[root@bogon directry]# ll
total 8
-rw-r--r--. 1 root root 992 Jan 14 06:31 0.log
-rw-r--r--. 1 root root   5 Jan 14 06:26 1.log
-rw-r--r--. 1 root root   0 Jan 14 06:00 2.log
-rw-r--r--. 1 root root   0 Jan 14 06:00 3.log
-rw-r--r--. 1 root root   0 Jan 14 06:00 4.log
-rw-r--r--. 1 root root   0 Jan 14 06:00 5.log
drwxr-xr-x. 2 root root  22 Jan 14 06:05 abc
-rw-r--r--. 1 root root   0 Jan 14 06:14 touch

搜索空文件

空文件可以是没有任何内容的普通文件,也可以是没有任何内容的目录。
例如搜索目录中没有文件的空目录 空文件。
[root@bogon directry]# find -type d -empty
./abc
[root@bogon directry]# find -type f -empty
./2.log
./3.log
./4.log
./5.log

搜索到文件后并删除

例如搜索到/directry下的”.log”文件然后删除。
[root@bogon directry]# ll
total 8
-rw-r--r--. 1 root root 992 Jan 14 06:31 0.log
-rw-r--r--. 1 root root   5 Jan 14 06:26 1.log
-rw-r--r--. 1 root root   0 Jan 14 06:00 2.log
-rw-r--r--. 1 root root   0 Jan 14 06:00 3.log
-rw-r--r--. 1 root root   0 Jan 14 06:00 4.log
-rw-r--r--. 1 root root   0 Jan 14 06:00 5.log
drwxr-xr-x. 2 root root  22 Jan 14 06:05 abc
-rw-r--r--. 1 root root   0 Jan 14 06:14 touch
[root@bogon directry]# find -type  f -name "*.log" -exec rm -rf '{}' \;
[root@bogon directry]# ll
total 0
drwxr-xr-x. 2 root root 6 Jan 14 06:39 abc
-rw-r--r--. 1 root root 0 Jan 14 06:14 touch

搜索指定日期范围的文件

例如搜索/test下2021-06-03到2021-06-06之间修改过的文件。

$ find /test -type f -newermt 2021-06-03 -a ! -newermt 2021-06-06

或者,创建两个临时文件,并用touch修改这两个文件的修改时间,然后find -newer去参照这两个文件。

$ touch -m -d 2017-06-03 tmp1.txt
$ touch -m -d 2017-06-06 tmp2.txt
$ find /test -type f -newer tmp1.txt -a ! -newer tmp2.txt

并行加速搜索

有时候,想要搜索的内容并不知道在哪里,这时我们会从根”/“开始搜索,这样的搜索速度可能会稍微长那么一点点。为了加速搜索,使用xargs的并行功能。例如,搜索”/“下的所有”.log”结尾的文件:
[root@bogon directry]# ls --hide proc / | xargs -i -P 0 find /{} -type f -name "*.log" /1.log
/2.log
/3.log
/4.log
/5.log
/home/sun/.local/share/gvfs-metadata/home-65477e94.log
/var/lib/mysql/tc.log
/var/log/audit/audit.log
/var/log/sssd/sssd.log
/var/log/sssd/sssd_implicit_files.log
/var/log/sssd/sssd_nss.log
/var/log/sssd/sssd_kcm.log
/var/log/tuned/tuned.log
/var/log/anaconda/anaconda.log
/var/log/anaconda/X.log
/var/log/anaconda/program.log
/var/log/anaconda/packaging.log
/var/log/anaconda/storage.log
/var/log/anaconda/lvm.log
/var/log/anaconda/dnf.librepo.log
/var/log/anaconda/hawkey.log
/var/log/anaconda/dbus.log
/var/log/anaconda/ks-script-2eqowa86.log
/var/log/anaconda/ks-script-883u3o8h.log
/var/log/anaconda/ks-script-qijn4v0c.log
/var/log/anaconda/ks-script-w2hkk999.log
/var/log/anaconda/journal.log
/var/log/boot.log
/var/log/vmware-vmtoolsd-root.log
/var/log/vmware-vmsvc-root.log
/var/log/vmware/rc.local.log
/var/log/kdump.log
/var/log/dnf.log
/var/log/dnf.librepo.log
/var/log/dnf.rpm.log
/var/log/hawkey.log
/var/log/mariadb/mariadb.log
/var/log/vmware-network.3.log
/var/log/vmware-network.2.log
/var/log/vmware-network.1.log
/var/log/vmware-network.log
/usr/lib/rpm/rpm.log

获取文件绝对路径

当find结合管道,而管道后的命令很可能想要获取到搜索到的文件的绝对路径,或者说是全路径。而问题是,当find的搜索路径是相对路径时,搜索出来的显示结果也是以相对路径显示的。
三种形式参考
[root@bogon directry]# find '*.log' $(pwd)
find: ‘*.log’: No such file or directory
/directry
/directry/abc
/directry/touch
/directry/1.log
/directry/2.log
/directry/3.log
/directry/4.log
/directry/5.log
[root@bogon directry]# find '*.log' $PWD
find: ‘*.log’: No such file or directory
/directry
/directry/abc
/directry/touch
/directry/1.log
/directry/2.log
/directry/3.log
/directry/4.log
/directry/5.log
[root@bogon directry]# find '*.log' ~+
find: ‘*.log’: No such file or directory
/directry
/directry/abc
/directry/touch
/directry/1.log
/directry/2.log
/directry/3.log
/directry/4.log
/directry/5.log

获取文件名部分(basename)

find的-printf选项有很多修饰符功能,对于处理路径方面的修饰符有%f、%p、%P,其中%f是获取basename(去除所有路径前缀),%p是获取路径自身,一般用不上,%P是获取除了find搜索路径的剩余部分。
首先,想要获取basename,建议使用%f
[root@bogon /]# find  /directry/ -printf "%f\n"
directry/
abc
touch
1.log
2.log
3.log
4.log
5.log[root@bogon /]# find  /directry/ -printf "%p\n"
/directry/
/directry/abc
/directry/touch
/directry/1.log
/directry/2.log
/directry/3.log
/directry/4.log
/directry/5.log再看使用%P的效果。结果仅仅是去掉了find搜索路径/tmp/test部分。当搜索路径只有一层(即没有子目录)时,它也可以用来获取basename。
[root@bogon /]# find  /directry/ -printf "%P\n"
abc
touch
1.log
2.log
3.log
4.log
5.log

从结果中排除目录自身

find搜索目录时,总是会将搜索路径自身也包含到搜索结果中。想办法排除它是必须的。
排除的方法是,加上一个-path选项并取反,-path的参数和find的搜索路径参数必须一致。
[root@bogon /]# find /directry/ ! -path /directry/
/directry/abc
/directry/touch
/directry/1.log
/directry/2.log
/directry/3.log
/directry/4.log
/directry/5.log
[root@bogon /]# find /directry/
/directry/
/directry/abc
/directry/touch
/directry/1.log
/directry/2.log
/directry/3.log
/directry/4.log
/directry/5.log

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://xiahunao.cn/news/350927.html

如若内容造成侵权/违法违规/事实不符,请联系瞎胡闹网进行投诉反馈,一经查实,立即删除!

相关文章

find()和findIndex()函数详解

今天我在拿数组中的存在的某一项索引时,我用了filter(),结果折腾了半天还是没求出索引,结果询问同事,他说你直接用findIndex()就可以了,一语点醒梦中人,可能平时ES6的这些函数用的少,根本就想不…

linux find -size参数,Linux find 常用命令

Linux find 常用命令 (2012-10-24 11:32:52) 标签: linux shell 说明: 以下内容,若标注在 [ ] 中括号内的均可省略,除非做特别说明。 但严格来说,若缺少 [ ] 中括号内的内容,在命令呈现的语义和返回结果上&…

二、linux find命令详解

目录 2. find【擅长在目录下找文件】 2.1 常用查询选项option 2.1.1 -name:根据名称匹配 2.1.2 -iname:忽略文件名称大小写 2.1.3 -user:根据所属用户组进行查询 2.1.4 -type:根据文件类型进行查找 2.1.5 -size:根据…

Linux的find命令

find 查找命令 -name 查找某个文件,文件名为name 例如: find /etc/ -name passwd 查找/etc/目录下名为passwd的文件并输出 find /etc -name *.conf 查找/etc/目录下所有以.conf结尾的文件 -user name 查找某个文件所有人为use…

Linux 命令----find:查找指定的文件

📢📢📢📣📣📣 哈喽!大家好,我是「奇点」,江湖人称 singularity。刚工作几年,想和大家一同进步🤝🤝 一位上进心十足的【Java ToB端大厂…

Linux下find命令详解

find命令格式: find path -option 【 -print 】 【 -exec -ok |xargs |grep 】 【 command {} \; 】 find命令的参数: 1)path:要查找的目录路径。 ~ 表示$HOME目录 . 表示当前目录 / 表示根目录 2&…

LinuxFinal

综合实验 tomcat下载安装 Apache Tomcat - Apache Tomcat 8 Software Downloads 进入上方链接下载tomcat,并上传至服务器。 mkdir /usr/local/tomcat8 # 创建tomcat文件夹 cd /usr/local/tomcat8 # 进入此目录,上传tomcat压缩包 tar -zxvf apache-tom…

Linux文件查找find

目录 前言 查找命令 命令演示 1.which:命令查找 2.locate命令 3.find命令(主要使用这个命令进行查找文件) 1)语法 2)find的用法介绍 按文件名查找 手动写入指定大小数据到文件内,介绍一下dd命令。…

Linux下的find指令

一、概述: 因为Linux下面一切皆文件,经常需要搜索某些文件来编写,所以对于linux来说find是一条很重要的命令。linux下面的find指令用于在目录结构中搜索文件,并执行指定的操作。它提供了相当多的查找条件,功能很强大。…

linux万能查询命令——find命令

find命令用来寻找文件或目录,功能非常强大,可以在查找到的文件上去执行命令,准确查找,遍历整个目录下的所有文件,可以对查询到的文件进行指定的动作,即查看、删除、移动等操作。 目录 1、常用查找命令 2…

linux find命令

一、find命令语法格式: find [路径] [选项] [操作] 选项参数对照表: 三、常用选项 -name  查找 /etc 目录下以 conf 结尾的文件,文件名区分大小写,例如:find /etc -name *.conf -iname 查找当前目录下所有文件名…

Linux - find命令详解

一、作用 find是搜索匹配条件的文件。 二、语法 用法:find[PATH][option][-print] PATH:目录路径,也可以使用“/ 根目录” option参数:   -name filename #查找名为filename的文件   -perm #按执行权限来查找   -user username #按文…

Linux中find命令用法大全

Linux 查找命令是Linux系统中最重要和最常用的命令之一。查找用于根据与参数匹配的文件指定的条件来搜索和查找文件和目录列表的命令。查找可以在各种条件下使用,您可以通过权限,用户,组,文件类型,日期,大小…

Linux命令之find命令

一、命令简介 Linux find 命令用来在指定目录下查找文件。任何位于参数之前的字符串都将被视为欲查找的目录名。如果使用该命令时,不设置任何参数,则 find 命令将在当前目录下查找子目录与文件。并且将查找到的子目录和文件全部进行显示。find 命令有非常…

Linux命令之查找文件find

概述 find 命令可以在目录层次结构中搜索符合指定条件的文件或目录,并且可以它们执行指定的操作。而搜索条件可以是文件权限、文件类型、日期、大小等。 语法 该命令的语法如下: # find 命令从指定的起始目录开始,递归地搜索其各个子目录&…

项目经理高效时间管理6大原则及技巧

1、 ( 10 2 ) * 5法则 项目经理以10分钟为一个周期,每个周期休息2分钟,这样1小时重复5次,就可以保障我们目标明确但精神和身体上的精力不会透支。 10分钟周期内,需要全心全意做一件事,进入全身心投入工作状态&#xf…

TDEngine3.0 环境安装、配置及使用经验总结

TDEngine3.0 环境安装、配置及使用经验总结 一、TDengine 介绍二、TDengine的下载三、TDengine Server安装及配置3.1 安装3.2 taos的参数配置3.3 启动3.4 taosAdapter 四、TDengine Client 安装4.1 linux客户端安装4.2 windows客户端安装 五、TDEngine3.x的使用总结 一、TDengi…

modelandview 跳转问题_ModelAndView 跳转的使用

路径: 学习springmvc ModelAndView的使用,之前一直仿别人ModelAndView的代码,都是好用的,自己建工程学了下,发现 new ModelAndView("jsp1/0724/myDialog")的时候跳转不过去 配置文件: /** * aut…

SpringMVC笔记--5处理响应数据ModelAndView

第5章 处理响应数据 5.1 SpringMVC 输出模型数据概述 对于MVC框架来说模式数据是最重要的,因为控制C是为了产生模型数据M,而视图V则是为了渲染模型数据。 如何将模型数据暴露给视图是Spring MVC框架的一项重要工作,Spring MVC提供了多种途…

浅入浅出 iptables 网络隔离原理

01 iptables简介 iptables ipfirewall(内核1.x时代) ipchains(内核2.x时代) iptables 网络协议栈 Link Layer 数据链路层的数据流向,根据mac寻址找到对应的网卡后向上进入网络层 Network Layer 网络层的数据流向&am…