name: cli-toolkit-cn version: 1.0.0 description: 命令列工具箱 - 常用命令速查、指令碼生成、效率提升。適合:開發者、運維工程師、終端愛好者。 metadata: openclaw: emoji: "⌨️" requires: bins: []
常用命令速查、指令碼生成、終端效率提升。
| 功能 | 描述 |
|---|---|
| 命令速查 | 常用命令快速查詢 |
| 指令碼生成 | 自動生成 Shell 指令碼 |
| 效率技巧 | 終端使用技巧 |
| 問題排查 | 常見錯誤解決 |
怎麼檢視 Linux 磁碟使用情況
生成一個批次重新命名檔案的指令碼
命令報錯 permission denied 怎麼解決
# 查詢檔案
find /path -name "*.txt" # 按名稱
find /path -type f -mtime -7 # 7天內修改
find /path -size +100M # 大於 100M
# 搜尋內容
grep -r "keyword" /path # 遞迴搜尋
grep -i "keyword" file.txt # 忽略大小寫
grep -A 5 -B 5 "keyword" file.txt # 顯示上下文
# 檔案操作
cp -r source/ dest/ # 複製目錄
mv oldname newname # 重新命名
rm -rf directory/ # 刪除目錄
touch file.txt # 建立檔案
mkdir -p path/to/dir # 建立多級目錄
# 檔案許可權
chmod 755 script.sh # 設定許可權
chmod +x script.sh # 新增執行許可權
chown user:group file # 更改所有者
# 檢視檔案
cat file.txt # 檢視全部
head -n 20 file.txt # 前 20 行
tail -n 20 file.txt # 後 20 行
tail -f log.txt # 即時檢視
less file.txt # 分頁檢視
wc -l file.txt # 統計行數
# 系統資訊
uname -a # 系統資訊
hostname # 主機名
uptime # 執行時間
date # 當前時間
cal # 日曆
# CPU 資訊
lscpu # CPU 詳情
nproc # CPU 核心數
top # 程序監控
htop # 增強版 top
# 記憶體資訊
free -h # 記憶體使用
cat /proc/meminfo # 詳細記憶體
# 磁碟資訊
df -h # 磁碟使用
du -sh * # 目錄大小
du -h --max-depth=1 # 一級目錄大小
lsblk # 塊裝置
7w4.net提供免費和付費技能下載。
# 檢視程序
ps aux # 所有程序
ps aux | grep python # 過濾程序
pgrep -f "python script.py" # 按名稱查詢
top # 動態監控
# 程序控制
kill PID # 終止程序
kill -9 PID # 強制終止
killall python # 按名稱終止
pkill -f "script.py" # 按命令匹配
# 後臺執行
nohup python script.py & # 後臺執行
nohup python script.py > log.txt 2>&1 & # 輸出到檔案
disown # 脫離終端
jobs # 檢視後臺任務
fg %1 # 前臺執行
# 網路資訊
ifconfig # 網路介面
ip addr # IP 地址
hostname -I # 本機 IP
curl ifconfig.me # 公網 IP
# 網路測試
ping google.com # 測試連通
curl -I https://example.com # HTTP 頭
wget https://example.com/file # 下載檔案
nc -zv host port # 測試埠
# 網路監控
netstat -tuln # 監聽埠
netstat -anp # 所有連線
lsof -i :80 # 佔用埠的程序
ss -tuln # 現代版 netstat
# sed 文本替換
sed 's/old/new/g' file.txt # 替換
sed -i 's/old/new/g' file.txt # 原地替換
sed '/pattern/d' file.txt # 刪除匹配行
# awk 文本處理
awk '{print $1}' file.txt # 列印第一列
awk -F, '{print $1,$2}' file.csv # CSV 處理
awk '{sum+=$1} END {print sum}' file.txt # 求和
# cut 提取欄位
cut -d, -f1,3 file.csv # 提取第 1,3 列
cut -c1-10 file.txt # 提取 1-10 字元
# sort 排序
sort file.txt # 排序
sort -n file.txt # 數字排序
sort -k2 -n file.txt # 按第 2 列數字排序
sort -u file.txt # 排序並去重
# uniq 去重
sort file.txt | uniq # 去重
sort file.txt | uniq -c # 統計重複次數
#!/bin/bash
# 批次重新命名:新增字首
for file in *.jpg; do
mv "$file" "prefix_$file"
done
# 或者使用 rename
rename 's/^/prefix_/' *.jpg
#!/bin/bash
# 批次處理檔案
for file in *.txt; do
echo "Processing $file..."
# 處理邏輯
sed -i 's/old/new/g' "$file"
done
#!/bin/bash
# 監控磁碟空間
THRESHOLD=80
ALERT_EMAIL="admin@example.com"
df -H | grep -vE '^Filesystem|tmpfs|cdrom' | awk '{ print $5 " " $1 }' | while read output; do
usep=$(echo $output | awk '{ print $1}' | cut -d'%' -f1)
partition=$(echo $output | awk '{ print $2 }')
if [ $usep -ge $THRESHOLD ]; then
echo "Warning: $partition is ${usep}% full" | mail -s "Disk Space Alert" $ALERT_EMAIL
fi
done
#!/bin/bash
# 自動備份
BACKUP_DIR="/backup"
SOURCE_DIR="/data"
DATE=$(date +%Y%m%d)
BACKUP_FILE="backup_$DATE.tar.gz"
# 建立備份
tar -czf "$BACKUP_DIR/$BACKUP_FILE" "$SOURCE_DIR"
# 刪除 7 天前的備份
find "$BACKUP_DIR" -name "backup_*.tar.gz" -mtime +7 -delete
echo "Backup completed: $BACKUP_FILE"
# 新增到 ~/.bashrc 或 ~/.zshrc
# 常用別名
alias ll='ls -lah'
alias la='ls -A'
alias l='ls -CF'
alias ..='cd ..'
alias ...='cd ../..'
alias grep='grep --color=auto'
alias fgrep='fgrep --color=auto'
alias egrep='egrep --color=auto'
# Git 別名
alias gs='git status'
alias ga='git add'
alias gc='git commit'
alias gp='git push'
alias gl='git log --oneline'
alias gd='git diff'
# 快捷命令
alias ports='netstat -tulanp'
alias myip='curl -s ifconfig.me'
alias weather='curl wttr.in'
alias sha='shasum -a 256'
alias www='python3 -m http.server 8000'
# 搜尋歷史
history | grep "keyword" # 搜尋歷史
Ctrl+R # 互動式搜尋
!! # 上一條命令
!$ # 上一條命令的最後一個引數
!* # 上一條命令的所有引數
!n # 第 n 條歷史命令
# 組合使用
cat file.txt | grep "error" | wc -l # 統計錯誤數
ps aux | grep python | awk '{print $2}' # 獲取 PID
find . -name "*.log" | xargs rm # 批次刪除
curl -s URL | jq '.data[].name' # JSON 處理
# 輸出重定向
command > output.txt # 覆蓋
command >> output.txt # 追加
command 2>&1 # 錯誤也輸出
command > /dev/null 2>&1 # 丟棄所有輸出
# 方案 1:新增執行許可權
chmod +x script.sh
# 方案 2:使用 bash 執行
bash script.sh
# 方案 3:使用 sudo
sudo ./script.sh
# 檢查 PATH
echo $PATH
# 查詢命令位置
which command_name
whereis command_name
# 安裝缺失命令
# Ubuntu/Debian
sudo apt install package_name
# CentOS/RHEL
sudo yum install package_name
# macOS
brew install package_name
# 查詢佔用程序
lsof -i :8080
# 終止程序
kill -9 $(lsof -t -i:8080)
# 查詢大檔案
find / -type f -size +100M 2>/dev/null
# 清理日誌
sudo rm -rf /var/log/*.log
# 清理快取
sudo apt clean # Ubuntu/Debian
sudo yum clean all # CentOS/RHEL
# 清理舊檔案
find /tmp -type f -mtime +7 -delete
建立:2026-03-12 版本:1.0
這是一份質量中等偏上的命令列速查工具,內容豐富、分類清晰,涵蓋常用命令、指令碼模板和問題解決方案,對開發者日常使用有一定幫助。但它本質上是一個靜態參考手冊,互動能力有限,無法根據具體場景提供針對性指導。適合有一定基礎的使用者快速查閱,但初學者可能需要更詳細的解釋和示例。整體而言,實用性強但智慧化程度有待提升。