Saving output to files · 把输出保存到文件
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
Catching the output
- Normally a command's output appears on screen. The
>symbol redirects it into a file instead:
- Nothing prints — the text went into
greeting.txt. Read it back withcat greeting.txt.
中文
捕获输出
- 通常命令的输出会显示在屏幕上。
>符号会把它重定向到一个文件里:
echo hello > greeting.txt
- 屏幕上什么都不打印 —— 文字进到了
greeting.txt里面。用cat greeting.txt把它读回来。
English
Replace vs add
>replaces the file's contents (or creates it if new).>>appends — it adds to the end, keeping what was there:
- Use
>>when you are building up a file, like a log.
中文
覆盖与追加
>覆盖文件的内容(如果文件不存在则创建它)。>>追加 —— 它把内容加到末尾,保留原有的内容:
echo line one > notes.txt
echo line two >> notes.txt
- 当你在逐步构建一个文件(比如日志)时,就用
>>。
English
Why this matters
- Saving output lets you keep results, build files from commands, and feed one program's output to another.
- ⚠️ Watch out:
>on an existing file erases it first. Use>>when you mean "add".
中文
为什么这很重要
- 保存输出能让你留存结果、用命令生成文件,还能把一个程序的输出喂给另一个程序。
- ⚠️ 注意:对一个已存在的文件用
>会先把它清空。当你想“追加”时,请用>>。
English
Common mistakes
>overwrites a file;>>adds to the end.>on a file that already exists erases it first.
中文
常见错误
>覆盖文件;>>追加到末尾。- 对已存在的文件用
>会先清空它。
Send the output of echo into a file with >. Write hello into · 生成 greeting.txt, then cat it back. · 用 > 把 echo 的输出送进一个文件。把 hello 写入 greeting.txt,然后再用 cat 读回来。
>> adds a line instead of replacing. Write first into · 生成 log.txt, then append second, then show the file. · >> 是追加一行,而不是覆盖。先把 first 写入 log.txt,再追加 second,然后显示文件内容。