The Linux operating system is widely used in servers, embedded systems, and personal computers due to its stability, security, and flexibility. Mastering common Linux commands is fundamental to efficiently using the Linux system. This article systematically introduces common Linux commands, covering file management, system monitoring, user management, and more.
1. File and Directory Management
File and directory management is the most basic and commonly used part of Linux commands. Mastering these commands allows for efficient operation of the file system.
1. Directory Navigation
# Display the current working directory
pwd
# Change directory
cd /path/to/directory
# Change to the user's home directory
cd ~
# Change to the parent directory
cd ..
# Change to the last directory
cd -
2. File and Directory Operations
# List directory contents
ls
# List in detail (including permissions, owner, size, etc.)
ls -l
# Show all files (including hidden files)
ls -a
# Display sorted by size
ls -lhS
# Create a directory
mkdir directory_name
# Recursively create multi-level directories
mkdir -p parent/child/grandchild
# Create an empty file
touch file.txt
# Copy files or directories
cp source destination
# Recursively copy directories
cp -r source_dir destination_dir
# Move or rename files/directories
mv old_name new_name
# Delete a file
rm file.txt
# Force delete (without prompt)
rm -f file.txt
# Delete a directory
rm -r directory
# Force delete a directory
rm -rf directory
3. Viewing File Contents
# View file contents
cat file.txt
# Show line numbers
cat -n file.txt
# View long files page by page
more file.txt
less file.txt
# View the first n lines of a file
head -n 10 file.txt
# View the last n lines of a file
tail -n 10 file.txt
# Real-time view of new content added to a file (commonly used for log monitoring)
tail -f logfile.log
2. File Permission Management
The Linux system ensures file security through permission control, making it crucial to understand and master permission management commands.
# View file permissions
ls -l file.txt
# Modify file permissions (symbolic method)
chmod u+rwx file.txt # Add read, write, and execute permissions for the owner
chmod g-w file.txt # Remove write permission for the group
chmod o=r file.txt # Give read-only permission to others
# Modify file permissions (numeric method)
chmod 755 file.txt # Owner: rwx, Group: r-x, Others: r-x
chmod 644 file.txt # Owner: rw-, Group: r--, Others: r--
# Recursively modify permissions of a directory and its contents
chmod -R 755 directory/
# Change file owner
chown user:group file.txt
# Recursively change directory owner
chown -R user:group directory/
Permission number meanings: r (4), w (2), x (1), adding the permission values for different roles gives the permission number.
3. System Monitoring and Management
Understanding system status and monitoring resource usage is part of a system administrator’s daily work.
1. Process Management
# View currently running processes
ps
# View all processes
ps aux
# Display processes in a tree structure
pstree
# Dynamically view processes (press q to exit)
top
# More powerful process viewing tool
htop
# Find process ID by name
pgrep process_name
# Terminate a process
kill PID
# Force terminate a process
kill -9 PID
# Terminate a process by name
pkill process_name
2. System Resource Monitoring
# View memory usage
free -h
# View disk usage
df -h
# View directory space usage
du -sh directory/
# View detailed space usage for each directory
du -h --max-depth=1 /path
3. System Information
# View kernel version
uname -r
# View complete system information
uname -a
# View system distribution information
cat /etc/os-release
# View CPU information
lscpu
# View system uptime and load
uptime
4. User and Group Management
Linux is a multi-user system, and user and group management is an important part of system security.
# View current logged-in user
whoami
# View all logged-in users
who
# Add a user
sudo adduser username
# Change user password
passwd username
# Delete a user
sudo userdel username
# Delete a user and their home directory
sudo userdel -r username
# Add a user group
sudo groupadd groupname
# Add a user to a group
sudo usermod -aG groupname username
# View groups a user belongs to
groups username
5. Network Management
In the network era, network configuration and diagnostic commands are essential tools.
# View network interface information
ip addr
ifconfig # Older command
# Test network connection
ping hostname/ip
# View DNS configuration
cat /etc/resolv.conf
# Display network routing table
route -n
ip route
# View network connection status
netstat -tuln
ss -tuln # More modern command
# View domain name resolution
nslookup domain.com
dig domain.com
# Download files
wget https://example.com/file.zip
curl -O https://example.com/file.zip
6. Compression and Archiving
Handling compressed files is a common task in daily work.
# Create a tar archive
tar -cvf archive.tar files/
# Extract tar archive
tar -xvf archive.tar
# Create a gzip compressed tar archive
tar -zcvf archive.tar.gz files/
# Extract gzip compressed tar archive
tar -zxvf archive.tar.gz
# Create a bzip2 compressed tar archive
tar -jcvf archive.tar.bz2 files/
# Extract bzip2 compressed tar archive
tar -jxvf archive.tar.bz2
# Compress zip files
zip archive.zip files/
# Extract zip files
unzip archive.zip
7. Searching and Finding
Quickly finding needed files or content in a large file system is key to improving efficiency.
# Find files in a specified directory
find /path/to/search -name "filename.txt"
# Find directories
find /path -type d -name "directoryname"
# Find files modified in the last 7 days
find /path -type f -mtime -7
# Find files larger than 100MB
find /path -type f -size +100M
# Search for text in a file
grep "search_term" file.txt
# Recursively search for text in a directory
grep -r "search_term" directory/
# Case-insensitive search
grep -i "search_term" file.txt
# Show line numbers of matching lines
grep -n "search_term" file.txt
8. Text Processing
Linux provides powerful text processing tools suitable for handling configuration files and log files.
# Sort text
sort file.txt
# Remove duplicates and sort
sort -u file.txt
# Count lines, words, characters
wc file.txt
# Find and replace text (without modifying the original file)
sed 's/old/new/' file.txt
# Find and replace text (modifying the original file)
sed -i 's/old/new/' file.txt
# Replace all matches
sed 's/old/new/g' file.txt
# Process text by column
awk '{print $1, $3}' file.txt # Print the 1st and 3rd columns
awk -F ',' '{print $2}' csvfile.txt # Process CSV file, print the 2nd column
9. Useful Tools and Shortcuts
Some useful tools and shortcuts can greatly improve work efficiency:
# View command history
history
# Execute the nth command in history
!n
# Execute the last command
!!
# View command location
which command
# Show command help
command --help
# View command manual
man command
# Pipe operation: use the output of one command as input for another
ls -l | grep ".txt"
ps aux | grep "java"
# Redirect output to a file
command > output.txt # Overwrite mode
command >> output.txt # Append mode
# Redirect standard error
command 2> error.log
# Run command in the background
command &
# Bring background command to the foreground
fg
Common shortcuts:
<span>Ctrl + C: Terminate the current command</span><span>Ctrl + Z: Pause the current command and put it in the background</span><span>Ctrl + D: Exit the current shell</span><span>Ctrl + L: Clear the screen</span><span>Tab: Auto-complete command or filename</span>
10. Conclusion
This article introduced the most commonly used commands in the Linux system, covering file management, permission control, system monitoring, user management, network configuration, and more. Mastering these commands is fundamental to effectively using the Linux system.
The key to learning Linux commands is practice, and readers are encouraged to gradually familiarize themselves with these commands through actual operations. As experience accumulates, you will find the power of Linux commands lies in their ability to be combined, using pipes and redirection to build powerful command chains to accomplish complex tasks.
For advanced learning, consider exploring shell scripting, which combines commonly used commands into scripts to automate tasks and further improve work efficiency.