Developer Tools

คำสั่ง Linux Command Line พื้นฐานที่นักพัฒนาทุกคนควรรู้

คู่มืออ้างอิงเชิงปฏิบัติสำหรับคำสั่ง Linux ที่ใช้งานประจำวันในการพัฒนา — การนำทางระบบไฟล์, สิทธิ์การเข้าถึง, การจัดการ process, การประมวลผลข้อความ, และระบบเครือข่าย

9 นาทีในการอ่าน

Terminal with green text on a dark screen

Linux command line คืออินเทอร์เฟซสากลสำหรับเซิร์ฟเวอร์, คอนเทนเนอร์, CI pipelines, และสภาพแวดล้อมการพัฒนา ไม่ว่าคุณจะ SSH เข้าไปยัง production server ตีสองตอนดึก หรือกำลัง debug Docker container ความคล่องแคล่วกับคำสั่งเหล่านี้คือความแตกต่างระหว่างความสับสนกับความมั่นใจ

การนำทางและจัดการไฟล์

# Navigate directories
pwd                      # print working directory
cd /var/log              # absolute path
cd ..                    # up one level
cd -                     # go back to previous directory
cd ~                     # home directory

# List files
ls                       # basic listing
ls -la                   # long format + hidden files
ls -lh                   # human-readable file sizes
ls -lt                   # sort by modification time (newest first)

# Create, copy, move, delete
mkdir -p path/to/dir     # create directory + parents
cp -r source/ dest/      # recursive copy
mv oldname newname       # rename or move
rm -rf directory/        # delete directory and contents (irreversible!)
touch file.txt           # create empty file / update timestamp

# View and navigate files
cat file.txt             # print entire file
less file.txt            # paginated viewer (q to quit, / to search)
head -n 20 file.txt      # first 20 lines
tail -n 20 file.txt      # last 20 lines
tail -f /var/log/app.log # follow log in real time

สิทธิ์การเข้าถึงไฟล์

สิทธิ์ใน Linux แสดงเป็นชุดบิต read/write/execute สามชุดสำหรับ owner, group, และ others:

-rwxr-xr--  1 alice devs  4096 Apr 8 10:00 script.sh

การอ่าน permission string: -rwxr-xr--

  • - — ประเภทไฟล์ (d = directory, l = symlink)
  • rwx — owner: read, write, execute
  • r-x — group: read, execute (ไม่มี write)
  • r-- — others: read อย่างเดียว

chmod — เปลี่ยนสิทธิ์การเข้าถึง

# Symbolic notation
chmod u+x script.sh      # add execute for owner
chmod g-w file.txt       # remove write for group
chmod o=r file.txt       # set others to read-only
chmod a+r file.txt       # add read for all (a = all)

# Numeric (octal) notation
chmod 755 script.sh      # rwxr-xr-x (common for executables)
chmod 644 file.txt       # rw-r--r-- (common for files)
chmod 600 ~/.ssh/id_rsa  # rw------- (required for SSH keys)
chmod 700 ~/.ssh/        # rwx------ (required for .ssh directory)

ใช้ Chmod Calculator ของเราเพื่อสร้าง permission string แบบ visual และทำความเข้าใจว่าแต่ละค่า octal หมายความว่าอะไร

การค้นหาและประมวลผลข้อความ

# Find files
find /var/log -name "*.log"               # find by name
find . -name "*.ts" -newer package.json   # files newer than package.json
find . -type f -size +10M                 # files over 10MB
find . -name "node_modules" -prune        # exclude directories

# Search file contents
grep "error" app.log                      # search in file
grep -r "TODO" ./src                      # recursive search
grep -i "error" app.log                   # case-insensitive
grep -n "error" app.log                   # show line numbers
grep -v "DEBUG" app.log                   # lines NOT matching
grep -E "error|warning" app.log           # regex: error OR warning

# Stream text processing
cat access.log | grep "404" | wc -l       # count 404 errors
cat data.csv | cut -d',' -f1,3            # extract columns 1 and 3
cat file.txt | sort | uniq                # sort and deduplicate
cat file.txt | sort | uniq -c | sort -rn  # count occurrences, sort by frequency

awk — การประมวลผลแบบ field-based

# Print specific fields (tab-separated by default)
awk '{print $1, $3}' file.txt

# Process CSV (comma-separated)
awk -F',' '{print $2}' data.csv

# Sum a column
awk -F',' '{sum += $3} END {print sum}' data.csv

# Print lines matching a pattern
awk '/ERROR/ {print $0}' app.log

sed — stream editor

