Reading files
Linux & the command line Lesson 3 1:56 English narration · English + 中文 subtitles burned in
Chapters
Transcript
The simplest way to read a file is c a t, which prints the whole thing straight to the screen.
读一个文件最简单的方式是 cat,它把整个文件直接打印到屏幕上。
Four lines in the file, four lines of output.
文件里四行,输出就是四行。
The odd name is short for concatenate, because its other trick is joining files: give it two names and it prints them one after the other.
这个奇怪的名字是 concatenate(连接)的缩写, 因为它的另一个本事是把文件接起来: 给它两个文件名,它就会把它们一个接一个地打印出来。
For a long file you usually want a peek, not the whole thing.
对一个长文件,你通常只想瞄一眼,而不是看全部。
Head takes the first lines — here the first two.
head 取开头的几行——这里是前两行。
Tail takes the last one instead.
tail 则取最后的几行。
Both use dash n to say how many, and if you leave it off you get ten.
两者都用 -n 来说明取几行,不写的话默认是 10 行。
Tail is what you reach for on a log file, because the newest lines are at the end.
看日志文件时你会用 tail,因为最新的几行在末尾。
Sometimes you do not want the contents at all, only the size.
有时候你根本不想要内容,只想要大小。
W c — word count — measures a file.
wc——word count——用来度量一个文件。
Dash l counts lines, and it is the option you will use most: how many entries in this log, how many students in this list.
-l 数行数,而它是你最常用的选项: 这份日志里有多少条记录,这份名单上有多少个学生。
Dash w and dash c give words and characters.
-w 和 -c 给出词数和字符数。
So which one do you reach for?
那么该用哪一个?
If the file is short enough to read, cat.
如果文件短到可以直接读完,用 cat。
If you want the start of a long one, head.
如果你想看一个长文件的开头,用 head。
If you want the newest lines of a log, tail.
如果你想看日志最新的几行,用 tail。
And if you only want to know how big it is, w c.
而如果你只想知道它有多大,用 wc。
Running cat on a file of a million lines floods your screen — that is exactly what the other three are for.
对一个上百万行的文件跑 cat 会把屏幕刷爆—— 那正是另外三个存在的理由。
Four things to take with you.
带走四点。
One: cat prints a whole file to the screen.
第一:cat 把整个文件打印到屏幕上。
Two: head shows the first lines and tail the last.
第二:head 显示开头几行,tail 显示末尾几行。
Three: dash n chooses how many lines.
第三:-n 决定取多少行。
Four: w c dash l counts lines, dash w words, -c characters.
第四:wc -l 数行,-w 数词,-c 数字符。
Now type the tasks into the terminal below.
现在把下面那些题目敲进终端里。