Skip to content

Finding files with find

Linux & the command line Lesson 9 1:59 English narration · English + 中文 subtitles burned in

space play · ←/→ 5s · j/l 10s · f fullscreen · ,/. speed

Chapters

Transcript
You have known l s since lesson two, and it has one limit: it shows only what is right here, in this one folder. 你从第 2 课起就认识 ls 了,而它有一个限制: 它只显示"就在这里"的东西,就在这一个文件夹里。
Find does not stop there. find 不止于此。
It walks every folder below you, however deep — including the file three folders down that l s would never have shown you. 它会走遍你下面的每一个文件夹,不管多深—— 包括那个埋在三层下面、ls 永远不会显示给你的文件。
That is the difference, and it is the whole reason find exists. 这就是区别,也是 find 存在的全部理由。
The command has three parts, and each does one job. 这条命令有三个部分,每一部分做一件事。
Find says walk a tree. find 表示"走一棵树"。
The dot says starting here, in the folder you are in — you could put any path there instead. 那个点表示"从这里开始",从你所在的文件夹——你也可以在那里放任意路径。
And dash name with a pattern keeps the names that match. 而 -name 加上一个模式,保留匹配的那些名字。
Out come three files, each with the path you would need to reach it. 出来三个文件,每一个都带着你要到达它所需的路径。
Now lesson five comes back to bite. 现在第 5 课的内容回来咬人了。
If you leave the pattern unquoted, the shell expands it BEFORE find runs — into whatever happens to match in the current folder — and find then hunts for that one name everywhere. 如果你不给模式加引号,shell 会在 find 运行之前就把它展开—— 展开成当前文件夹里碰巧匹配到的东西—— 然后 find 就到处去找那一个名字。
The quotes hide it from the shell, so find gets the pattern itself and does the matching, folder by folder, as it walks. 引号把它藏起来不让 shell 看见, 于是 find 拿到的是模式本身,由它一边走一边逐个文件夹去匹配。
And because find prints one name per line, it goes into a pipe like everything else. 而因为 find 每行打印一个名字,它和别的东西一样可以接进管道。
Send it to w c dash l and you have how many there are — three text files anywhere under this folder. 把它送给 wc -l,你就得到了"有多少个"—— 这个文件夹底下任意位置的三个文本文件。
One name per line is exactly the shape every other tool expects, which is why these commands combine at all. 每行一个名字,正是其他每一个工具所期待的形状, 这也正是这些命令能够互相组合的原因。
Four things to take with you. 带走四点。
One: find walks every folder below where you start. 第一:find 走遍你出发点之下的每一个文件夹。
Two: a dot means start in the current folder. 第二:一个点表示从当前文件夹开始。
Three: quote the pattern so the shell leaves it alone. 第三:给模式加引号,让 shell 别去动它。
Four: find prints one name per line, ready for a pipe. 第四:find 每行打印一个名字,正好可以接进管道。
Now type the tasks into the terminal below. 现在把下面那些题目敲进终端里。

Log in or create account

IGCSE, A-Level & AP