Linux Command Line Tools
This article serves as an introduction and learning guide to the core command line tools in Linux. These tools are key differentiators between Linux beginners and proficient users, mastering them will greatly enhance your efficiency and control.
We will categorize them into four groups for explanation:
- The “Three Musketeers” of Text Processing:
<span>grep</span>,<span>awk</span>,<span>sed</span> - Remote Management and File Transfer:
<span>ssh</span>,<span>scp</span> - Package Management Tools:
<span>apt</span>(Debian/Ubuntu),<span>yum</span>(RHEL/CentOS) - Development and Editing Tools:
<span>GCC</span>,<span>nano</span>,<span>vim</span>
1. The “Three Musketeers” of Text Processing: grep, awk, sed
These tools are used to search, filter, and transform data in text, making them powerful for handling logs, configuration files, and data streams.
1. <span>grep</span> – Global Regular Expression Print
- Core Functionality: Searches for lines in text that match specific patterns (strings or regular expressions).
- Analogy: The “Ctrl+F” of text search, but more powerful.
- Common Options:
<span>-i</span>: Ignore case<span>-v</span>: Invert match, only show lines that do not match<span>-n</span>: Show line numbers of matching lines<span>-r</span>or<span>-R</span>: Recursively search all files in a directory<span>-E</span>: Use extended regular expressions (equivalent to<span>egrep</span>)- Classic Examples:
grep "error" /var/log/syslog # Search for lines containing "error" in the system log grep -r "TODO" ~/myproject/ # Recursively search for "TODO" in the project directory ps aux | grep nginx # Find nginx processes in the process list
2. <span>sed</span> – Stream Editor
- Core Functionality: Filters and transforms text streams based on scripts (or commands). Particularly good at replacing and deleting operations.
- Analogy: A “find and replace” tool for text, but can be automated and batched.
- Common Commands:
<span>s/target/replacement/flags</span>: Replacement operation (most commonly used)<span>d</span>: Delete lines- Classic Examples:
sed 's/foo/bar/g' file.txt # Replace all occurrences of foo with bar in the file (g means global) sed '5d' file.txt # Delete the 5th line sed -i.bak 's/old/new/g' file.txt # Modify the file directly and create a backup file.txt.bak
3. <span>awk</span> – Text and Data Processing Language
- Core Functionality: A powerful programming language adept at processing column-based (field) text data. It automatically splits each line into fields based on spaces (or specified delimiters) (
<span>$1</span>,<span>$2</span>, …<span>$NF</span>). - Analogy: A “mini Excel” for text data, capable of calculating, counting, and reporting by columns.
- Basic Structure:
<span>awk 'pattern { action }' file</span> - Built-in Variables:
<span>NR</span>(current line number),<span>NF</span>(number of fields in the current line),<span>FS</span>(input field separator) - Classic Examples:
awk '{print $1}' file.txt # Print the first column of each line awk -F: '{print $1, $6}' /etc/passwd # Print the home directory of system users, separated by colons awk 'NR>=10 && NR<=20' file.txt # Print lines 10 to 20 of the file ps aux | awk '{sum += $4} END {print sum}' # Calculate the total percentage of memory usage by all processes
Learning Path Suggestion: Master <span>grep</span> first, then learn common replacement operations with <span>sed</span>, and finally tackle the most powerful <span>awk</span>.
2. Remote Management and File Transfer: ssh, scp
1. <span>ssh</span> – Secure Shell
- Core Functionality: An encrypted remote login protocol used to securely connect to another Linux server.
- Basic Usage:
<span>ssh username@host_ip_or_domain</span> - Advanced Usage:
- Key Pair Authentication (passwordless login): More secure than passwords.
- Specify Port:
<span>ssh -p 2222 username@host_ip</span>(if the server’s SSH port is not the default 22)
- Generate a key pair locally:
<span>ssh-keygen -t rsa</span> - Upload the public key to the server:
<span>ssh-copy-id username@host_ip</span>
ssh [email protected] # Log in to the server as root user
2. <span>scp</span> – Secure Copy
- Core Functionality: A file transfer tool based on SSH, used to securely copy files between local and remote hosts.
- Syntax:
<span>scp [options] source_file target_file</span> - Classic Examples:
# Copy from local to remote scp myfile.txt username@host_ip:/remote/directory/ # Copy from remote to local scp username@host_ip:/remote/file.txt /local/directory/ # Recursively copy an entire directory scp -r my_local_dir/ username@host_ip:/remote/path/
3. Package Management Tools: apt, yum
Used to install, update, and uninstall software from the system’s software repositories.Select one based on your distribution.
| Feature | <span>apt</span> (Debian/Ubuntu) |
<span>yum</span> (RHEL/CentOS 7) / <span>dnf</span> (RHEL/Rocky 8+) |
|---|---|---|
| Update Software Source List | <span>sudo apt update</span> |
<span>sudo yum check-update</span> / <span>sudo dnf check-update</span> |
| Upgrade All Software | <span>sudo apt upgrade</span> |
<span>sudo yum update</span> / <span>sudo dnf upgrade</span> |
| Install Software | <span>sudo apt install package_name</span> |
<span>sudo yum install package_name</span> |
| Uninstall Software | <span>sudo apt remove package_name</span> |
<span>sudo yum remove package_name</span> |
| Search Software | <span>apt search keyword</span> |
<span>yum search keyword</span> |
Core Concept: <span>update</span> updates the list of where software can be downloaded, while <span>upgrade</span> actually downloads and updates the software itself.
4. Development and Editing Tools: GCC, nano, vim
1. <span>GCC</span> – GNU Compiler Collection
- Core Functionality: Compiles source code from high-level languages like C and C++ into executable files.
- Basic Usage:
<span>gcc source.c -o executable_name</span> - Example:
# Write a C program cat > hello.c << EOF #include <stdio.h> int main() { printf("Hello, World!\n"); return 0; } EOF # Compile gcc hello.c -o hello # Run ./hello
2. <span>nano</span> – Simple and Easy-to-Use Text Editor
- Features: Extremely beginner-friendly, with all common commands displayed at the bottom of the screen.
- Basic Operations:
- Type text directly to edit.
<span>Ctrl + O</span>: Save file (Write Out).<span>Ctrl + X</span>: Exit.<span>Ctrl + W</span>: Search.- Learning Suggestion: If you are a beginner, start with
<span>nano</span>, you can immediately start editing files.
3. <span>vim</span> – Powerful Modal Editor
- Features: Steep learning curve, but once mastered, editing efficiency is extremely high. It is a standard tool for system administrators and developers.
- Core Concepts: Modes
- Normal Mode: The mode when first started, used for cursor movement, deletion, copying, pasting, and other command operations. Press
<span>Esc</span>to return to this mode at any time. - **Insert Mode**: You can only input text in this mode. Press
<span>i</span>to enter. - **Command-line Mode**: Press
<span>:</span>in normal mode to enter, used for saving, exiting, and other operations. - Essential Survival Commands:
<span>:w</span>Save<span>:q</span>Exit<span>:q!</span>Force exit without saving
- Start Vim:
<span>vim filename</span> - Press
<span>i</span>to enter insert mode and start editing. - Press
<span>Esc</span>to return to normal mode. - Type
<span>:wq</span>and press enter to save and exit.
Editor Selection Suggestion: Use <span>nano</span> for quick daily edits; if you aspire to become a professional, invest time in learning <span>vim</span>, it is worth your time.
Summary and Learning Roadmap
- Survival Stage: Master
<span>ssh</span>,<span>scp</span>,<span>apt/yum</span>,<span>nano</span>. Ensure you can connect to servers, install software, and edit files. - Proficient Stage: Master the basic operations of
<span>grep</span>,<span>sed</span>, and learn the survival commands of<span>vim</span>. Greatly enhance text manipulation efficiency. - Expert Stage: Delve into
<span>awk</span>programming, master various plugins and efficient navigation techniques in<span>vim</span>, and understand the compilation options of<span>GCC</span>.
These tools are the soul of Linux; through continuous practice, you will gradually experience the powerful capabilities and flexibility that the command line brings.

Spend a decade pulling yourself up,and by then the future will have arrived, and the stars will bloom.
● Various methods for implementing common page layouts in work
● Five methods for three-column responsive layout (fixed width on the sides, flexible in the middle)
● Summary of various methods for two-column adaptive layout
● Analysis of common two-column layout cases in work
● One hundred ways to achieve vertical centering layout
● Summary of several major methods for commonly used nine-grid layout
● Why does manipulating the DOM affect the performance of web applications?
● Six solutions for mobile scrolling penetration
● Summary of pitfalls in Vue + TypeScript
Let us walk together on the front-end path!
Long press the image below to identify the QR code, follow the public account and reply with [Join Group] to enter.