Have you ever felt lost in front of the terminal? Facing the blinking cursor, unsure of what command to input? As the “dominant force” in the server domain, the Linux command line is an essential skill for developers and operations engineers. Compared to graphical interfaces, command line operations are more efficient, better suited for automation scripts, and serve as the core entry point for understanding Linux system logic. Today, we will outline 20 of the most commonly used Linux commands, along with practical tips and learning resources, to help you transition from a novice to an expert!
1
File and Directory Operations: The “Traffic Rules” of the Linux WorldIn the Linux system, everything is a file, and mastering file and directory operations is the first step to getting started. The following commands are like the routes you take every day to work; you must know them by heart.The Three Musketeers of Basic Navigation
- ls: Lists directory contents, equivalent to “viewing what is in the current folder”.
β Example: ls -l displays detailed information (permissions, size, modification time), ls -a shows hidden files (files starting with a dot).π‘ Tip: ll is an alias for ls -l, saving you three keystrokes and doubling your efficiency!
- cd: Changes directories, equivalent to “opening a folder”.
β Example: cd /home/user jumps to an absolute path, cd .. returns to the previous directory, cd ~ quickly returns to the home directory.π‘ Tip: cd – switches to the last directory you were in, just like a “back” button.
- pwd: Displays the current path, equivalent to “Where am I?”.
β Example: pwd outputs the absolute path directly, such as /home/user/documents.Essential File Operations
- mkdir: Creates a directory, equivalent to “creating a new folder”.
β Example: mkdir project creates a single directory, mkdir -p a/b/c recursively creates multiple levels of directories (without needing to manually create a and b).
- touch: Creates an empty file, equivalent to “creating a new text document”.
β Example: touch note.txt creates a blank file, touch file{1..5}.txt creates files from file1 to file5 in bulk.
- cp: Copies files/directories, equivalent to “copy and paste”.
β Example: cp file.txt /backup copies the file to the target path, cp -r dir1 dir2 recursively copies an entire directory.
- mv: Moves/renames files, equivalent to “cut and paste” or “rename”.
β Example: mv old.txt new.txt renames the file, mv file.txt ~/downloads moves it to the downloads folder.
- rm: Deletes files/directories, equivalent to “delete”; use caution with this dangerous operation!
β Example: rm file.txt deletes the file, rm -r dir recursively deletes the directory (including all contents), rm -f file.txt forcefully deletes without prompting.β οΈ Warning: rm -rf / is the command to delete the entire system; do not attempt!
2
System Monitoring and Management: Master Your Linux “Health Report”When you need to check system status, manage processes, or troubleshoot issues, these commands act like a doctor’s “stethoscope”, helping you quickly understand the health of your system.Process Management
- ps: View processes, equivalent to “Task Manager”.
β Example: ps aux displays detailed information about all processes (user, CPU usage, memory usage), ps -ef | grep python filters for Python processes.
- top/htop: Real-time monitoring of system resources; top is the basic version, htop is more intuitive (requires installation).
β Example: top shows the processes with the highest CPU and memory usage; press k and enter PID to terminate a process.
- kill: Terminates a process, equivalent to “end task”.
β Example: kill 1234 terminates the process with PID 1234, kill -9 1234 forcefully terminates (used for unresponsive processes).System Resource Viewing
- df: View disk space, equivalent to “viewing hard drive capacity”.
β Example: df -h displays in human-readable format (GB/MB), df -T shows the file system type (ext4/xfs).
- free: View memory usage, equivalent to “memory page in Task Manager”.
β Example: free -h shows total memory, used, and free; free -m displays in MB.
- uptime: View system uptime, equivalent to “how long has it been running”.
β Example: uptime outputs: 14:30:00 up 2 days, 3h, 2 users, load average: 0.50, 0.30, 0.20 (the lower the load, the better).
3
Practical Tips: “Shortcuts” to Double Your Command Line EfficiencyBy mastering these tips, you’ll find that the Linux command line isn’t so difficult, and can even be more efficient than graphical interfaces!Tab Completion: Reduce Input by 90%When entering commands or paths, press the Tab key for automatic completion. For example, typing cd /ho and then pressing Tab will automatically complete to cd /home; if there are multiple options, pressing Tab twice will display all possibilities.2. Command History: Repeat Execution Without Re-typing
- history: View command history; history | grep “ls” searches for historical commands containing “ls”.
- !!: Repeats the last command, for example, sudo !! executes the last command with sudo (very convenient if you forgot to add sudo).
- !n: Executes the nth command in history, such as !5 executes the 5th command.
- !$: References the last parameter of the previous command, for example, mkdir dir && cd !$ (creates a directory and then directly enters it).
3. Pipes and Redirection: Data “Great Migration”
- Pipe |: Uses the output of one command as the input for another command.
β Example: ls -l | grep “.txt” lists all txt files, ps aux | sort -k3nr sorts processes by CPU usage.
- Output Redirection >: Saves command results to a file (overwriting existing content).
β Example: ls -l > filelist.txt writes the file list to filelist.txt.
- Append Redirection >>: Appends content to a file (without overwriting).
β Example: echo “new line” >> notes.txt adds a line at the end of the file.
4
Recommended Learning Resources: The “Growth Roadmap” from Novice to ExpertIntroductory Books
- “The Linux Command Line” by William Shotts: A classic in Chinese, covering everything from system installation to detailed command explanations, suitable for building a foundational understanding from scratch.
- “Linux Command Line and Shell Scripting Bible” (2nd Edition): Focuses on the command line, covering bash scripting and text processing, with plenty of examples.
Online Websites
- Commandlinefu.com: A user-shared library of shell commands, sorted by popularity, searchable for practical tips.
- ExplainShell.com: Automatically parses command parameters when you input a command, for example, explain shell ls -l shows the meaning of each option.
- Linux China (linux.cn): A Chinese technical community with command tutorials, practical cases, and industry news.
Practical Platforms
- Experiment Station: Provides an online Linux environment, no local installation required, practice commands directly in the browser.
- Aliyun/Tencent Cloud: Purchase a student machine (as low as 9.9 yuan/month) to set up a real server environment for practice.
5
Conclusion: The Linux Command Line is More Than Just “Commands”The charm of the Linux command line lies in its efficiency and flexibility. From simple file operations to complex system management, from automation scripts to server operations, mastering these commands can greatly enhance your technical journey. Remember, “What is learned on paper is shallow; true understanding comes from practice”βthe best way to learn is to open the terminal and try each command one by one.What Linux commands have you used that doubled your work efficiency? Feel free to share your “secret tips” in the comments! If you found this article useful, don’t forget to like and share it, helping more people say goodbye to their Linux novice status~Further Reading: Want to dive deeper into learning shell scripts? In the next issue, we will launch “Write an Automatic Backup Script in 10 Minutes”; follow our public account “Tech Tribe” to not miss the updates!