Introduction
In the previous article, we introduced the basic format of the Linux command line and the fundamental concepts of the Shell.
Getting Started with Linux: Entering the World of Command Line
In this article, we will delve deeper into the various categories of commonly used commands in Linux.
These commands are the foundation of Linux operations, essential for file management, system monitoring, and network configuration. By reading this article, you will be able to use the Linux system more efficiently for daily management and development tasks.
1. Directory and File Management Commands
These commands are primarily used for viewing, creating, moving, copying, and deleting directories and files, making them one of the most commonly used commands in Linux.
| Command | Brief Description |
|---|---|
<span>ls</span> |
View directory contents |
<span>cd</span> |
Change working directory |
<span>pwd</span> |
Display current path |
<span>mkdir</span> |
Create a new directory |
<span>rmdir</span> |
Remove empty directory |
<span>cp</span> |
Copy files or directories |
<span>mv</span> |
Move or rename files |
<span>rm</span> |
Delete files or directories |
<span>touch</span> |
Create an empty file or modify timestamp |
<span>tree</span> |
Display directory structure in a tree format |
🧩 Practical Case: Organizing Project Files
Assuming we are developing a C language project and need to categorize source files, header files, and compiled files.
mkdir project # Create a main directory named project
cd project # Enter the project directory
mkdir src include build # Create source, header, and build directories
touch src/main.c # Create the main program file in the src directory
touch include/main.h # Create the header file in the include directory
ls -R # Recursively list all directory and file structures
Thus, a standardized project directory structure is quickly established!
2. Search and Compression Commands
The commands for searching and compressing files in Linux are very powerful. They help us efficiently locate files, view contents, or package archives.
| Command | Brief Description |
|---|---|
<span>find</span> |
Find files by conditions |
<span>grep</span> |
Search for strings in files |
<span>locate</span> |
Quickly find file paths (depends on database) |
<span>tar</span> |
Package and unpack files |
<span>gzip</span> / <span>gunzip</span> |
Compress and decompress .gz files |
<span>zip</span> / <span>unzip</span> |
Compress and decompress .zip files |
🧩 Practical Case: Backing Up and Compressing Log Files
Assuming you have many log files in the /var/log/ directory and need to package them for backup.
cd /var/log # Enter the log directory
find . -name "*.log" > loglist.txt # Find all files ending with .log and record to loglist.txt
tar -czvf logs_backup.tar.gz *.log # Package and compress all log files into logs_backup.tar.gz
ls -lh logs_backup.tar.gz # Check the size of the generated compressed file
With these commands, you can easily complete the archiving and compression of logs.
3. System Management Commands
These commands are used to view system status, disk usage, memory usage, etc., and are the “toolbox” for system maintenance personnel.
| Command | Brief Description |
|---|---|
<span>top</span> |
View system processes and resource usage in real-time |
<span>df</span> |
Display disk space usage |
<span>du</span> |
Display space usage of files or directories |
<span>free</span> |
View memory usage |
<span>uptime</span> |
View system uptime and load |
<span>who</span> |
View currently logged-in users |
<span>uname -a</span> |
Display system information |
<span>shutdown</span> |
Shut down the system |
<span>reboot</span> |
Reboot the system |
🧩 Practical Case: Investigating Disk Space Usage
Assuming the system prompts that disk space is insufficient, we want to find the directory that occupies the most space.
df -h # View disk usage for each partition (human-readable format)
cd /home # Enter the user directory
du -sh * | sort -h # View the size of each subdirectory and sort
With the above commands, we can quickly locate the directory that occupies the most space and clean it up.
4. Network Tool Commands
Network-related commands are used to test connections, view ports, monitor network status, etc. This part is particularly important for server maintenance personnel.
| Command | Brief Description |
|---|---|
<span>ping</span> |
Test network connectivity |
<span>ifconfig</span> / <span>ip</span> |
View or configure network interfaces |
<span>netstat</span> / <span>ss</span> |
View network connection status and ports |
<span>curl</span> |
Send HTTP requests |
<span>wget</span> |
Download files from the network |
<span>scp</span> |
Remote file copy |
<span>ssh</span> |
Remote login to a server |
🧩 Practical Case: Checking Server Reachability
Assuming we want to check if the remote server example.com is online.
ping -c 4 example.com # Send 4 ICMP packets to the server to test connectivity
curl -I https://example.com # Check if the server can respond to HTTP requests normally
With the ping and curl commands, we can quickly determine whether the issue is network-related or a web service anomaly.
5. Process and Service Management Commands
These commands are used to view and terminate processes as well as manage background services, making them important tools for maintaining Linux system operations.
| Command | Brief Description |
|---|---|
<span>ps</span> |
View process status |
<span>top</span> |
View process activity dynamically |
<span>kill</span> |
Terminate a specified process |
<span>pkill</span> |
Terminate processes by name |
<span>systemctl</span> |
Manage system services |
<span>service</span> |
Control service start and stop |
<span>jobs</span> / <span>bg</span> / <span>fg</span> |
Manage background tasks |
🧩 Practical Case: Restarting the Nginx Service
Assuming our web server is experiencing access issues and we need to restart the Nginx service.
ps -ef | grep nginx # Check if the nginx process is running
sudo systemctl restart nginx # Restart the nginx service
sudo systemctl status nginx # Check the current status of the nginx service
With these few commands, we can quickly locate and restore the service, ensuring normal website access.
6. Conclusion
This article systematically presents the classification and functions of commonly used commands in Linux, and through practical cases, we understand their actual applications. Mastering these commands allows you to transition from a “beginner user” to a “proficient user”.
💬 Interactive Topic: What is your most commonly used Linux command? Or do you remember the first command you typed in the terminal? Feel free to share your stories in the comments section!
If you liked it, please consider following.