Linux Command Line — 50 คำสั่งที่ SysAdmin ต้องรู้ ฉบับสมบูรณ์ 2026
ไม่ว่าคุณจะเป็น SysAdmin, DevOps Engineer, Developer หรือ Network Engineer — ถ้าทำงานกับ Linux คุณต้องเชี่ยวชาญ command line ผมทำงานกับ Linux มากว่า 30 ปี ตั้งแต่ Slackware 3.0 ในปี 1995 จนถึง Ubuntu 24.04 ในปัจจุบัน สิ่งที่ผมเรียนรู้คือ — คนที่เก่ง command line ทำงานเร็วกว่าคนที่ใช้ GUI 5-10 เท่า บทความนี้รวม 50 คำสั่งที่ผมใช้ทุกวัน พร้อมตัวอย่างจริงที่ copy ไปใช้ได้ทันที
📁 Section 1: File & Directory Management
1. ls — List files
# Basic
ls -la # list all + details
ls -lah # human-readable sizes
ls -lt # sort by time (newest first)
ls -lS # sort by size (largest first)
ls -R # recursive
ls -la --color=auto # colorized output
# ดูเฉพาะ directories
ls -d */
# ดูเฉพาะ hidden files
ls -la .??*
2. cd — Change directory
cd /var/log # absolute path
cd ../ # parent directory
cd ~ # home directory
cd - # previous directory (toggle)
cd # home directory (shortcut)
3. find — ค้นหาไฟล์
# หาไฟล์ตามชื่อ
find / -name "nginx.conf" 2>/dev/null
find /var/log -name "*.log" -mtime -7 # modified ภายใน 7 วัน
# หาไฟล์ขนาดใหญ่
find / -type f -size +100M -exec ls -lh {} \; 2>/dev/null
# หาและลบไฟล์เก่า
find /tmp -type f -mtime +30 -delete
# หาไฟล์ที่มี permission ผิด
find /var/www -type f ! -perm 644 -ls
find /var/www -type d ! -perm 755 -ls
# หาไฟล์ที่ถูกแก้ไขใน 24 ชั่วโมงที่ผ่านมา
find /etc -type f -mtime -1 -ls
4. cp, mv, rm — Copy, Move, Remove
# Copy
cp -r /source/dir /dest/dir # recursive copy
cp -a /source /dest # archive (preserve permissions, timestamps)
cp -v file1 file2 # verbose
# Move/Rename
mv oldname.txt newname.txt
mv /tmp/file /opt/app/
# Remove
rm -rf /tmp/old-data/ # recursive force (ระวัง!)
rm -i important-file.txt # interactive (ถามก่อนลบ)
5. mkdir, rmdir — Create/Remove directories
mkdir -p /opt/app/{config,data,logs,tmp} # สร้างหลาย dirs พร้อมกัน
mkdir -m 750 /opt/secure-dir # สร้างพร้อมตั้ง permission
6. ln — Symbolic links
ln -s /etc/nginx/sites-available/mysite /etc/nginx/sites-enabled/mysite
ln -sf /opt/app/v2 /opt/app/current # force overwrite existing link
📄 Section 2: Text Processing
7. grep — ค้นหาข้อความ
# Basic search
grep "error" /var/log/syslog
grep -i "error" /var/log/syslog # case-insensitive
grep -r "password" /etc/ # recursive
grep -rn "TODO" /opt/app/src/ # with line numbers
grep -c "404" /var/log/nginx/access.log # count matches
grep -v "DEBUG" app.log # exclude (invert)
# Regex
grep -E "^[0-9]{1,3}\.[0-9]{1,3}" access.log # IP addresses
grep -P "\d{4}-\d{2}-\d{2}" app.log # dates (Perl regex)
# Context
grep -A 3 "ERROR" app.log # 3 lines After
grep -B 3 "ERROR" app.log # 3 lines Before
grep -C 3 "ERROR" app.log # 3 lines Context (before + after)
8. awk — Text processing powerhouse
# Print specific columns
awk '{print $1, $4}' access.log # IP and timestamp
# Filter + calculate
awk '$9 == 500 {count++} END {print count}' access.log # count 500 errors
# Sum values
awk '{sum += $10} END {print sum/1024/1024 " MB"}' access.log # total bytes
# Top 10 IPs
awk '{print $1}' access.log | sort | uniq -c | sort -rn | head -10
# Custom delimiter
awk -F: '{print $1, $3}' /etc/passwd # username and UID
9. sed — Stream editor
# Replace text
sed -i 's/old/new/g' file.txt # in-place replace
sed -i 's|http://|https://|g' config.yml # use | as delimiter
# Delete lines
sed -i '/^#/d' config.conf # delete comment lines
sed -i '/^$/d' config.conf # delete empty lines
# Insert/Append
sed -i '1i\# This is a header' file.txt # insert at line 1
sed -i '$a\# This is a footer' file.txt # append at end
# Replace in specific line range
sed -i '10,20s/foo/bar/g' file.txt
10. cat, head, tail, less — View files
cat -n file.txt # with line numbers
head -50 file.txt # first 50 lines
tail -100 file.txt # last 100 lines
tail -f /var/log/syslog # follow (real-time)
tail -f /var/log/syslog | grep --line-buffered "error"
less +F /var/log/syslog # like tail -f but scrollable
11. sort, uniq, wc — Data processing
# Sort
sort -n file.txt # numeric sort
sort -k2 -t: /etc/passwd # sort by field 2
sort -rn -k5 ls-output.txt # reverse numeric sort by field 5
# Unique
sort file.txt | uniq # remove duplicates
sort file.txt | uniq -c | sort -rn # count + sort by frequency
# Word/Line count
wc -l file.txt # line count
wc -w file.txt # word count
find /opt/app/src -name "*.py" | xargs wc -l | tail -1 # total lines of code
ผู้เชี่ยวชาญแนะนำ - siamlancard
แนะนำ: | |
🎬 วิดีโอที่เกี่ยวข้อง — YouTube @icafefx
🖥 Section 3: System Monitoring
12. top / htop — Process monitoring
top -bn1 | head -20 # batch mode (for scripts)
htop # interactive (install: apt install htop)
# top shortcuts:
# P = sort by CPU
# M = sort by Memory
# k = kill process
# 1 = show per-CPU
13. ps — Process status
ps aux # all processes
ps aux | grep nginx # find specific process
ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%mem | head -20 # top memory users
ps -eo pid,ppid,cmd,%cpu --sort=-%cpu | head -20 # top CPU users
ps -ef --forest # process tree
14. free — Memory usage
free -h # human-readable
free -h -s 5 # refresh every 5 seconds
# Output:
# total used free shared buff/cache available
# Mem: 31Gi 12Gi 2.1Gi 256Mi 17Gi 18Gi
# Swap: 4.0Gi 0B 4.0Gi
# "available" คือ memory ที่ใช้ได้จริง (free + reclaimable cache)
15. df, du — Disk usage
df -h # filesystem usage
df -h / # specific mount
df -i # inode usage (สำคัญ!)
du -sh /var/log/* # size of each item
du -sh /var/log/ --max-depth=1 | sort -rh # sorted by size
du -sh /opt/app/ # total size of directory
# หา directories ที่ใหญ่ที่สุด
du -h / --max-depth=2 2>/dev/null | sort -rh | head -20
16. iostat — Disk I/O
iostat -x 1 5 # extended stats, every 1s, 5 times
# ดู: %util (ถ้าเกิน 80% = disk bottleneck)
# ดู: await (ถ้าเกิน 10ms = disk ช้า)
17. vmstat — Virtual memory stats
vmstat 1 10 # every 1s, 10 times
# ดู: si/so (swap in/out — ถ้ามีค่า = RAM ไม่พอ!)
# ดู: wa (I/O wait — ถ้าสูง = disk bottleneck)
18. dmesg — Kernel messages
dmesg | tail -50 # recent kernel messages
dmesg -T | grep -i error # errors with timestamps
dmesg | grep -i "out of memory" # OOM killer events
🌐 Section 4: Networking
19. ss — Socket statistics (แทน netstat)
ss -tlnp # TCP listening ports + process
ss -ulnp # UDP listening ports
ss -s # summary statistics
ss -tnp state established # established connections
ss -tnp | grep :443 | wc -l # count HTTPS connections
20. ip — Network interface management
ip addr show # show all interfaces
ip addr show eth0 # specific interface
ip route show # routing table
ip route get 8.8.8.8 # which route to use
ip link set eth0 up/down # enable/disable interface
ip neigh show # ARP table
21. curl — HTTP client
curl -I https://example.com # headers only
curl -s https://api.example.com/health # silent mode
curl -o file.zip https://example.com/file # download
curl -X POST -H "Content-Type: application/json" \
-d '{"key":"value"}' https://api.example.com/data
# Timing
curl -w "DNS: %{time_namelookup}s\nConnect: %{time_connect}s\nTTFB: %{time_starttransfer}s\nTotal: %{time_total}s\n" \
-o /dev/null -s https://example.com
# Follow redirects
curl -L -o page.html https://example.com
22. dig / nslookup — DNS lookup
dig example.com # A record
dig example.com MX # MX records
dig example.com +short # short output
dig @8.8.8.8 example.com # use specific DNS server
dig -x 1.2.3.4 # reverse DNS
23. ping, traceroute, mtr
ping -c 5 google.com # 5 pings
traceroute google.com # trace route
mtr google.com # combined ping + traceroute (interactive)
24. tcpdump — Packet capture
sudo tcpdump -i eth0 port 80 -n # HTTP traffic
sudo tcpdump -i eth0 host 10.0.1.10 -n # traffic to/from specific host
sudo tcpdump -i eth0 -w capture.pcap # save to file (open in Wireshark)
sudo tcpdump -i eth0 'tcp[tcpflags] & tcp-syn != 0' -n # SYN packets only
🔐 Section 5: Security & Permissions
25. chmod — Change permissions
chmod 755 script.sh # rwxr-xr-x
chmod 644 config.yml # rw-r--r--
chmod 600 id_rsa # rw------- (SSH key)
chmod -R 750 /opt/app/ # recursive
chmod +x script.sh # add execute permission
26. chown — Change ownership
chown www-data:www-data /var/www/ -R # change owner + group
chown -R appuser:appgroup /opt/app/
27. ufw — Firewall
sudo ufw status verbose
sudo ufw allow 22/tcp comment "SSH"
sudo ufw allow from 10.0.0.0/8 to any port 5432 comment "PostgreSQL internal"
sudo ufw deny from 203.0.113.0/24 # block IP range
sudo ufw enable
28. ssh — Secure Shell
ssh user@server # basic
ssh -i ~/.ssh/mykey.pem user@server # specific key
ssh -L 5432:db-server:5432 bastion # local port forwarding
ssh -D 1080 user@server # SOCKS proxy
ssh -J bastion user@internal-server # jump host
# SSH config (~/.ssh/config)
# Host prod
# HostName 10.0.1.10
# User deploy
# IdentityFile ~/.ssh/prod.pem
# ProxyJump bastion
(อ้างอิง: linux command คอ อะไร)
⚙️ Section 6: System Administration
29. systemctl — Service management
systemctl status nginx
systemctl start/stop/restart nginx
systemctl enable/disable nginx # auto-start on boot
systemctl list-units --type=service --state=running
systemctl list-timers # scheduled tasks
journalctl -u nginx -f # follow logs
journalctl -u nginx --since "1 hour ago"
30. crontab — Scheduled tasks
crontab -e # edit
crontab -l # list
# Format: minute hour day month weekday command
# ทุกวัน ตี 2
0 2 * * * /opt/scripts/backup.sh >> /var/log/backup.log 2>&1
# ทุก 5 นาที
*/5 * * * * /opt/scripts/health-check.sh
# ทุกวันจันทร์ 9:00
0 9 * * 1 /opt/scripts/weekly-report.sh
31-35: tar, gzip, rsync, scp, wget
# tar — Archive
tar czf backup.tar.gz /opt/app/ # create compressed
tar xzf backup.tar.gz # extract
tar xzf backup.tar.gz -C /opt/restore/ # extract to specific dir
tar tzf backup.tar.gz # list contents
# rsync — Sync files (better than scp)
rsync -avz --progress /local/dir/ user@server:/remote/dir/
rsync -avz --delete /source/ /dest/ # mirror (delete extra files)
rsync -avz --exclude='*.log' /source/ /dest/
# scp — Secure copy
scp file.txt user@server:/tmp/
scp -r /local/dir user@server:/remote/
# wget — Download
wget -c https://example.com/large-file.zip # resume download
wget -r -l 2 https://example.com/docs/ # recursive download
36-40: User & Process management
# useradd
sudo useradd -m -s /bin/bash -G sudo newuser
sudo passwd newuser
# kill
kill -15 PID # graceful (SIGTERM)
kill -9 PID # force (SIGKILL)
pkill -f "python app.py" # kill by name
killall nginx # kill all nginx processes
# nohup — Run in background
nohup python long-task.py > output.log 2>&1 &
# screen / tmux — Terminal multiplexer
tmux new -s mysession
tmux attach -t mysession
tmux ls
📌 บทความแนะนำจาก siamlancard: | |
🔧 Section 7: Advanced Commands
41. xargs — Build commands from input
# ลบไฟล์ .log ทั้งหมดที่เก่ากว่า 30 วัน
find /var/log -name "*.log" -mtime +30 | xargs rm -f
# Parallel execution
cat urls.txt | xargs -P 10 -I {} curl -s {} -o /dev/null -w "%{url}: %{http_code}\n"
42. jq — JSON processor
curl -s https://api.example.com/data | jq .
curl -s https://api.example.com/users | jq '.[0].name'
curl -s https://api.example.com/users | jq '.[] | select(.age > 30)'
cat config.json | jq '.database.host'
43. watch — Repeat command
watch -n 2 'df -h' # disk usage every 2s
watch -n 1 'ss -s' # connection stats every 1s
watch -d 'free -h' # highlight changes
44. lsof — List open files
lsof -i :80 # who's using port 80
lsof -u www-data # files opened by user
lsof +D /var/log/ # files opened in directory
lsof -p 12345 # files opened by PID
45. strace — System call tracer
strace -p 12345 # attach to running process
strace -c -p 12345 # summary of syscalls
strace -e trace=network -p 12345 # network calls only
46-50: One-liners ที่ใช้บ่อย
# 46. หา process ที่ใช้ port
sudo lsof -i :8080 -P -n | grep LISTEN
# 47. ดู disk I/O per process
sudo iotop -o
# 48. Generate random password
openssl rand -base64 32
# 49. ดู SSL certificate expiry
echo | openssl s_client -connect example.com:443 2>/dev/null | openssl x509 -noout -dates
# 50. Quick HTTP server
python3 -m http.server 8000 # serve current directory
🎯 Bonus: Useful Aliases
# เพิ่มใน ~/.bashrc หรือ ~/.zshrc
alias ll='ls -lah'
alias la='ls -A'
alias ..='cd ..'
alias ...='cd ../..'
alias ports='ss -tlnp'
alias myip='curl -s ifconfig.me'
alias duh='du -sh * | sort -rh | head -20'
alias psg='ps aux | grep -v grep | grep'
alias reload='source ~/.bashrc'
alias update='sudo apt update && sudo apt upgrade -y'
alias logs='sudo journalctl -f'
# Docker aliases
alias d='docker'
alias dc='docker compose'
alias dps='docker ps --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}"'
alias dlog='docker logs -f'
คำถามที่พบบ่อย (FAQ)
Q: ควรเรียน Bash หรือ Zsh?
A: เรียน Bash ก่อนเพราะเป็น default shell บน Linux servers ทุกตัว Zsh ใช้สำหรับ local development (มี Oh My Zsh ที่สะดวก) แต่ scripts ควรเขียนด้วย Bash เพื่อ portability
Q: man page อ่านยังไง?
A: man command เช่น man grep ใช้ /keyword เพื่อค้นหา q เพื่อออก หรือใช้ tldr command สำหรับ cheat sheet แบบสั้น
Q: Pipe (|) กับ Redirect (>, >>) ต่างกันอย่างไร?
A: Pipe ส่ง output ของ command หนึ่งเป็น input ของอีก command > เขียนทับไฟล์ >> append ต่อท้ายไฟล์
Q: ทำไมต้องใช้ 2>/dev/null?
A: 2 คือ stderr (error output) /dev/null คือ black hole ดังนั้น 2>/dev/null = ซ่อน error messages
Q: $? คืออะไร?
A: Exit code ของ command ก่อนหน้า 0 = สำเร็จ, อื่นๆ = error ใช้ใน scripts: if [ $? -eq 0 ]; then echo "success"; fi
สรุป — เริ่มฝึก Command Line วันนี้
50 คำสั่งนี้ครอบคลุม 95% ของงาน SysAdmin ประจำวัน เริ่มจากท่องคำสั่งพื้นฐาน (ls, cd, grep, find) จากนั้นฝึก text processing (awk, sed) และ networking (ss, curl, tcpdump) ภายใน 2 สัปดาห์คุณจะทำงานเร็วขึ้นอย่างเห็นได้ชัด
ถ้ามีคำถาม สอบถามได้ที่ SiamCafe Forum ครับ
💡 เรียนรู้เพิ่มเติม: | | — จาก ผู้เชี่ยวชาญประสบการณ์กว่า 13 ปี
🎬 ดูวิดีโอเพิ่มเติม
เรียนรู้ IT, Forex Trading และเทคนิค Server จากประสบการณ์จริง 30 ปี