Linux Command Line Essential untuk Developer
Kemampuan menggunakan command line adalah skill fundamental bagi developer. Artikel ini membahas perintah Linux essential yang akan meningkatkan produktivitasmu.
Navigasi File System
Perintah Dasar
# Print working directory
pwd
# List files
ls # Basic list
ls -la # Detailed + hidden files
ls -lh # Human readable sizes
ls -lt # Sort by time
# Change directory
cd /path/to/dir
cd .. # Parent directory
cd ~ # Home directory
cd - # Previous directory
# Create directory
mkdir mydir
mkdir -p parent/child/grandchild # Create nested
Manipulasi File
# Create file
touch file.txt
# Copy
cp source.txt dest.txt
cp -r sourcedir/ destdir/ # Recursive
# Move/Rename
mv old.txt new.txt
mv file.txt /path/to/dir/
# Remove
rm file.txt
rm -r directory/ # Recursive
rm -rf directory/ # Force (hati-hati!)
# View file content
cat file.txt # Semua content
head -n 20 file.txt # 20 baris pertama
tail -n 20 file.txt # 20 baris terakhir
tail -f log.txt # Follow (real-time)
less file.txt # Scrollable viewer
Text Processing
grep - Search Text
# Basic search
grep "error" log.txt
# Case insensitive
grep -i "error" log.txt
# Recursive search
grep -r "TODO" ./src/
# Show line numbers
grep -n "function" script.js
# Invert match (exclude)
grep -v "debug" log.txt
# Count matches
grep -c "error" log.txt
# Regex
grep -E "error|warning" log.txt
sed - Stream Editor
# Replace text
sed 's/old/new/' file.txt # First occurrence
sed 's/old/new/g' file.txt # All occurrences
sed -i 's/old/new/g' file.txt # In-place edit
# Delete lines
sed '/pattern/d' file.txt # Lines matching pattern
sed '1,5d' file.txt # Lines 1-5
# Print specific lines
sed -n '10,20p' file.txt # Lines 10-20
awk - Text Processing
# Print columns
awk '{print $1}' file.txt # First column
awk '{print $1, $3}' file.txt # First and third
# Custom delimiter
awk -F',' '{print $2}' data.csv
# Conditional
awk '$3 > 100 {print $1}' data.txt
# Sum column
awk '{sum += $2} END {print sum}' data.txt
Other Text Tools
# Sort
sort file.txt
sort -n numbers.txt # Numeric sort
sort -r file.txt # Reverse
sort -u file.txt # Unique only
# Unique
uniq file.txt # Remove duplicates
uniq -c file.txt # Count occurrences
# Word count
wc file.txt # Lines, words, chars
wc -l file.txt # Lines only
# Cut
cut -d',' -f1,3 data.csv # Fields 1 and 3
File Permissions
Understanding Permissions
-rwxr-xr-x
│└┬┘└┬┘└┬┘
│ │ │ └── Others: r-x (read, execute)
│ │ └───── Group: r-x (read, execute)
│ └──────── Owner: rwx (read, write, execute)
└────────── File type (- = file, d = directory)
chmod - Change Permissions
# Symbolic mode
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
# Numeric mode
chmod 755 script.sh # rwxr-xr-x
chmod 644 file.txt # rw-r--r--
chmod 600 secret.key # rw-------
chown - Change Ownership
chown user file.txt
chown user:group file.txt
chown -R user:group directory/
Process Management
Viewing Processes
# List processes
ps aux # All processes
ps aux | grep node # Filter
# Real-time monitoring
top
htop # Better interface (install dulu)
# Process tree
pstree
Managing Processes
# Kill process
kill PID # Graceful
kill -9 PID # Force kill
killall processname
# Background/Foreground
command & # Run in background
jobs # List background jobs
fg %1 # Bring to foreground
bg %1 # Continue in background
Ctrl+Z # Suspend current process
# nohup - survive logout
nohup command &
Networking
Basic Commands
# Check connectivity
ping google.com
ping -c 4 google.com # 4 packets only
# DNS lookup
nslookup domain.com
dig domain.com
# Network interfaces
ip addr
ifconfig # Older systems
# Routing
ip route
netstat -rn
# Open ports
netstat -tulpn
ss -tulpn # Modern alternative
curl - HTTP Requests
# GET request
curl https://api.example.com/users
# POST request
curl -X POST -d '{"name":"John"}' \
-H "Content-Type: application/json" \
https://api.example.com/users
# Download file
curl -O https://example.com/file.zip
curl -o myfile.zip https://example.com/file.zip
# Follow redirects
curl -L https://example.com
# Show headers
curl -I https://example.com
curl -v https://example.com # Verbose
wget - Download Files
wget https://example.com/file.zip
wget -O custom-name.zip https://example.com/file.zip
wget -c https://example.com/large-file.zip # Resume
Disk Usage
# Disk space
df -h # Human readable
df -h /home # Specific mount
# Directory size
du -sh directory/ # Summary
du -h --max-depth=1 # One level deep
# Find large files
find / -size +100M -type f 2>/dev/null
Compression
# tar
tar -cvf archive.tar files/ # Create
tar -xvf archive.tar # Extract
tar -tvf archive.tar # List contents
# tar.gz
tar -czvf archive.tar.gz files/
tar -xzvf archive.tar.gz
# zip
zip -r archive.zip directory/
unzip archive.zip
unzip -l archive.zip # List contents
Environment Variables
# View
echo $PATH
echo $HOME
env # All variables
printenv VAR_NAME
# Set (current session)
export MY_VAR="value"
# Permanent (add to ~/.bashrc or ~/.zshrc)
echo 'export MY_VAR="value"' >> ~/.bashrc
source ~/.bashrc
Pipes dan Redirection
# Pipe output ke command lain
cat file.txt | grep "error" | wc -l
# Redirect output
command > file.txt # Overwrite
command >> file.txt # Append
command 2> error.txt # Stderr only
command &> all.txt # Both stdout and stderr
# Input redirection
command < input.txt
# Here document
cat << EOF > file.txt
Line 1
Line 2
EOF
Useful Shortcuts
Keyboard Shortcuts
| Shortcut | Action |
|---|---|
Ctrl+C | Cancel current command |
Ctrl+D | Exit shell / EOF |
Ctrl+L | Clear screen |
Ctrl+A | Go to line start |
Ctrl+E | Go to line end |
Ctrl+R | Search command history |
Tab | Autocomplete |
!! | Repeat last command |
!$ | Last argument of previous command |
Command History
history # Show history
history | grep git # Search history
!123 # Run command #123
!! # Run last command
sudo !! # Run last command with sudo
Shell Scripting Basics
#!/bin/bash
# Variables
NAME="World"
echo "Hello, $NAME!"
# Conditionals
if [ -f "file.txt" ]; then
echo "File exists"
elif [ -d "directory" ]; then
echo "Directory exists"
else
echo "Not found"
fi
# Loops
for i in 1 2 3 4 5; do
echo "Number: $i"
done
for file in *.txt; do
echo "Processing $file"
done
# Functions
greet() {
echo "Hello, $1!"
}
greet "Developer"
Tips Produktivitas
1. Aliases
# Add to ~/.bashrc
alias ll='ls -la'
alias gs='git status'
alias dc='docker-compose'
alias k='kubectl'
2. Useful One-liners
# Find and replace in multiple files
find . -name "*.js" -exec sed -i 's/old/new/g' {} +
# Kill process by port
kill $(lsof -t -i:3000)
# Watch file changes
watch -n 1 'ls -la'
# Quick HTTP server
python3 -m http.server 8000
3. tmux - Terminal Multiplexer
tmux # Start session
tmux new -s name # Named session
Ctrl+B, % # Split vertical
Ctrl+B, " # Split horizontal
Ctrl+B, arrow # Navigate panes
Ctrl+B, d # Detach
tmux attach -t name # Reattach
Kesimpulan
Menguasai command line akan sangat meningkatkan produktivitasmu sebagai developer. Mulai dengan perintah dasar, lalu pelajari yang lebih advanced sesuai kebutuhan.
Tips: Buat cheat sheet pribadi dengan perintah yang sering kamu gunakan!
Referensi:
Komentar
Memuat komentar...
Tulis Komentar