find命令用来在指定目录下查找文件。任何位于参数之前的字符串都将被视为欲查找的目录名。如果使用该命令时,不设置任何参数,则find命令将在当前目录下查找子目录与文件。并且将查找到的子目录和文件全部进行显示。
1、查找当前目录中所有文件: find .
bjhl$ find .
.
./printable_docs
./printable_docs/index.html
./printable_docs/jmeter_irc.html
./printable_docs/changes.html
./printable_docs/issues.html
./printable_docs/mail.html
./printable_docs/changes_history.html
2、查找某个目录下的指定后缀文件:find /目录 -name "*.xlsx"
llll$ find Downloads/ -name "*.xlsx"
Downloads//第一个数据 (1).xlsx
Downloads//第二个数据20200101-20200201.xlsx
Downloads//第三个数据 (9).xlsx
Downloads//2019数据详情.xlsx
Downloads//2019数据详情 (1).xlsx
Downloads//第四个数据.xlsx
Downloads//第五个数据 (2).xlsx
3、查找某个目录底下忽略大小写的文件:find /目录 -iname "*.xlsx"
bjhl$ find Downloads/ -iname "*.xlsx"
Downloads//第一个数据 (1).xlsx
Downloads//第二个数据20200201.xlsx
Downloads//第三个数据 (9).xlsx
Downloads//第四个数据.xlsx
Downloads//第五个数据 (1).xlsx
Downloads//第六个数据.xlsx
Downloads//第七个数据 (2).xlsx
4、同事在同一目录下查询两个后缀名不一致的文件:find . /目录 -name "*.xlsx" -o -name "*.jpg"
bjhl$ find . Downloads/ -name "*.xlsx" -o -name "*.jpg"
Downloads//第一个数据 (1).xlsx
Downloads//第二个数据20200101.xlsx
Downloads//第三个数据 (9).xlsx
Downloads//第四个数据.xlsx
Downloads//第五个数据 (1).xlsx
Downloads//第六个数据.xlsx
Downloads//第七个数据 (2).xlsx
Downloads//12/1/8.jpg
Downloads//12/2/9.jpg
Downloads//12/3/10.jpg
Downloads//12/4/4.jpg
5、在当前目录下或者当前目录的子目录下查找后缀名不一的文件两种写法:find . /目录 /( -name "*.xlxs" -o -name "*.jpg" /)
~ llll$ find . -name "*.xlsx" -o -name "*.pdf"
./Desktop/学习/英语.xlsx
./Desktop/学习/语文.pdf
./Desktop/学习/数学.pdf
./Desktop/学习/政治.xlsx
./Desktop/\/物理.xlsx
~ llll$ find . \( -name "*.pdf" -o -name ".xlsx" \) #一定要注意前后括号与-name之间有空格
./Desktop/11.pdf
./Desktop/112.pdf
./Desktop/113.pdf
./Desktop/114.pdf
~ bjhl$ find . \( -name "*.txt" -o -name "*.pdf" \)
./.eclipse/org.eclipse.oomph.jreinfo/infos.txt
./gitTest/one.txt
./.local/share/virtualenvs/5.txt
./.local/share/virtualenvs/bjhl-A-cAkrVB/4.txt
./.local/share/virtualenvs/3.txt
./.local/share/virtualenvs/2.txt
./.local/share/1.txt
6、查找某个目录底下的文件或者目录:find /目录/ -path "*查找的关键字*"
~ llll$ find /etc/ -path "*man*"
/etc//manpaths
/etc//asl/com.apple.performance
/etc//manpaths.d
/etc//apache2/original/extra/httpd-manual.conf
/etc//apache2/extra/httpd-manual.conf
find: /etc//cups/certs: Permission denied
/etc//man.conf
7、在某个目录底下找出不是以某个后缀结尾的文件:find /目录 ! -name "*.xlsx"
~ llll$ find usr/ ! -name "*.txt" #寻找usr目录底下文件名后缀不为.txt的文件
usr//share/cups/mime/apple.types
usr//share/cups/mime/mime.types
usr//share/cups/mime/apple.convs
usr//share/cups/mime/mime.convs
usr//share/cups/profiles
usr//share/cups/data
8、根据文件类型进行文件搜索,语法为:find . type 参数,文件类型主要有以下几类:
~ llll$ find . type c
~ llll$ find . type d
~ llll$ find . type f
~ llll$ find . type s
更多内容,可参考:https://man.linuxde.net/find