查看文件内容:`cat`、`less`、`more`
探索在终端中显示文件内容以及逐页浏览文件内容的不同命令。
查看文件内容:`cat`、`less`、`more` 是 CoddyKit 上的免费 Linux Command Line Mastery 课时。 这是第 1 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Linux Command Line Mastery 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Linux Command Line Mastery 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Welcome to File Viewing
When working in the Linux terminal, you'll often need to peek inside files to see their contents. Whether it's a small configuration file or a massive log, knowing how to view files is crucial.
In this lesson, we'll explore three fundamental commands: cat, less, and more. Each has its strengths for different scenarios.
`cat`: Displaying Whole Files
The cat command, short for 'concatenate', is primarily used to display the entire content of one or more files directly to your terminal's standard output.
It's best for small files, as it will print everything at once, potentially scrolling off your screen if the file is too large.
`cat` in Action
Let's say you have a small file named hello.txt. To view its content, you simply type cat followed by the filename:
cat hello.txt
# Expected Output (if hello.txt contains):
# Hello CoddyKit!
# This is a small file.`cat` for Creating Files
A less common but useful trick is using cat to quickly create a small file directly from your terminal input. You'll type your content and then press Ctrl+D to save and exit.
cat > new_message.txt
This is a new message.
It was created with cat.
(Press Ctrl+D here)
# Then to view it:
cat new_message.txt`cat`: Concatenating Files
As its name suggests, cat can also 'concatenate' or join multiple files. You can display them one after another, or redirect the combined output into a new file.
cat file1.txt file2.txt
# To combine into a new file:
cat file1.txt file2.txt > combined.txtWhen `cat` Falls Short
While cat is quick and simple, it's not suitable for large files. Imagine a log file with thousands of lines; cat would just blast it all onto your screen, making it impossible to read.
For such cases, we need a 'pager' that displays content one screen at a time.
`less`: The Powerful Pager
The less command is your go-to tool for viewing large files. It displays content page by page, allowing you to scroll, search, and navigate efficiently without loading the entire file into memory.
less large_log.log
# This will open large_log.log in a special viewer.Navigating in `less`
Once you open a file with less, you can use several keys to navigate:
- Spacebar: Scroll forward one page.
- b: Scroll backward one page.
- g: Go to the beginning of the file.
- G: Go to the end of the file.
- /text: Search forward for 'text'.
- ?text: Search backward for 'text'.
- q: Quit
less.
`less` with Piped Output
less is incredibly versatile and is often used with pipes (|) to view the output of other commands. This is perfect when a command generates a lot of output.
ls -l /etc | less
# This lists the contents of /etc, then pipes them to less for easy viewing.`more`: The Older Pager
The more command is an older file pager, similar to less. It also displays content one screen at a time, but it has fewer features.
The main difference is that more typically only allows you to scroll forward, not backward. You press Spacebar to advance and q to quit.
more old_document.txt
# This will open old_document.txt, allowing forward-only paging.Check Your Understanding
Let's test your knowledge of cat, less, and more.
Recap: Viewing File Content
Great job! You've learned the essentials of viewing file content in the Linux terminal:
cat: Best for quickly displaying small files or concatenating multiple files.less: The go-to pager for large files, offering powerful navigation, searching, and backward scrolling.more: An older pager, useful for viewing files page by page but typically limited to forward scrolling.
Choose the right tool for the right job to efficiently manage your files!
常见问题解答
「查看文件内容:`cat`、`less`、`more`」课时是免费的吗?
是的 — 「查看文件内容:`cat`、`less`、`more`」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Linux Command Line Mastery 课程的其余内容,请升级到 CoddyKit PRO。 Linux Command Line Mastery 课程共包含 4 节课。
「查看文件内容:`cat`、`less`、`more`」这节课中我会学到什么?
探索在终端中显示文件内容以及逐页浏览文件内容的不同命令。 你通过在浏览器中直接运行的动手代码来练习 Linux Command Line Mastery,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Linux Command Line Mastery 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Linux Command Line Mastery 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 4 节。
「查看文件内容:`cat`、`less`、`more`」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Linux Command Line Mastery 课中编写并运行代码吗?
能。每节 Linux Command Line Mastery 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 查看文件内容:`cat`、`less`、`more`
- 搜索文件:`find` 和 `locate`
- 权限与所有权:`chmod`、`chown`
- 创建与使用符号链接