Wildcards
Linux & the command line Lesson 5 2:05 English narration · English + 中文 subtitles burned in
Chapters
Transcript
A wildcard is a symbol that stands for "anything", and it turns one command into a command about many files.
通配符是一个代表"任意内容"的符号, 它把一条命令变成一条关于很多文件的命令。
Star dot t x t means every name ending in .txt — four of these six.
*.txt 表示所有以 .txt 结尾的名字——这六个里有四个。
File star means every name starting with file — three.
file* 表示所有以 file 开头的名字——三个。
And star dot j p g catches the jpg, on its own.
而 *.jpg 只抓到那个 jpg,就它一个。
There are two, and the difference shows up on exactly one of these names.
通配符有两个,而它们的区别正好体现在这些名字中的一个上。
A star matches any number of characters, even none.
星号匹配任意多个字符,哪怕一个都没有。
A question mark matches exactly one character — never zero, never two.
问号恰好匹配一个字符——不能是零个,也不能是两个。
So file one zero dot t x t has two characters where the question mark allows one, and only the star matches it.
所以 file10.txt 在问号只允许一个字符的位置上有两个字符, 只有星号能匹配它。
Here is the part that explains the danger.
下面这部分解释了危险从何而来。
The SHELL expands the pattern before the command runs.
是 shell 在命令运行之前把模式展开的。
You type r m star dot t x t; the shell turns that into a list of real names and hands it over.
你敲 rm *.txt;shell 把它变成一串真实的文件名,然后交过去。
So r m never sees a star — it just receives twelve filenames and deletes twelve files.
所以 rm 从来看不到那个星号—— 它只是收到了十二个文件名,然后删掉十二个文件。
It cannot warn you, because as far as it knows you typed all twelve yourself.
它没法警告你,因为在它看来,那十二个名字是你自己敲的。
So the habit is the same as last lesson, with the pattern in place of the name.
所以这个习惯和上一课一样,只是把名字换成了模式。
Run the pattern with l s first and read the list it prints.
先用 ls 跑一遍这个模式,读一读它打印出来的列表。
If that is what you meant, then delete it, with the same pattern.
如果那正是你想要的,再用同样的模式去删。
The l s shows you exactly the list r m is about to be given — which is the only warning you are going to get.
那条 ls 让你看到的,正是 rm 即将拿到的那份列表—— 而那也是你唯一能得到的警告。
Four things to take with you.
带走四点。
One: a star matches any run of characters, even none.
第一:星号匹配任意一段字符,哪怕一个都没有。
Two: a question mark matches exactly one.
第二:问号恰好匹配一个字符。
Three: the shell expands the pattern, not the command.
第三:展开模式的是 shell,不是那条命令。
Four: run it with l s before you run it with r m.
第四:在用 rm 之前,先用 ls 跑一遍。
Now type the tasks into the terminal below.
现在把下面那些题目敲进终端里。