Comprehensive Guide to Common Linux Commands

1. File and Directory Operations

Command Function Common Parameters Example
<span>ls</span> List directory contents <span>-l</span>(detailed) <span>-a</span>(hidden files) <span>-h</span>(human-readable sizes) <span>ls -lah /home</span>
<span>cd</span> Change directory <span>..</span>(parent directory) <span>~</span>(home directory) <span>cd ~/Documents</span>
<span>pwd</span> Display current path <span>pwd</span>
<span>mkdir</span> Create directory <span>-p</span>(create parent directories) <span>mkdir -p project/{src,doc}</span>
<span>cp</span> Copy files/directories <span>-r</span>(recursive) <span>-v</span>(verbose) <span>cp -rv data/ backup/</span>
<span>mv</span> Move/Rename <span>-i</span>(prompt before overwrite) <span>mv old.txt new.txt</span>
<span>rm</span> Delete files <span>-r</span>(recursive) <span>-f</span>(force) ⚠️ Use with caution <span>rm -rf tmp/*</span>
<span>find</span> Search for files <span>-name</span>(name) <span>-type</span>(type) <span>find / -name "*.log"</span>
<span>grep</span> Text search <span>-i</span>(ignore case) <span>-r</span>(recursive) <span>grep -r "error" /var/log</span>

2. Viewing and Editing File Contents

Command Function Common Operations
<span>cat</span> Display file contents <span>cat file.txt</span>
<span>less</span> View file page by page <span>/keyword</span>(search) <span>q</span>(quit)
<span>head</span> Display the beginning of a file <span>head -n 20 file.log</span>
<span>tail</span> Display the end of a file <span>tail -f app.log</span> ⭐ Real-time log tracking
<span>vim</span> Text editor <span>i</span>(insert mode) <span>:wq</span>(save and exit) <span>:q!</span>(force quit)
<span>nano</span> Simple editor <span>Ctrl+O</span>(save) <span>Ctrl+X</span>(exit)

3. Permissions and User Management

Command Function Key Notes
<span>chmod</span> Change permissions <span>chmod 755 script.sh</span> (rwxr-xr-x)
<span>chown</span> Change owner <span>chown user:group file</span>
<span>sudo</span> Execute with elevated privileges <span>sudo apt update</span> ⭐ Temporarily gain root privileges
<span>passwd</span> Change password <span>passwd</span> (current user) <span>passwd username</span> (admin)
<span>useradd</span> Add user <span>useradd -m -s /bin/bash alice</span>
<span>usermod</span> Modify user <span>usermod -aG sudo bob</span> ⭐ Add sudo privileges

4. System Monitoring and Process Management

Command Function Useful Parameters
<span>top</span> Dynamic process monitoring <span>P</span>(sort by CPU) <span>M</span>(sort by memory)
<span>htop</span> Enhanced version of top ⭐ Visual operation (requires installation)
<span>ps</span> Process snapshot `ps aux grep nginx`
<span>kill</span> Terminate process <span>kill -9 PID</span> ⚠️ Force termination
<span>df</span> Disk space <span>df -h</span> (human-readable format)
<span>free</span> Memory usage <span>free -m</span> (in MB)

5. Network Tools

Command Function Classic Usage
<span>ping</span> Network connectivity <span>ping -c 4 google.com</span>
<span>ifconfig</span>/<span>ip</span> Network configuration <span>ip addr show</span>
<span>netstat</span>/<span>ss</span> Network connections <span>ss -tunlp</span> ⭐ View port usage
<span>curl</span> Network request <span>curl -I https://baidu.com</span> (only headers)
<span>wget</span> File download <span>wget -c url</span> ⚡ Resume download
<span>scp</span> Secure copy <span>scp file user@remote:/path</span>

6. Package Management

System Command Function
Debian/Ubuntu <span>apt update</span> Update source list
<span>apt install nginx</span> Install software
<span>apt remove --purge pkg</span> Completely uninstall
CentOS/RHEL <span>yum install httpd</span> Install software
<span>dnf groupinstall "Development Tools"</span> Install development suite

7. Compression and Decompression

Format Compression Command Decompression Command
.tar <span>tar -cvf archive.tar dir/</span> <span>tar -xvf archive.tar</span>
.gz <span>gzip file</span> <span>gunzip file.gz</span>
.tar.gz <span>tar -zcvf archive.tar.gz dir/</span> <span>tar -zxvf archive.tar.gz</span>
.zip <span>zip -r archive.zip dir/</span> <span>unzip archive.zip</span>

8. Shell Tricks

Command Function Example
` ` Pipe `cat log.txt grep “error”`
<span>></span> Redirect output <span>ls > files.txt</span> ⚠️ Overwrite file
<span>>></span> Append output <span>echo "new" >> file.txt</span>
<span>&&</span> Command chaining <span>make && make install</span>
<span>alias</span> Create alias <span>alias ll='ls -alh'</span> ⭐ To make permanent, write to <span>~/.bashrc</span>

9. Scheduled Tasks

Command Function Example
<span>crontab -e</span> Edit scheduled tasks <span>* * * * * /path/script.sh</span>
<span>crontab -l</span> View task list
<span>at</span> One-time scheduled task `echo “cmd” at 02:00`

10. System Information

Command Function Example Output
<span>uname -a</span> System information <span>Linux server 5.15.0-xx-generic</span>
<span>lsb_release -a</span> Distribution information <span>Ubuntu 22.04 LTS</span>
<span>lscpu</span> CPU information Number of cores/architecture/frequency
<span>lshw</span> Hardware overview ⚡ Requires root privileges

📌 Pitfall Tips:

  1. <span>rm -rf /</span> willdestroy the system! Always confirm the path before deleting

  2. <span>chmod 777</span> lowers security; it is recommended to follow the principle of least privilege

  3. Use <span>sudo</span> with caution in production environments to avoid accidental operations

Leave a Comment