# Replace text
sed 's/foo/bar/' file.txt          # replace first occurrence per line
sed 's/foo/bar/g' file.txt         # replace all occurrences (global)
sed -i 's/foo/bar/g' file.txt      # in-place edit (modifies the file)

# Delete lines
sed '/^#/d' file.txt               # delete comment lines
sed '5,10d' file.txt               # delete lines 5-10

# Print specific lines
sed -n '10,20p' file.txt           # print lines 10-20

การจัดการ Process

# View running processes
ps aux                             # all processes with details
ps aux | grep nginx                # find nginx processes
top                                # interactive process viewer
htop                               # better interactive viewer (if installed)

# Process control
kill PID                           # send SIGTERM (graceful)
kill -9 PID                        # send SIGKILL (force kill)
pkill nginx                        # kill by name
killall node                       # kill all matching processes

# Background jobs
command &                          # run in background
jobs                               # list background jobs
fg %1                              # bring job 1 to foreground
nohup command &                    # run immune to hangup (survives logout)

# System resources
free -h                            # memory usage
df -h                              # disk space usage
du -sh /var/log/                   # directory size
lsof -i :3000                      # what's using port 3000

คำสั่งเครือข่าย

# Connectivity
ping google.com                    # test connectivity
curl -I https://example.com        # HTTP headers only
curl -o file.zip https://example.com/file.zip  # download file
wget https://example.com/file.zip  # alternative downloader

# Inspect connections
ss -tulpn                          # listening ports and processes
netstat -tulpn                     # alternative (older systems)
lsof -i :80                        # what's on port 80

# DNS
dig example.com                    # DNS lookup
dig example.com MX                 # MX records
host example.com                   # simple DNS lookup
nslookup example.com               # interactive DNS query

SSH และการเข้าถึงระยะไกล

# Connect to a server
ssh user@server.com
ssh -p 2222 user@server.com        # non-standard port
ssh -i ~/.ssh/id_rsa user@server.com  # specific key

# Copy files
scp file.txt user@server.com:~/    # copy to remote home directory
scp user@server.com:~/file.txt .   # copy from remote
scp -r ./dist user@server.com:~/app/  # recursive copy

# SSH tunneling (port forwarding)
ssh -L 5432:localhost:5432 user@server.com  # tunnel remote DB to local port

สร้าง SSH key pairs สำหรับการยืนยันตัวตนที่ปลอดภัยด้วย SSH Key Generator ของเรา

Environment และ Shell

# Environment variables
export API_KEY=abc123              # set for current session and child processes
echo $API_KEY                      # print variable
env                                # list all environment variables
printenv PATH                      # print specific variable
unset API_KEY                      # remove variable

# Shell history
history                            # list previous commands
!!                                 # repeat last command
!grep                              # repeat last grep command
Ctrl+R                             # reverse search history (type to search)

# Useful shortcuts
Ctrl+C                             # interrupt current process
Ctrl+Z                             # suspend to background
Ctrl+L                             # clear screen (same as clear)
Ctrl+A                             # jump to start of line
Ctrl+E                             # jump to end of line
Ctrl+U                             # clear line before cursor

Pipes และการเปลี่ยนทิศทาง Output

# Pipes — chain commands
ls -la | grep ".log" | wc -l

# Redirect output
command > file.txt                 # overwrite file with stdout
command >> file.txt                # append stdout to file
command 2> errors.txt              # redirect stderr
command > all.txt 2>&1             # redirect stdout + stderr to same file
command &> all.txt                 # shorthand for above

# /dev/null — discard output
command > /dev/null 2>&1           # discard all output (run silently)

One-liner ที่มีประโยชน์สำหรับนักพัฒนา

# Find and replace in all files in a directory
find . -name "*.tsx" | xargs sed -i 's/OldComponent/NewComponent/g'

# Get public IP
curl -s ifconfig.me

# Monitor a file for changes
watch -n 2 "ls -la uploads/"

# Compress a directory
tar -czf archive.tar.gz ./directory/

# Extract a tar.gz
tar -xzf archive.tar.gz

# Show the 10 largest files in current directory
du -sh * | sort -rh | head -10

# Count lines of code by file type
find . -name "*.ts" | xargs wc -l | sort -rn | head -20

Command line ให้ผลตอบแทนคุ้มค่ากับการลงทุนเวลาเรียนรู้ ทุกคำสั่งที่คุณเรียนรู้กลายเป็นส่วนประกอบสำหรับการรวมคำสั่งที่ทรงพลังยิ่งขึ้น เริ่มต้นด้วย navigation และ grep แล้วเพิ่ม pipes ตามด้วย awk — แล้วคุณจะรับมือกับงานประมวลผลข้อความทุกอย่างที่พบเจอได้