Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> Linux 系統教學 >> Linux 教學

Linux 開發效率倍增器:tar、find、grep、readelf 四大神器實戰

編輯:Linux 教學

熟練掌握命令列工具是 Linux 開發者的核心競爭力。圖形介面雖直觀,但批量處理、自動化腳本、遠端維護等場景下,命令列的速度與彈性無可取代。本文精選四大高頻工具,附贈實戰範例速查表。

1. tar:打包壓縮瑞士刀

常用參數組合:

  • -c 建立、-x 解開、-t 列出、-v 顯示過程
  • 壓縮演算法:-z (gzip)、-j (bzip2)、-Z (compress)
  • -f 指定檔名、-C 指定解壓目標目錄
# 打包 /tmp 為 gzip 壓縮檔
tar -czf tmp.tar.gz /tmp

# 解壓至 /home
tar -xzf tmp.tar.gz -C /home

# 僅查看內容
tar -tzf tmp.tar.gz

2. find:檔案搜尋核彈頭

# 當前目錄遞歸找 1.txt
find . -name 1.txt

# /tmp 下找 1.txt
find /tmp -name 1.txt

# 模糊比對:所有 .c 且檔名含 test
find . -name "*test*.c"

# 進階:找 7 天前修改、大於 10M 的普通檔
find /var/log -type f -mtime +7 -size +10M

3. grep:內容搜尋與管道過濾王

# 單檔搜尋字串
grep "cams" test.txt

# 目錄遞歸搜尋
grep -r "cams" /root/cams

# 管道過濾:找含 ora 的進程
ps -ef | grep ora

# 進階:忽略大小寫、顯示行號、反向匹配
grep -in "error" app.log
grep -v "^#" config.cfg

4. readelf:ELF 二進位剖析利器

適用於除錯連結錯誤、檢查符號表、分析段屬性等進階場景。

# 完整資訊
readelf -a binary

# ELF Header
readelf -h binary

# 符號表
readelf -s binary

# 所有 Section 詳情
readelf -S binary

# Dump 特定 section(以 .rodata 為例)
readelf -p .rodata binary   # 字串形式
readelf -x .rodata binary   # 十六進位形式

實戰組合拳

# 找出所有 .c 檔中呼叫 malloc 但未檢查回傳值的行
find . -name "*.c" -exec grep -n "malloc" {} \; | grep -v "if.*malloc\|NULL.*malloc"

# 分析動態連結庫相依
readelf -d libfoo.so | grep NEEDED

工具不在多,在精。將這四大神器內化為肌肉記憶,開發除錯效率將質變。

Copyright © Windows教程網 All Rights Reserved