1. Core Purpose & Concepts
- Core Purpose: To use a series of specialized commands to read, merge, reverse, and extract file content, all operations are performed in the command line without the need to launch a full text editor, thus achieving efficient file content preview and processing.
- Core Terminology:
<span>cat</span>(concatenate): Its core function is **”to connect and print”**. It can read the content of one or more files, concatenate them, and then output to standard output (usually the screen).<span>tac</span>(cat backwards):<span>cat</span>in reverse, its function is to vertically reverse the line order of the file (the last line is displayed first).<span>rev</span>(reverse): Horizontally reverses the character order of each line in the file (the last character is displayed first).<span>less</span>(Pager): A **”pager”** program used to view long files or long command outputs interactively, page by page, avoiding the problem of massive text scrolling.<span>head</span>: A command used to view the beginning part of a file or data stream, defaulting to display the first 10 lines.<span>tail</span>: A command used to view the end part of a file or data stream, defaulting to display the last 10 lines.
2. Key Command & Options
Overview of Core Commands
| Command | Syntax | Core Function | Key Options |
|---|---|---|---|
<span>cat</span> |
<span>cat [file1] [file2]...</span> |
Read and concatenate the entire content of files. | N/A |
<span>tac</span> |
<span>tac [file]</span> |
Vertically reverse the line order of the file. | N/A |
<span>rev</span> |
<span>rev [file]</span> |
Horizontally reverse the characters of each line. | N/A |
<span>less</span> |
<span>less [file]</span> |
Interactively view the file in a pager. | (Interactive keys, not options) |
<span>head</span> |
<span>head [options] [file]</span> |
Display the first N lines of a file. | <span>-n N</span>: Specify the number of lines to display. |
<span>tail</span> |
<span>tail [options] [file]</span> |
Display the last N lines of a file. | <span>-n N</span>: Specify the number of lines to display. |
<span>less</span> Common Keys in the Pager
<span>q</span>: Quit, return to the command line.- Arrow
<span>↑</span>/<span>↓</span>: Scroll up/down one line. <span>PageUp</span>/<span>PageDown</span>: Scroll up/down one page.<span>/</span>+<span>keyword</span>: Search down for the keyword.<span>?</span>+<span>keyword</span>: Search up for the keyword.<span>n</span>: Jump to the next search match.
Classic Command Examples
Bash
# Example 1: Merge three part files into a complete filecat part1.txt part2.txt part3.txt > story.txt
# Example 2: Safely view a potentially long system log file using lessless /var/log/syslog
# Example 3: View the result of a long command output, avoiding scrollingfind /etc -type f | less
# Example 4: Quickly view the first 20 lines of a configuration filehead -n 20 /etc/ssh/sshd_config
# Example 5: Monitor the latest dynamics of a log file in real-time (one of the most common usages) # -f option means "follow", which continuously displays new content added to the end of the filetail -f /var/log/nginx/access.log
# Example 6: Extract lines 11 to 20 of a file # First use head to get the first 20 lines, then use tail to get the last 10 from those 20 lineshead -n 20 some_file.txt | tail -n 10
3. Practical Use Cases
- Quick Preview: Use
<span>cat</span>to quickly view short configuration files, scripts, or notes to confirm the content is correct. - Reading Large Files:
<span>less</span>is the standard way to read large log files, code files, or any long text, as it does not occupy the terminal screen and allows for easy searching and navigation. - Log Analysis: Use
<span>head</span>to view the initial configuration information of log files, and use<span>tail</span>to view the latest errors or access records.<span>tail -f</span>is a powerful tool for real-time monitoring of service status. - Merging Documents: Use the
<span>cat</span>command to sequentially merge scattered text fragments (such as various chapters of a report) into a final document. - Pipeline Filtering: In complex command pipelines, use
<span>head</span>or<span>tail</span>to extract the parts of data you care about for further processing. For example,<span>sort -r data.txt | head -n 5</span>(find the top 5 largest values).
4. Common Pitfalls
- Using
<span>cat</span>on Large Files: This is the classic beginner’s mistake. Using<span>cat</span>on a log file of several hundred megabytes will instantly fill the terminal with massive text, potentially causing it to freeze. The correct approach is to always use<span>less</span>for files of unknown size. - Confusing
<span>tac</span>and<span>rev</span>:<span>tac</span>is the reverse of<span>cat</span>, dealing with the order of “lines” (vertical reversal);<span>rev</span>deals with the order of “characters” within lines (horizontal reversal). - Forgetting to Exit
<span>less</span>: After viewing a file, the user remains in the interactive interface of<span>less</span>, and must press<span>q</span>to return to the normal shell prompt. - Redirecting and Overwriting Files: Using
<span>cat a.txt b.txt > c.txt</span>will overwrite the content of<span>c.txt</span>if it already exists, without mercy. If your intention is to append content, you should use<span>>></span>, i.e.,<span>cat a.txt b.txt >> c.txt</span>.