主要是实际工作中比较常用的一些,长期不玩,容易忘记,稍微整理回顾下
以下通过mac bash实践,linux可能不一样,man find
命令查看下
find
find是文件层次的查找(在路径树中的查找)
根据文件名称的查找
最常用,特别是模糊文件名的查找,工作中总是容易忘记具体是什么文件,记得很模糊
- 查找当前目录即其所有子目录下的 以
t
打头,.txt
结尾的文件(这里的文件指所有类型,当然包括了目录之类的文件)
find . -name "t*.txt"
- 控制目录查找深度
-maxdepth number
find . -name "t*" -maxdepth 1 # 一级目录
- 根据名字查找时,忽略大小写
-iname
find . -iname "t*.txt"
- 指定查找的文件类型
-type t
True if the file is of the specified type. Possible file types
are as follows:
b block special
c character special
d directory
f regular file
l symbolic link
p FIFO
s socket
find . -name "t*" -type d # 查找类型为目录类型
- 相反查找
-not
find . -not -name "t*"
根据文件大小查找
特别是大文件的查找,因为这些文件可能占用磁盘多,坑是一些无用的文件之类
- 查找超过100M的文件,并具体的显示出来
find . -size +100M -exec ls -lhG {} \;
grep
grep对文件内容层面的匹配查找
- 最常用的,单纯的匹配文件中的字符串,且没有正则,通配符之类的
grep include a.c # 或者 grep "include" a.c
- 通配符匹配
. 匹配任意一个字符
* 前一字符匹配0次或者任意多次
grep "in." a.c
grep "in.*" a.c
反向匹配
-v
忽略大小写
-i
利用管道,进行文件查找
ls | grep "t.*"
- 递归查找
-r or -R
mubideMacBook-Pro:mydata mubi$ grep "include" -r *
a.c:#include <stdio.h>
test/m1:include a
test/m1:include c
- 显示匹配内容在文件中的行号
-n
grep "include" -nr *
参考
[1] 31个实用find命令的案例.PengChonggui[EB/OL]http://blog.51cto.com/peenboo/2091203