Finding files with find · 用 find 查找文件
This page needs a recent browser (with SharedArrayBuffer support). Please update Chrome, Edge, Firefox or Safari to the latest version. · 此页面需较新浏览器(支持 SharedArrayBuffer)。请升级 Chrome、Edge、Firefox 或 Safari 至最新版本。
English
Searching the whole tree
lsshows one folder.findsearches a folder and everything inside it, however deep:
.means "start in the current folder".findlists every file and folder it can reach.
中文
搜索整棵树
ls只显示一个文件夹。find会搜索一个文件夹及其内部的一切,无论藏得多深:
find .
.表示“从当前文件夹开始”。find会列出它能到达的每个文件和文件夹。
English
Find by name
- Usually you want files matching a pattern. Use
-name:
- This finds every
.txtfile in the tree — even ones buried three folders down. - Quote the pattern (
"*.txt") so the shell passes it tofinduntouched.
中文
按名字查找
- 通常你想要匹配某种模式的文件。用
-name:
find . -name "*.txt"
- 这会找出树中每一个
.txt文件 —— 哪怕它埋在三层文件夹之下。 - 给模式加上引号(
"*.txt"),让 shell 原封不动地把它交给find。
English
Why it is handy
- "Where did I put that file?" —
findanswers it in one line. - Combined with pipes, you can act on the results — for example count them with
wc -l.
中文
它为什么好用
- “我把那个文件放哪了?” ——
find一行就能回答。 - 配合管道,你还能对结果进一步处理 —— 比如用
wc -l数一数有多少个。
English
Common mistakes
find . -name "*.txt"searches by name from the current folder down.- Quote the pattern so the shell does not expand it first.
中文
常见错误
find . -name "*.txt"从当前目录向下按名字搜索。- 给模式加引号,免得 shell 先展开它。
Explore · 探索
find walks the tree · find 遍历目录树
find searches the whole tree below a directory. · find 搜索某个目录下的整棵树。
Find every .txt file, even deep inside subfolders, with find . -name "*.txt". (. means "start from here".) · 用 find . -name "*.txt" 找出每一个 .txt 文件,哪怕它藏在很深的子文件夹里。(. 表示“从这里开始”。)
You can also start the search from a subfolder. Find just the .txt files inside docs (and below) with find docs -name "*.txt". · 你也可以从某个子文件夹开始搜索。用 find docs -name "*.txt" 只查找 docs(及其子目录)里的 .txt 文件。