Saving output to files · Lưu đầu ra vào tệp
This page needs a recent browser (with SharedArrayBuffer support). Please update Chrome, Edge, Firefox or Safari to the latest version. · Trang này cần trình duyệt gần đây (hỗ trợ SharedArrayBuffer). Vui lòng cập nhật Chrome, Edge, Firefox hoặc Safari lên phiên bản mới nhất.
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.
Tiếng Việt
Bắt đầu kết quả đầu ra
- Thông thường, kết quả của một lệnh sẽ hiển thị trên màn hình. Ký hiệu
>sẽ định hướng (redirect) chúng vào một tập tin thay vì vậy:
echo hello > greeting.txt
- Không có dòng nào được in ra — văn bản đã đi vào trong
greeting.txt. Đọc lại bằng cách dùngcat 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.
Tiếng Việt
Thay thế so với thêm vào
>thay thế nội dung của file (hoặc tạo mới nếu chưa tồn tại).>>thêm vào (append) — nó bổ sung vào cuối, giữ nguyên những gì đã có sẵn:
echo line one > notes.txt
echo line two >> notes.txt
- Sử dụng
>>khi bạn đang xây dựng dần một tập tin, chẳng hạn như nhật ký (log).
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".
Tiếng Việt
Tại sao điều này quan trọng
- Lưu kết quả đầu ra cho phép bạn giữ lại các kết quả, tạo tập tin từ các lệnh, và cung cấp đầu ra của chương trình này làm đầu vào cho chương trình khác.
- ⚠️ Cẩn thận: Khi dùng
>trên một tập tin đã tồn tại, nó sẽ xóa tập tin đó đi trước. Hãy dùng>>khi ý bạn là "thêm vào".
English
Common mistakes
>overwrites a file;>>adds to the end.>on a file that already exists erases it first.
Tiếng Việt
Lỗi thường gặp
>ghi đè lên tập tin;>>thì thêm vào cuối.- Khi dùng
>trên một tập tin đã tồn tại, nó sẽ xóa tập tin đó đi trước.
Send the output of echo into a file with >. Write hello into greeting.txt, then cat it back. · Gửi đầu ra của echo vào một tệp với >. Viết hello vào greeting.txt, sau đó read cat nó trở lại.
>> adds a line instead of replacing. Write first into log.txt, then append second, then show the file. · >> thêm một dòng thay vì ghi đè. Viết first vào log.txt, sau đó append second, rồi hiển thị tệp.