Detailed Explanation and Usage Rules of Common Linux Commands
The Linux command line is a core tool for system administration, development, and operations. The following document provides a detailed introduction to 20+ common Linux commands, covering file management, system monitoring, network operations, permission control, and process management, with each command including syntax, options, examples, precautions, and best practice recommendations.
1. Basic Commands
1. <span>ls</span> – List directory contents
Syntax:<span>ls [options] [path]</span>Common Options:
- •
<span>-l</span>: Display detailed information (permissions, size, time, etc.) - •
<span>-a</span>: Show hidden files - •
<span>-R</span>: Recursively list subdirectories - •
<span>-h</span>: Display sizes in human-readable format (e.g.,<span>K/M/G</span>)
Example:
ls -l /home# Display detailed information of the /home directory
ls -a .# Show hidden files in the current directory
ls -R /etc# Recursively display contents of the /etc directory
Precautions:
- • If no path is specified, it defaults to listing the contents of the current directory.
- • Hidden files start with a
<span>.</span>(e.g.,<span>.bashrc</span>).
2. <span>cd</span> – Change directory
Syntax:<span>cd [path]</span>Example:
cd /usr/local# Enter the specified directory
cd ~# Enter the user's home directory
cd ..# Return to the previous directory
Precautions:
- •
<span>~</span>represents the current user’s home directory (e.g.,<span>/home/username</span>). - •
<span>.</span>represents the current directory, and<span>..</span>represents the parent directory.
3. <span>pwd</span> – Display current path
Syntax:<span>pwd</span>Example:
pwd# Output something like /home/user
Precautions:
- • No parameters, directly outputs the absolute path of the current working directory.
2. File and Directory Operations
4. <span>mkdir</span> – Create directory
Syntax:<span>mkdir [options] directory_name</span>Common Options:
- •
<span>-p</span>: Create directories recursively (including parent directories)
Example:
mkdir project# Create a single-level directory
mkdir -p a/b/c# Recursively create nested directories
Precautions:
- • When using
<span>-p</span>, even if the parent directory does not exist, it will be created automatically.
5. <span>touch</span> – Create empty file
Syntax:<span>touch filename</span>Example:
touch file.txt# Create an empty file
touch file1.txt file2.txt# Create multiple files
Precautions:
- • If the file already exists, it will update its timestamp (access time
<span>atime</span>and modification time<span>mtime</span>).
6. <span>cp</span> – Copy files or directories
Syntax:<span>cp [options] source_path target_path</span>Common Options:
- •
<span>-r</span>: Recursively copy directories - •
<span>-i</span>: Prompt before overwriting (if the target exists) - •
<span>-v</span>: Display detailed process
Example:
cp file.txt backup/# Copy file to backup directory
cp -r dir/ dir_copy/# Recursively copy directory
Precautions:
- • If the target path is a directory, the source file will be copied into that directory; if the target path is a file, the source file will be renamed to the target filename.
7. <span>mv</span> – Move or rename files
Syntax:<span>mv [options] source_path target_path</span>Example:
mv old_name.txt new_name.txt# Rename file
mv file.txt /tmp/# Move file to /tmp
Precautions:
- • If the target path exists, the source file will be renamed to the target filename.
- • Using the
<span>-i</span>option can avoid prompts when overwriting.
8. <span>rm</span> – Remove files or directories
Syntax:<span>rm [options] file/directory</span>Common Options:
- •
<span>-r</span>: Recursively remove directories - •
<span>-f</span>: Force removal (no prompt) - •
<span>-i</span>: Prompt before deletion
Example:
rm file.txt# Remove file
rm -rf dir/# Force remove directory and its contents
Precautions:
- •
<span>rm -rf</span>is a dangerous command; confirm the path to avoid accidental deletion. - • Using the
<span>trash</span>tool (e.g.,<span>trash-cli</span>) can prevent permanent deletion.
3. System Management
9. <span>top</span> – Real-time monitoring of system resources
Syntax:<span>top</span>Common Operations:
- • Press
<span>q</span>to exit - • Press
<span>1</span>to display all CPU cores - • Press
<span>k</span>to kill a specified process
Example:
top# View system resource usage
Precautions:
- •
<span>top</span>refreshes in real-time, defaulting to sort by CPU usage. - • Using
<span>htop</span>(requires installation) provides a more user-friendly interface.
10. <span>df</span> – View disk space
Syntax:<span>df [options]</span>Common Options:
- •
<span>-h</span>: Display in human-readable format - •
<span>-i</span>: Show inode usage
Example:
df -h# View disk space
df -i /# View inode usage of the root directory
Precautions:
- • The output includes information on the filesystem, size, used, available, and usage percentage.
11. <span>du</span> – View directory size
Syntax:<span>du [options] path</span>Common Options:
- •
<span>-s</span>: Show only total size - •
<span>-h</span>: Display in human-readable format
Example:
du -sh /var/log# View total size of /var/log
Precautions:
- •
<span>du</span>defaults to recursively counting the size of subdirectories;<span>-s</span>is used for summarizing.
4. Network Related Commands
12. <span>ping</span> – Test network connectivity
Syntax:<span>ping [options] target_address</span>Example:
ping google.com# Test connection to google.com
ping -c 4 192.168.1.1 # Send 4 requests and then exit
Precautions:
- •
<span>ping</span>uses ICMP protocol and may be blocked by firewalls. - •
<span>-c</span>specifies the number of requests to send.
13. <span>ifconfig</span> – Configure network interfaces (deprecated)
Syntax:<span>ifconfig [interface] [options]</span>Example:
ifconfig eth0# View eth0 interface information
ifconfig eth0 192.168.1.100 netmask 255.255.255.0 # Configure IP
Precautions:
- • In modern systems, the
<span>ip</span>command (e.g.,<span>ip a</span>) has gradually replaced<span>ifconfig</span>.
14. <span>ip</span> – Network management tool (recommended)
Syntax:<span>ip [subcommand] [options]</span>Common Subcommands:
- •
<span>ip a</span>: Show all network interfaces - •
<span>ip r</span>: View routing table
Example:
ip a show eth0# Show detailed information of eth0 interface
ip route add 192.168.2.0/24 via 192.168.1.1 # Add route
Precautions:
- •
<span>ip</span>is a replacement for<span>ifconfig</span>and<span>route</span>, with more powerful features.
5. Permission Management
15. <span>chmod</span> – Change file permissions
Syntax:<span>chmod [options] permissions file</span>Permission Notation:
- • Symbolic:
<span>u/g/o/a + -=rwx</span> - • Numeric:
<span>755</span>(<span>rwxr-xr-x</span>)
Example:
chmod +x script.sh# Add executable permission to file
chmod 755 directory/ # Set directory permission to rwxr-xr-x
Precautions:
- •
<span>u</span>: User (file owner),<span>g</span>: Group,<span>o</span>: Others,<span>a</span>: All. - • In numeric notation,
<span>4</span>(read),<span>2</span>(write),<span>1</span>(execute) combinations represent permissions.
16. <span>chown</span> – Change file owner
Syntax:<span>chown [options] user:group file</span>Example:
chown user:group file.txt# Change file owner and group
Precautions:
- • You need to use
<span>sudo</span>for elevated privileges; regular users cannot change ownership of other users’ files.
6. Process Management
17. <span>ps</span> – View process status
Syntax:<span>ps [options]</span>Common Options:
- •
<span>ax</span>: Show all processes - •
<span>ef</span>: Show detailed information
Example:
ps ax# View all processes
ps -ef | grep java # Find Java processes
Precautions:
- •
<span>ps</span>only shows the current snapshot; use<span>top</span>or<span>htop</span>for real-time monitoring.
18. <span>kill</span> – Terminate process
Syntax:<span>kill [signal] PID</span>Common Signals:
- •
<span>-9</span>: Force termination - •
<span>-15</span>: Normal termination (default)
Example:
kill 1234# Terminate process with PID 1234
kill -9 1234# Force terminate process
Precautions:
- • Force termination (
<span>-9</span>) may lead to data loss; prefer using the default signal.
7. Compression and Decompression
19. <span>tar</span> – Package/Unpackage files
Syntax:<span>tar [options] [file/directory]</span>Common Options:
- •
<span>-c</span>: Create package - •
<span>-x</span>: Unpackage - •
<span>-v</span>: Show progress - •
<span>-f</span>: Specify filename
Example:
tar -cvf archive.tar dir/# Package directory
tar -xvf archive.tar# Unpackage
Precautions:
- •
<span>tar</span>itself does not compress files; it needs to be combined with<span>gzip</span>(<span>.tar.gz</span>) or<span>bzip2</span>(<span>.tar.bz2</span>).
20. <span>gzip</span> / <span>gunzip</span> – Compress/Decompress <span>.gz</span> files
Example:
gzip file.txt# Compress to file.txt.gz
gunzip file.txt.gz# Decompress
Precautions:
- •
<span>gzip</span>will delete the original file; use<span>-c</span>to keep it.
8. Finding and Searching
21. <span>find</span> – Find files or directories
Syntax:<span>find [path] [expression]</span>Common Options:
- •
<span>-name</span>: Find by name - •
<span>-type</span>: Find by type (<span>f</span>for files,<span>d</span>for directories) - •
<span>-mtime</span>: Find by modification time
Example:
find /home -name "*.log"# Find all .log files
find . -type d# Find all directories
Precautions:
- • Use
<span>-exec</span>to combine with other commands to operate on results (e.g.,<span>find . -name "*.tmp" -exec rm -f {} \;</span>).
22. <span>grep</span> – Text search
Syntax:<span>grep [options] pattern file</span>Common Options:
- •
<span>-i</span>: Ignore case - •
<span>-r</span>: Recursively search directories - •
<span>-n</span>: Show line numbers
Example:
grep "error" file.log# Find lines containing "error"
grep -r "TODO" /project/# Recursively find TODO comments
Precautions:
- • Use
<span>egrep</span>or<span>fgrep</span>for extended regex or fixed string matching.
9. User Management
23. <span>useradd</span> / <span>userdel</span> – Add/Delete users
Example:
sudo useradd newuser# Add new user
sudo userdel newuser# Delete user
Precautions:
- •
<span>useradd</span>does not create a home directory by default; use<span>-m</span>option.
24. <span>passwd</span> – Change password
Example:
passwd username# Change user password
Precautions:
- • Regular users can only change their own passwords; administrators can change other users’ passwords.
10. Environment Variables and Scripts
25. <span>export</span> – Set environment variables
Example:
export PATH=$PATH:/opt/myapp
Precautions:
- • Environment variables only take effect for the current shell session; to make them permanent, write to
<span>~/.bashrc</span>or<span>/etc/environment</span>.
26. <span>bash</span> – Run scripts
Example:
bash script.sh# Run script
Precautions:
- • The script must have executable permissions (
<span>chmod +x script.sh</span>), or use<span>./script.sh</span>to run.
11. Advanced Commands
27. <span>xargs</span> – Build and execute command lines
Example:
find . -name "*.tmp" | xargs rm -f# Delete all .tmp files
Precautions:
- •
<span>xargs</span>will separate input by spaces; when processing long paths, it is recommended to use the<span>-0</span>option.
28. <span>rsync</span> – Synchronize files or directories
Example:
rsync -avz /source/ user@remote:/dest/# Synchronize to remote server
Precautions:
- •
<span>-a</span>: Archive mode (preserve permissions, time, etc.) - •
<span>-z</span>: Enable compression
12. Debugging and Logging
29. <span>tail</span> – View end of file
Example:
tail -f /var/log/syslog# Real-time track logs
Precautions:
- •
<span>-n</span>option specifies the number of lines (e.g.,<span>tail -n 100 file.txt</span>).
30. <span>journalctl</span> – View system logs (Systemd)
Example:
journalctl -u sshd# View sshd service logs
Precautions:
- •
<span>journalctl</span>is the logging tool for Systemd, supporting filtering by service, time, etc.
13. Precautions and Best Practices
- 1. Permission Operations: Be cautious when using
<span>sudo</span>to avoid accidental operations. - 2. File Deletion:
<span>rm -rf</span>will forcefully delete directories; confirm the path. - 3. Network Commands:
<span>ifconfig</span>may be replaced by<span>ip</span>in modern systems. - 4. Script Safety: Check the content before running scripts to avoid executing malicious code.
- 5. Log Management: Use
<span>logrotate</span>to regularly rotate log files to prevent disk from filling up.
14. Command Quick Reference
| Command | Usage |
<span>ls</span> |
List directory contents |
<span>cd</span> |
Change directory |
<span>cp</span> |
Copy files/directories |
<span>mv</span> |
Move/rename files/directories |
<span>rm</span> |
Remove files/directories |
<span>grep</span> |
Text search |
<span>find</span> |
Find files |
<span>top</span> |
View process resource usage |
<span>ping</span> |
Test network connection |
<span>tar</span> |
Package/unpackage files |
<span>chmod</span> |
Change file permissions |
By continuously learning and practicing, you will be able to proficiently use the Linux command line to handle various system management tasks. It is recommended to combine pipes <span>|</span> and redirection <span>></span> for complex operations (e.g., <span>grep "error" file.txt | wc -l</span>).