find 查找目录下的文件
1. 命令功能
find命令用于查到目录下的文件,同时也可以调用其它命令执行相应操作。
2. 语法格式
find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]
find [-H] [-L] [-P] [-Olevel] [pathname] [expression]
find [选项] [ 路径 ] [操作语句]
常见用法示例:
find path -option [ -print ] [ -exec -ok command ] {} \;
3. 使用范例
1.基础使用范例
范例1: 在/下查找test.txt文件
[root@localhost ~]# find / -name "test.txt"
/root/test.txt
/home/test.txt
范例2: 查找指定时间内修改过的文件,查找在/home目录下1天内访问过的文件
[root@localhost home]# find /home -atime -1 # -atime 表示在多少天访问过;-1:1天内
/home
/home/test.txt
/home/cxf
/home/cxf/.bash_history
/home/cxf/ok
/home/cxf/ok/a
....
范例3: 查找在/home 2天内修改过的文件
/home/cxf/ok/a/b
/home/cxf/ok/a/b/c
/home/cxf/dir2
/home/cxf/dir2/123.txt
/home/cxf/.lesshst
范例4 :用-name指定关键字查找
[root@localhost home]# find /home -mtime -1 -name "*.txt"
/home/test.txt
/home/cxf/dir2/123.txt
范例5: 利用“!”反向查找
[root@localhost home]# find /home -type d #在/home目录下查找目录 –type d表示目录
/home
/home/cxf
/home/cxf/ok
/home/cxf/ok/a
/home/cxf/ok/a/b
/home/cxf/ok/a/b/c
/home/cxf/dir2
/home/DIR
[root@localhost home]# find /home ! -type d #查找不是目录的文件
/home/test.txt
/home/cxf/.bash_history
/home/cxf/.bash_logout
/home/cxf/.bashrc
/home/cxf/.bash_profile
/home/cxf/dir2/123.txt
范例6:按照目录或文件的权限来查找文件 -perm
[root@localhost home]# find /home -perm 755
/home
/home/cxf/ok
/home/cxf/ok/a
范例7:按大小查找文件
[root@localhost home]# find . -size +1000c | xargs ls -lhd
drwxr-xr-x. 4 root root 4.0K Mar 14 22:45 .
drwx------. 4 cxf cxf 4.0K Mar 14 00:16 ./cxf
-rw-------. 1 cxf cxf 4.4K Mar 14 00:18 ./cxf/.bash_history
drwxr-xr-x. 2 root root 4.0K Mar 14 00:17 ./cxf/dir2
范例 8:查找文件时希望忽略某个目录
准备工作:
[root@localhost home]# mkdir /home/data/{dir1,dir2,dir3} -p
[root@localhost home]# ls -l ./data/
total 12
drwxr-xr-x. 2 root root 4096 Mar 15 22:01 dir1
drwxr-xr-x. 2 root root 4096 Mar 15 22:01 dir2
drwxr-xr-x. 2 root root 4096 Mar 15 22:01 dir3
[root@localhost home]# ls ./data/{dir1,dir2,dir3}
./data/dir1:
file1.txt
./data/dir2:
file2.txt
./data/dir3:
file3.txt
查找/home/data目录下的文件,但是需要忽略/home/data/dir2目录
查到对比:
- 查找/home/data目录下的内容
[root@localhost home]# find /home/data
/home/data
/home/data/dir3
/home/data/dir3/file3.txt
/home/data/dir2
/home/data/dir2/file2.txt
/home/data/dir1
/home/data/dir1/file1.txt
- 忽略/home/data/dir2目录
[root@localhost home]# find /home/data -path "/home/data/dir2" -prune -o -print
/home/data
/home/data/dir3
/home/data/dir3/file3.txt
/home/data/dir1
/home/data/dir1/file1.txt
参数说明:find /home/data -path "/home/data/dir2" -prune -o –print
find /home/data 是查找指定目录/home/data下的内容
-path “/home/data/dir2”是查找/home/data/dir2目录
-o 表示取并集不在当前指定目录下查找
-prune表示
-print将匹配的文件输出到标准输出(默认功能,使用中可省略)
范例9 :忽略多个目录
[root@localhost home]# find /home/data \( -path /home/data/dir1 -o -path /home/data/dir3 \) -prune -o -print
/home/data
/home/data/dir2
/home/data/dir2/file2.txt
范例10: 使用user和nouser选项
[cxf@localhost test]$ ls -lh
total 4.0K
drwxrwxr-x. 2 cxf cxf 4.0K Mar 15 22:30 cxf_dir
-rw-rw-r--. 1 cxf cxf 0 Mar 15 22:30 cxf.txt
[root@localhost home]# find /home/cxf -user cxf
/home/cxf
/home/cxf/.bash_history
/home/cxf/test
/home/cxf/test/cxf.txt
/home/cxf/test/cxf_dir
-user 表示有属主的文件;-nouser表示没有属主的文件。-nouser时查找哪些属主账号被删除的文件。 使用-nouser选项不必给出用户。
其中group和nogroup 与user用法一致。
范例11: find正则表达式
-name参数只支持“*”“?”“[]”这三个通配符,碰到复杂要求,就必须用到正则表达式。
find正则表达式语法如下:
find –pathname –repextype “type” –regex “pattern”
示例:
[root@localhost ~]# find / -regex ".*cxf"
/var/spool/mail/cxf
/home/cxf
范例:12: ls –l命令放在find命令的-exec选项中执行
[root@localhost home]# find . -type f -exec ls -l {} \;
-rw-------. 1 cxf cxf 4499 Mar 14 00:18 ./cxf/.bash_history
-rw-rw-r--. 1 cxf cxf 0 Mar 15 22:30 ./cxf/test/cxf.txt
-rw-r--r--. 1 cxf cxf 18 May 11 2016 ./cxf/.bash_logout
-rw-r--r--. 1 cxf cxf 124 May 11 2016 ./cxf/.bashrc
-rw-r--r--. 1 cxf cxf 176 May 11 2016 ./cxf/.bash_profile
-rw-------. 1 cxf cxf 51 Mar 13 11:39 ./cxf/.lesshst
-rw-r--r--. 1 root root 0 Mar 15 22:03 ./data/dir3/file3.txt
-rw-r--r--. 1 root root 0 Mar 15 22:03 ./data/dir2/file2.txt
-rw-r--r--. 1 root root 0 Mar 15 22:03 ./data/dir1/file1.txt
说明:-exec后面跟的是command命令,最后以分号(;)作为结束标志。为了不让分号产生歧义分号前用\转义。
{}的作用:指代前面find命令查找的内容
上面命令可以用:find . -type f |xargs ls –l 替换
find 查找目录下的文件的更多相关文章
- linux递归查找目录下所有文件夹以及文件
相对于ls或者ll,可能find在这个时候更加给力 先看我的目录结构 tree命令是查看目录的结构,而且最后会列出所有的directory的数目以及文件夹的数目...好像我们接下来要做的就没有必要了, ...
- linux 查找目录下的文件内容并替换(批量)
2.sed和grep配合 命令:sed -i s/yyyy/xxxx/g `grep yyyy -rl --include="*.txt" ./` 作用:将当前目录(包括子目录)中 ...
- [转载] linux查找目录下的所有文件中是否含有某个字符串
链接自 http://blog.sina.com.cn/s/blog_691a84f301015khx.html,并略加修订. 查找目录下的所有文件中是否含有某个字符串 find .|xargs gr ...
- linux查找目录下的所有文件中是否含有某个字符串
查找目录下的所有文件中是否含有某个字符串 find .|xargs grep -ri "IBM" find .|xargs grep -ri "IBM" -l ...
- 查找目录下的所有文件中是否含有某个字符串 linux
查找目录下的所有文件中是否含有某个字符串 find .|xargs grep -ri "IBM" 查找目录下的所有文件中是否含有某个字符串,并且只打印出文件名 find .|xar ...
- 用shell查找某个目录下最大文件
网上资料学习: 1.查找当前目录下最大文件(包括子目录里文件): find . -type f -exec stat -c "%s %n" {} \; | sort -nr | h ...
- linux查找目录下的所有文件中是否含有某个字符串 (转)
查找目录下的所有文件中是否含有某个字符串 find .|xargs grep -ri "IBM" 查找目录下的所有文件中是否含有某个字符串,并且只打印出文件名 find .|xar ...
- linux查找目录下的所有文件中是否含有某个字符串 <zhuan>
查找目录下的所有文件中是否含有某个字符串 find .|xargs grep -ri "IBM" 查找目录下的所有文件中是否含有某个字符串,并且只打印出文件名 find .|xar ...
- Linux查找和替换目录下所有文件中字符串(转载)
转自:http://rubyer.me/blog/1613/ 单个文件中查找替换很简单,就不说了.文件夹下所有文件中字符串的查找替换就要记忆了,最近部署几十台linux服务器,记录下总结. 查找文件夹 ...
随机推荐
- (10)python学习笔记一
学习参考博客:http://blog.csdn.net/a359680405/article/details/42486689 深表感谢 1.单行注释 # 多行注释 "" ...
- python3.0笔记
python文件头 #!/usr/bin/env python # -*- coding: utf- -*- ''' Created on 2017年5月9日 @author: Administrat ...
- [CSP-S模拟测试]:回家(塔尖)
题目传送门(内部题7) 输入格式 第一行一个整数$T$,表示共$T$组数据.对于每组数据,第一行两个数$n,m$表示有$n$个建筑物,$m$条道路.接下来$m$行,每行两个整数$u,v$,表示第$u$ ...
- 微信小程序之登录用户不是该小程序的开发者
当uniapp项目转为小程序项目的时候,预览会显示“您不是登录用户...”,所以没有办法发布,这个时候打开“project.config.json”设置一下appid,与你公众号appid一致就可以
- SecureCRT上传、下载文件 使用rz【上传】& sz【下载】命令
首先安装:apt-get install lrzsz SecureCRT这款SSH客户端软件同时具备了终端仿真器和文件传输功能.比ftp命令方便多了,而且服务器不用再开FTP服务了.rz,sz是便是L ...
- Android流媒体开发之路三:基于NDK开发Android平台RTSP播放器
基于NDK开发Android平台RTSP播放器 最近做了不少android端的开发,有推流.播放.直播.对讲等各种应用,做了RTMP.RTSP.HTTP-FLV.自定义等各种协议,还是有不少收获和心得 ...
- 使用thumbnailator给图片加水印
引入方式:Maven <dependency> <groupId>net.coobird</groupId> <artifactId>thumbnail ...
- (转) intellij idea部署web项目时的位置(Tomcat)
这篇文章说的比较好: 原文地址:https://blog.csdn.net/zmx729618/article/details/78340566 1.当你项目启动的时候console能看到项目运行的位 ...
- JMeter Gui – TestElement约定(转自约会言行的博客,链接:http://blog.csdn.net/yue530tomtom/article/details/77649872?locationNum=4&fps=1)
在编写任何JMeter组件时,必须注意某些特定的约定——如果JMeter环境中正确地运行JMeter组件,那么它将会运行.本部分描述了组件的GUI部分必须满足的约定. JMeter中的GUI代码严格地 ...
- javaScript 递归 闭包 私有变量
递归 递归的概念 在程序中函数直接或者间接调用自己. 跳出结构,有了跳出才有结果. 递归的思想 递归的调用,最终还是要转换为自己这个函数. 应用 function sum(n){ if(n == ...