14-find 查找文件
find - search for files in a directory hierarchy 查找文件
【语法】: find 【选项】 【参数】
【功能介绍】
find命令用来在指定目录下查找文件。任何位于参数之前的字符串都将被视为欲查找的目录名。如果使用该命令时,不设置任何参数,则find命令将在当前目录下查找子目录与文件。并且将查找到的子目录和文件全部进行显示。
【选项说明】
-maxdepth levels 设置最大目录层级;
Descend at most levels (a non-negative integer) levels of directories below the com-
mand line arguments. -maxdepth
4 means only apply the tests and actions to the command line arguments. -mindepth levels 设置最小目录层级;
Do not apply any tests or actions at levels less than levels (a non-negative integer).
-mindepth means process all files except the command line arguments. -name pattern 指定字符串作为寻找文件或目录的范本样式; -size 指定文件大小 -type c 指定文件类型 b block (buffered) special 字符设备
c character (unbuffered) special 字符串
d directory 目录
p named pipe (FIFO)
f regular file 普通文件
l symbolic link; this is never true if the -L option or the -follow option is in 符号连接
effect, unless the symbolic link is broken. If you want to search for symbolic
links when -L is in effect, use -xtype. -exec <执行指令>:假设find指令的回传值为True,就执行该指令;
--mtime 查找指定天数
!取反
【参数说明】
查找文件的起始目录
【经验技巧】
- find指令支持逻辑运算符与(and)、或(or)和非(not)组成的复合查询条件。现象“-a”为默认选项。逻辑与表示当所有给定的条件都满足时,符合查找条件;逻辑非表示查找与所给条件相反的文件。
- 通过find指令的“-exec” 选项可以通过外部Linux指令对找到的文件进行操作。如果找到的文件较多,有可能出现“参数太长”,或者“参数溢出”的错误。可以使用xargs指令每次制度取一部分查找到的文件,等处理完毕后再读取一部分查找的文件,一次类推,直到所有的文件都被处理完毕。
- 为了缩短find指令的执行时间,要尽量的缩小查找的其实目录。因为find指令使用递归方式遍历目录,所以其实目录范围较大,会导致find指令的运行过程过长。
- 不带任何选项和参数的find指令可以打印当前目录下的所有内容,包括所有子目录下的文件列表。
【实例】
实例1:删除当前目录下所有.txt文件
[root@cobbler6 ~]# find . -type f -name "*.txt"
./oldboy.txt
./luoahong/.txt
./test/e.txt
./test/b.txt
./test/a.txt
./test/c.txt
./test/d.txt
[root@cobbler6 ~]# find . -type f -name "*.txt"|xargs rm -f
[root@cobbler6 ~]# ll
total
drwxr-xr-x root root Nov : -
-rw-------. root root Oct : anaconda-ks.cfg
-rw-r--r--. root root Oct : install.log
-rw-r--r--. root root Oct : install.log.syslog
-rw-r--r-- root root Dec : luo1.tar.gz
drwxr-xr-x root root Dec : luoahong
-rw-r--r-- root root Nov : luo.conf
-rw-r--r-- root root Nov : nginx.conf
-rw-r--r-- root root Nov : oldboy---.tar.gz
drwxr-xr-x root root Nov : oldboydir
lrwxrwxrwx root root Nov : oldboydir_hard_link -> oldboydir
lrwxrwxrwx root root Nov : oldboydir_soft_link -> oldboydir
-rw-r--r-- root root Nov : oldboy.log
drwxr-xr-x root root Nov : p
drwxr-xr-x root root Dec : test
实例2:借助-exec选项与其他命令结合使用
找出当前目录下所有root的文件,并把所有权更改为用户tom
find .-type f -user root -exec chown tom {} \;
上例中,{} 用于与-exec选项结合使用来匹配所有文件,然后会被替换为相应的文件名。 找出自己家目录下所有的.txt文件并删除
find $HOME/. -name "*.txt" -ok rm {} \;
上例中,-ok和-exec行为一样,不过它会给出提示,是否执行相应的操作。 查找当前目录下所有.txt文件并把他们拼接起来写入到all.txt文件中
find . -type f -name "*.txt" -exec cat {} \;> all.txt
将30天前的.log文件移动到old目录中
find . -type f -mtime + -name "*.log" -exec cp {} old \;
实例3:删除7前以前的日志文件
[root@zabbix-agent log]# find ./ -type f -name "*.log" -mtime +|xargs rm -f
实例4:搜索大于10KB的文件
[root@zabbix-agent ~]# find . -type f -size +10k
实例5:搜索出深度距离当前目录至少2个子目录的所有文件
[root@zabbix-agent ~]# find . -mindepth -type f
实例6:向下最大深度限制为3
[root@zabbix-agent ~]# find . -maxdepth -type f
14-find 查找文件的更多相关文章
- 菜鸟学Linux命令:find命令 查找文件
find命令是Linux下最常用的命令之一,灵活的使用find命令,你会发现查找文件变得十分简单. 命令格式 find [指定查找目录] [查找规则(选项)] [查找完后执行的动作] 参数规则 - ...
- Linux 查找文件命令 find whereis locate
Linux 有三个查找文件的命令:find, whereis, locate 其中find 不常用,whereis与locate经常使用,因为find命令速度较慢,因为whereis与locate是利 ...
- 【Linux】常用指令、ps查看进程、kill杀进程、启动停止tomcat命令、查看日志、查看端口、find查找文件
1.说出 10 个 linux 常用的指令 1) ls 查看目录中的文件 2)cd /home 进入 '/ home' 目录:cd .. 返回上一级目录:cd ../.. 返回上两级目录 3)mkdi ...
- 查找文件工具find
与locate.whereis命令相比,find具有本质的区别: 首先,find是从指定的位置进行遍历查找(可以理解为对文件和目录进行逐一查找). 其次,find可以查找具有某一类特征的文件(例如查找 ...
- 第14章 启动文件详解—零死角玩转STM32-F429系列
第14章 启动文件详解 全套200集视频教程和1000页PDF教程请到秉火论坛下载:www.firebbs.cn 野火视频教程优酷观看网址:http://i.youku.com/firege ...
- find命令的基础用法以及按文件修改时间查找文件
一般文件查找方法: find 命令学好是一件很有趣的事情,也可以帮你在查找系统文件的时候事倍功半,还可以与正则表达式结合使用,功能强大,是一个很好的查找工具.可以整体提高你的系统管理能力. 基础用法 ...
- Linux fing cd 查找文件/文件夹并进入目录命令
查找文件|文件夹并进入目录命令:cd $() [root@localhost /]# cd $(dirname "`find / -name 'ifcfg-ens33'`") // ...
- Linux find命令:在目录中查找文件(超详解)
find 是 Linux 中强大的搜索命令,不仅可以按照文件名搜索文件,还可以按照权限.大小.时间.inode 号等来搜索文件.但是 find 命令是直接在硬盘中进行搜索的,如果指定的搜索范围过大,f ...
- day12 查找文件
day12 查找文件 find命令:查找文件 find命令:在linux系统中,按照我们的要求去查询文件. 格式: find [查询的路径] [匹配模式] [匹配规则] 匹配模式: -name : 按 ...
- locate 最快的查找文件的命令 NB
我见过最NB的查找文件最快的命令 [root@NB data]# locate teamviewer. /data/Software/teamviewer.i686.rpm /home/ok/.loc ...
随机推荐
- hive中的一种假NULL现象
使用hive时,我们偶尔会遇到这样的问题,当你将结果输出到屏幕时,查出的数据往往显示为null,但是当你将结果输出到文本时,却显示为空(即未填充),这是为什么呢? 在hive中有一种假NULL,它看起 ...
- 构建 ARM Linux 4.7.3 嵌入式开发环境 —— BusyBox 构建 RootFS
上一篇我们已经成功将 ARM Linux 4.7.3 的内核利用 U-BOOT 引导了起来.但是细心的你会发现,引导到后面,系统无法启动,出现内核恐慌 (Kernel Panic). 原因是找不到文件 ...
- 警惕多iframe下的同名id引起的诡异问题
遇到个诡异bug,虽然bug中套bug,忽略次要bug,其中最诡异最典型的现象是多行window.top.$("#id")取值操作,其中有一行却取不到值.这个着实让我费解.因为用到 ...
- Hibernate4中使用getCurrentSession报Could not obtain transaction-synchronized Session for current thread
架个spring4+hibernate4的demo,dao层直接注入的sessionFactory,然后用getCurrentSession方法获取session,然后问题来了,直接报错: Could ...
- Addthis使用
一.使用 https://www.addthis.com/.国外的SNS分享站点较多,适用于英文站点内容的分享.有教程. 我的测试代码如下: <html> <meta charset ...
- cuda中thread id
//////////////////////////////////////////////////////////////////////////// // // Copyright 1993-20 ...
- 翻译《Writing Idiomatic Python》(四):字典、集合、元组
原书参考:http://www.jeffknupp.com/blog/2012/10/04/writing-idiomatic-python/ 上一篇:翻译<Writing Idiomatic ...
- POJ2230Watchcow[欧拉回路]
Watchcow Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 7512 Accepted: 3290 Specia ...
- USB Type-C 接口有什么优点?
USB Type-C 接口有什么优点? 提到USB Type-C接口(以下简称为USB-C),大家第一个能想到的是USB-C接口能正反插,用起来很舒服.了解更多的可能还支持USB-C接口速度更快, 达 ...
- php 正则匹配中文(转)
我使用正则表达式来匹配中问的时候,出现了无法匹配的问题,问题如下 PCRE does not support \L, \l, \N{name}, \U, or \u at offset 2 我原来的匹 ...