1. Why Learn the Linux Command Line?¶
In Linux server management, the command line is the most direct and efficient way to operate. It does not require a graphical interface and can quickly execute complex tasks such as batch processing files, monitoring system status, and configuring services. For beginners, after mastering basic commands, you will find that the command line is more flexible than the graphical interface and can even perform operations that the graphical interface cannot achieve.
2. Basic File and Directory Operations¶
The core of the command line is executing operations through the structure of “command + parameters + object”. Let’s start with the most commonly used file/directory operations:
-
ls:List Directory Contents
- Purpose: Display files and folders in the current directory.
- Common Parameters:-l: Detailed information (shows permissions, size, modification time, etc.);-a: Show hidden files (files starting with.);- Example:
ls -la(shows all files, including hidden ones, with detailed information).
-
cd:Change Directory
- Purpose: Enter a specified directory.
- Common Paths:cd ~: Return to the user’s home directory (~is a shortcut symbol);cd ..: Return to the parent directory;cd /: Enter the root directory (the top level of the system, operate with caution);- Example:
cd Documents(enter the “Documents” folder in the current directory).
-
pwd:Show Current Path
- Purpose: Quickly check which directory you are currently in.
- Example: After executingpwd, it will output a path like/home/user/Documents. -
mkdir:Create Directory
- Purpose: Create a new folder.
- Example:mkdir test(create a folder named “test” in the current directory). -
touch:Create Empty File
- Purpose: Create an empty file (if the file already exists, it will update the modification time).
- Example:touch demo.txt(create an empty “demo.txt” file). -
rm:Delete File/Directory
- Purpose: Delete files or directories (DANGER! Cannot be directly restored after deletion).
- Common Parameters:-i: Prompt for confirmation before deletion;-r: Recursively delete directories (including subdirectories and files);- Examples:
- Delete a file:
rm -i demo.txt(will prompt “Do you want to delete?”, confirm to delete); - Delete a directory:
rm -rf test(force recursive deletion of the “test” directory, use with caution!).
-
cp:Copy File/Directory
- Purpose: Copy files or directories to the target location.
- Example:cp demo.txt new_demo.txt(copy “demo.txt” to “new_demo.txt”). -
mv:Move/Rename File
- Purpose: Move files/directories or rename files.
- Examples:- Rename:
mv old_name.txt new_name.txt; - Move:
mv new_name.txt /home/user/Documents(move to the Documents directory).
- Rename:
3. Efficiency-Doubling Practical Tips¶
Master these tips to significantly reduce repetitive operations:
-
Command Line Shortcuts (must remember!):
-Ctrl+C: Interrupt the currently executing command (e.g., when a command is stuck);
-Ctrl+D: Exit the current terminal (or close the current Shell session);
-Ctrl+Z: Pause the current command (pressfgto resume it to the foreground);
- Up/Down Arrows: Quickly review previously executed commands (pressCtrl+Rto search command history);
-Tab: Auto-complete commands/paths/file names (press Tab twice to list candidates). -
Wildcards: Batch File Processing
-*: Matches any number of characters (including 0);
-?: Matches a single character;
-[]: Matches any single character inside the brackets;
- Examples:ls *.txt: List all files ending with.txt;ls file?.txt: List files starting with “file”, followed by 1 character, then.txt(e.g.,file1.txt,fileA.txt);ls [0-9].txt: List files starting with a number and ending with.txt(e.g.,1.txt,5.txt).
-
Pipe
|:Combine Commands
- Purpose: Use the output of the previous command as the input of the next command (similar to “water flow”).
- Examples:ls | grep "txt": First list all files, then filter files containing “txt”;cat test.txt | wc -l: Count the number of lines in the “test.txt” file (wc -lcounts lines).
-
Run Commands in Background
- Purpose: Execute commands without occupying the foreground, and return to the terminal to continue operations.
- Method: Add&at the end of the command;
- Example:ping www.baidu.com &(run ping in the background, and the terminal can continue to enter other commands). -
View Command Help
- When you forget parameters, use--helpor themanmanual:ls --help: Show simple help for thelscommand;man ls: Open the detailed manual for thelscommand (pressqto exit).
-
History Command Management
-history: View all executed commands (filter commands containing “ls” withhistory | grep "ls");
-Ctrl+R: Real-time search of history commands (press Enter to execute after entering keywords).
4. Common Issues and Solutions¶
-
Permission Denied?
- If you encounter “Permission denied” when executing a command, usesudoto gain privileges (requires an administrator password):- Example:
sudo apt update(update system packages, requires root privileges).
- Example:
-
Command Execution Error?
- Check if the spelling is correct (e.g.,mkirshould bemkdir);
- If unsure about parameters, usecommand --helporman commandto check help. -
How to Exit the Terminal?
- Enterexitor pressCtrl+D.
5. Summary¶
The Linux command line seems complex but is actually a “toolkit” composed of simple commands. Start with basic file operations, gradually master skills like shortcuts, wildcards, and pipes to quickly improve efficiency. Remember: practice more, don’t be afraid of typing wrong commands (the command line has low trial and error cost). You’ll soon discover the power of the command line!
Recommended Learning Resources:
- Rookie Tutorial: Linux Command Line Introduction (simple and easy to understand);
- Online Practice Platform: Learn Linux Terminal (interactive practice).
(End)