linux命令学习笔记(22):find 命令的参数详解
find一些常用参数的一些常用实例和一些具体用法和注意事项。
.使用name选项:
文件名选项是find命令最常用的选项,要么单独使用该选项,要么和其他选项一起使用。 可以使用某种文件名
模式来匹配文件,记住要用引号将文件名模式引起来。 不管当前路径是什么,如果想要在自己的根目录$HOME中
查找文件名符合*.log的文件,使用~作为 'pathname'参数,波浪号~代表了你的$HOME目录。
find ~ -name "*.log" -print
想要在当前目录及子目录中查找所有的‘ *.log‘文件,可以用:
find . -name "*.log" -print
想要的当前目录及子目录中查找文件名以一个大写字母开头的文件,可以用:
find . -name "[A-Z]*" -print
想要在/etc目录中查找文件名以host开头的文件,可以用:
find /etc -name "host*" -print
想要查找$HOME目录中的文件,可以用:
find ~ -name "*" -print 或find . -print
要想让系统高负荷运行,就从根目录开始查找所有的文件。
find / -name "*" -print
如果想在当前目录查找文件名以一个个小写字母开头,最后是4到9加上.log结束的文件:
命令:
find . -name "[a-z]*[4-9].log" -print
输出:
[root@localhost test]# ll
总计
-rw-r--r-- root root - : log2012.log
-rw-r--r-- root root - : log2013.log
-rw-r--r-- root root - : log2014.log
-rw-r--r-- root root - : log2015.log
drwxr-xr-x root root - : scf
drwxrwxr-x root root - : test3
drwxrwxr-x root root - : test4
[root@localhost test]# find . -name "[a-z]*[4-9].log" -print
./log2014.log
./log2015.log
./test4/log2014.log
[root@localhost test]#
.用perm选项:
按照文件权限模式用-perm选项,按文件权限模式来查找文件的话。最好使用八进制的权限表示法。
如在当前目录下查找文件权限位为755的文件,即文件属主可以读、写、执行,其他用户可以读、执行的文件,
可以用:
[root@localhost test]# find . -perm -print
.
./scf
./scf/lib
./scf/service
./scf/service/deploy
./scf/service/deploy/product
./scf/service/deploy/info
./scf/doc
./scf/bin
[root@localhost test]# 还有一种表达方法:在八进制数字前面要加一个横杠-,表示都匹配,如-007就相当于777,-005相当于555,
命令:
find . -perm -
输出:
[root@localhost test]# ll
总计
-rw-r--r-- root root - : log2012.log
-rw-r--r-- root root - : log2013.log
-rw-r--r-- root root - : log2014.log
-rw-r--r-- root root - : log2015.log
drwxr-xr-x root root - : scf
drwxrwxr-x root root - : test3
drwxrwxr-x root root - : test4
[root@localhost test]# find . -perm -
.
./test4
./scf
./scf/lib
./scf/service
./scf/service/deploy
./scf/service/deploy/product
./scf/service/deploy/info
./scf/doc
./scf/bin
./test3
[root@localhost test]#
.忽略某个目录:
如果在查找文件时希望忽略某个目录,因为你知道那个目录中没有你所要查找的文件,那么可以使用-prune选项来
指出需要忽略的目录。在使用-prune选项时要当心,因为如果你同时使用了-depth选项,那么-prune选项就会被
find命令忽略。如果希望在test目录下查找文件,但不希望在test/test3目录下查找,可以用:
命令:
find test -path "test/test3" -prune -o -print
输出:
[root@localhost soft]# find test -path "test/test3" -prune -o -print
test
test/log2014.log
test/log2015.log
test/test4
test/test4/log2014.log
test/test4/log2013.log
test/test4/log2012.log
test/scf
test/scf/lib
test/scf/service
test/scf/service/deploy
test/scf/service/deploy/product
test/scf/service/deploy/info
test/scf/doc
test/scf/bin
test/log2013.log
test/log2012.log
[root@localhost soft]#
.使用find查找文件的时候怎么避开某个文件目录:
实例1:在test 目录下查找不在test4子目录之内的所有文件
命令:
find test -path "test/test4" -prune -o -print
输出:
[root@localhost soft]# find test
test
test/log2014.log
test/log2015.log
test/test4
test/test4/log2014.log
test/test4/log2013.log
test/test4/log2012.log
test/scf
test/scf/lib
test/scf/service
test/scf/service/deploy
test/scf/service/deploy/product
test/scf/service/deploy/info
test/scf/doc
test/scf/bin
test/log2013.log
test/log2012.log
test/test3
[root@localhost soft]# find test -path "test/test4" -prune -o -print
test
test/log2014.log
test/log2015.log
test/scf
test/scf/lib
test/scf/service
test/scf/service/deploy
test/scf/service/deploy/product
test/scf/service/deploy/info
test/scf/doc
test/scf/bin
test/log2013.log
test/log2012.log
test/test3
[root@localhost soft]#
说明:
find [-path ..] [expression]
在路径列表的后面的是表达式
-path "test" -prune -o -print 是 -path "test" -a -prune -o -print 的简写表达式按顺序求值, -a
和 -o 都是短路求值,与 shell 的 && 和 || 类似如果
-path "test" 为真,则求值 -prune , -prune 返回真,与逻辑表达式为真;否则不求值 -prune,与逻辑
表达式为假。如果 -path "test" -a -prune 为假,则求值 -print ,-print返回真,或逻辑表达式为真
;否则不求值 -print,或逻辑表达式为真。
这个表达式组合特例可以用伪码写为:
if -path "test" then
-prune
else
实例2:避开多个文件夹:
命令:
find test \( -path test/test4 -o -path test/test3 \) -prune -o -print
输出:
[root@localhost soft]# find test \( -path test/test4 -o -path test/test3 \) -prune -o -print
test
test/log2014.log
test/log2015.log
test/scf
test/scf/lib
test/scf/service
test/scf/service/deploy
test/scf/service/deploy/product
test/scf/service/deploy/info
test/scf/doc
test/scf/bin
test/log2013.log
test/log2012.log
[root@localhost soft]# 说明:
圆括号表示表达式的结合。 \ 表示引用,即指示 shell 不对后面的字符作特殊解释,而留给 find 命令去解
释其意义。
实例3:查找某一确定文件,-name等选项加在-o 之后
命令:
find test \(-path test/test4 -o -path test/test3 \) -prune -o -name "*.log" -print
输出:
[root@localhost soft]#
find test \( -path test/test4 -o -path test/test3 \) -prune -o -name "*.log" -print
test/log2014.log
test/log2015.log
test/log2013.log
test/log2012.log
[root@localhost soft]#
.使用user和nouser选项:
按文件属主查找文件:
实例1:在$HOME目录中查找文件属主为peida的文件
命令:
find ~ -user peida -print
实例2:在/etc目录下查找文件属主为peida的文件:
命令:
find /etc -user peida -print
说明:
实例3:为了查找属主帐户已经被删除的文件,可以使用-nouser选项。在/home目录下查找所有的这类文件
命令:
find /home -nouser -print
说明:
这样就能够找到那些属主在/etc/passwd文件中没有有效帐户的文件。在使用-nouser选项时,不必给出用户名;
find命令能够为你完成相应的工作。
.使用group和nogroup选项:
就像user和nouser选项一样,针对文件所属于的用户组, find命令也具有同样的选项,为了在/apps目录下查找
属于gem用户组的文件,可以用:
find /apps -group gem -print
要查找没有有效所属用户组的所有文件,可以使用nogroup选项。下面的find命令从文件系统的根目录处查找这样
的文件:
find / -nogroup-print
.按照更改时间或访问时间等查找文件:
如果希望按照更改时间来查找文件,可以使用mtime,atime或ctime选项。如果系统突然没有可用空间了,很有可
能某一个文件的长度在此期间增长迅速,这时就可以用mtime选项来查找这样的文件。
用减号-来限定更改时间在距今n日以内的文件,而用加号+来限定更改时间在距今n日以前的文件。
希望在系统根目录下查找更改时间在5日以内的文件,可以用:
find / -mtime - -print
为了在/var/adm目录下查找更改时间在3日以前的文件,可以用:
find /var/adm -mtime + -print
.查找比某个文件新或旧的文件:
如果希望查找更改时间比某个文件新但比另一个文件旧的所有文件,可以使用-newer选项。
它的一般形式为:
newest_file_name ! oldest_file_name
其中,!是逻辑非符号。
实例1:查找更改时间比文件log2012.log新但比文件log2017.log旧的文件
命令:
find -newer log2012.log ! -newer log2017.log
输出:
[root@localhost test]# ll
总计
-rw-r--r-- root root - : log2012.log
-rw-r--r-- root root - : log2013.log
-rw-r--r-- root root - : log2014.log
-rw-r--r-- root root - : log2015.log
-rw-r--r-- root root - : log2016.log
-rw-r--r-- root root - : log2017.log
drwxr-xr-x root root - : scf
drwxrwxr-x root root - : test3
drwxrwxr-x root root - : test4
[root@localhost test]# find -newer log2012.log ! -newer log2017.log
.
./log2015.log
./log2017.log
./log2016.log
./test3
[root@localhost test]#
实例2:查找更改时间在比log2012.log文件新的文件
命令:
find . -newer log2012.log -print
输出:
[root@localhost test]# find -newer log2012.log
.
./log2015.log
./log2017.log
./log2016.log
./test3
[root@localhost test]#
.使用type选项:
实例1:在/etc目录下查找所有的目录
命令:
find /etc -type d -print
实例2:在当前目录下查找除目录以外的所有类型的文件
命令:
find . ! -type d -print
实例3:在/etc目录下查找所有的符号链接文件
命令:
find /etc -type l -print
.使用size选项:
可以按照文件长度来查找文件,这里所指的文件长度既可以用块(block)来计量,也可以用字节来计量。以字节计
量文件长度的表达形式为N c;以块计量文件长度只用数字表示即可。
在按照文件长度查找文件时,一般使用这种以字节表示的文件长度,在查看文件系统的大小,因为这时使用块来计
量更容易转换。
实例1:在当前目录下查找文件长度大于1 M字节的文件
命令:
find . -size +1000000c -print
实例2:在/home/apache目录下查找文件长度恰好为100字节的文件:
命令:
find /home/apache -size 100c -print
实例3:在当前目录下查找长度超过10块的文件(一块等于512字节)
命令:
find . -size + -print
.使用depth选项:
在使用find命令时,可能希望先匹配所有的文件,再在子目录中查找。使用depth选项就可以使find命令这样做。
这样做的一个原因就是,当在使用find命令向磁带上备份文件系统时,希望首先备份所有的文件,其次再备份子目
录中的文件。
实例1:find命令从文件系统的根目录开始,查找一个名为CON.FILE的文件。
命令:
find / -name "CON.FILE" -depth -print
说明:
它将首先匹配所有的文件然后再进入子目录中查找
.使用mount选项:
在当前的文件系统中查找文件(不进入其他文件系统),可以使用find命令的mount选项。
实例1:从当前目录开始查找位于本文件系统中文件名以XC结尾的文件
命令:
find . -name "*.XC" -mount -print
linux命令学习笔记(22):find 命令的参数详解的更多相关文章
- (转)live555学习笔记10-h264 RTP传输详解(2)
参考: 1,live555学习笔记10-h264 RTP传输详解(2) http://blog.csdn.net/niu_gao/article/details/6936108 2,H264 sps ...
- linux命令学习笔记( 2 ) : cd 命令
Linux cd 命令可以说是Linux中最基本的命令语句,其他的命令语句要进行操作,都是建立在使用 cd 命令上的. 所以,学习Linux 常用命令,首先就要学好 cd 命令的使用方法技巧. . 命 ...
- linux命令学习笔记( 7 ) : mv 命令
mv命令是move的缩写,可以用来移动文件或者将文件改名(move (rename) files),是Linux系统下常用的命令, 经常用来备份文件或者目录. .命令格式: mv [选项] 源文件或目 ...
- SNMP学习笔记之SNMP 原理与实战详解
原文地址:http://freeloda.blog.51cto.com/2033581/1306743 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法 ...
- 学习笔记--Grunt、安装、图文详解
学习笔记--Git安装.图文详解 安装Git成功后,现在安装Gruntjs,官网:http://gruntjs.com/ 一.安装node 参考node.js 安装.图文详解 (最新的node会自动安 ...
- ROS学习笔记六:xxx.launch文件详解
每当我们需要运行一个ROS节点或工具时,都需要打开一个新的终端运行一个命令.当系统中的节点数量不断增加时,每个节点一个终端的模式会变得非常麻烦.那么有没有一种方式可以一次性启动所有节点呢?答案当然是肯 ...
- 大数据学习笔记——Spark工作机制以及API详解
Spark工作机制以及API详解 本篇文章将会承接上篇关于如何部署Spark分布式集群的博客,会先对RDD编程中常见的API进行一个整理,接着再结合源代码以及注释详细地解读spark的作业提交流程,调 ...
- Java8学习笔记(五)--Stream API详解[转]
为什么需要 Stream Stream 作为 Java 8 的一大亮点,它与 java.io 包里的 InputStream 和 OutputStream 是完全不同的概念.它也不同于 StAX 对 ...
- Linux基础学习笔记以及常用命令
1.windows自带命令进入mysql所在磁盘 2.进入mysql安装目录的bin文件 D:\>cd D:\Program Files (x86)\mysql-5.5.25-winx64\ ...
- linux命令学习笔记(62)-curl命令-url下载工具
linux curl是一个利用URL规则在命令行下工作的文件传输工具.它支持文件的上传和下载,所以是综合 传输工具,但按传统,习惯称url为下载工具. 一,curl命令参数,有好多我没有用过,也不知道 ...
随机推荐
- hiho一下 第115周:网络流一•Ford-Fulkerson算法 (Edmond-Karp,Dinic,SAP)
来看一道最大流模板水题,借这道题来学习一下最大流的几个算法. 分别用Edmond-Karp,Dinic ,SAP来实现最大流算法. 从运行结过来看明显SAP+当前弧优化+gap优化速度最快. hi ...
- ios __block typeof 编译错误解决
type specifier missing a parameter list without types is only allowed in a function definition 解决: 工 ...
- 在mac上独立安装PHP环境
1.http://dditblog.com/blog_418.html 2.http://www.jianshu.com/p/0456dd3cc78b
- Lumen开发:lumen源码解读之初始化(4)——服务提供(ServiceProviders)与路由(Routes)
版权声明:本文为博主原创文章,未经博主允许不得转载. 前面讲了singleton和Middleware,现在来继续讲ServiceProviders和Routes,还是看起始文件bootstrap/a ...
- .net EF监控 MiniProfiler
1.从NuGet上下载所需要的包:MiniProfiler.mvc,MiniProfiler,MiniProfiler.ef 2.Global.asax 加入 protected void Appli ...
- Eureka集群搭建
服务注册.发现是微服务架构的关键原理之一,由于微服务架构是由一系列职责单一的细粒度服务构成的网状结构,服务之间通过轻量机制进行通信,这就必然引入一个服务注册发现的问题,也就是说服务提供方要注册报告服务 ...
- Arrays类--Arrays.asList()方法使用
java.util类 Arrays java.lang.Object——java.util.Arrays public class Arrays extends Object 此类包含用来操作数组(比 ...
- iOS解决导航引起视图高度问题
经过导航栏跨越的坑,总结出有两种方法可以无痕解决(前提>=iOS7版本)(TabBar与导航栏类似) 1.通过设置导航栏的透明度实现(这种方式的控制器view的起始坐标是充(0,64)开始的) ...
- AFN errorCode对应的状态码
转 http://blog.csdn.NET/wangyanchang21/article/details/50932191 在很多时候都会遇到错误, 还会带有一些 Error Code , 比如在各 ...
- jQuery-Ajax-Timeout属性不生效的问题
async必须设置为async:ture,timeout才生效: 如果设置为async:false,则锁住浏览器,禁止一切操作,直到请求有返回结果.