belajarkoding Platform belajar web development Indonesia. Artikel, cheat sheets, roadmap, dan code challenges untuk developer Indonesia.
© 2026 BelajarKoding. All rights reserved.
Bagian dari ekosistem Galih Pratama
Bash & Linux untuk Developer Cheat Sheet Referensi cepat Bash dan Linux commands buat developer. File operations, text processing, networking, process management, dan scripting. Perfect buat daily terminal workflow.
Shell 12 min read 2.306 kata
Silakan
login atau
daftar untuk membaca cheat sheet ini.
Baca Cheat Sheet Lengkap Login atau daftar akun gratis untuk membaca cheat sheet ini.
Bash & Linux untuk Developer Cheat Sheet - BelajarKoding | BelajarKoding
# File Operations
# Navigasi
# Print working directory
pwd
# List files
ls # basic
ls -la # detail + hidden files
ls -lh # human-readable sizes
ls
-lt
# sort by modification time
ls -ltr # reverse sort (oldest first)
ls -R # recursive
# Change directory
cd /path/to/dir # absolute
cd ../.. # naik 2 level
cd ~ # ke home directory
cd - # ke directory sebelumnya
cd # ke home directory
# Create, Copy, Move, Delete# Create files
touch file.txt # buat file kosong
echo "hello" > file.txt # tulis ke file
echo "line 2" >> file.txt # append ke file
# Create directories
mkdir
# cat: baca seluruh file
cat file.txt
cat -n file.txt # dengan line numbers
# head: baca N baris pertama
head file.txt # default 10 baris
head -n 50 file.txt # 50 baris
head -c 1000
# Basic search
grep "pattern" file.txt
grep -i "pattern" file.txt # case insensitive
grep -r "pattern" /path/ # recursive
grep -v "pattern" file.txt # inverse (lines WITHOUT pattern)
# Substitute (replace)
sed 's/old/new/' file.txt # replace first occurrence per line
sed 's/old/new/g' file.txt # replace all (global)
sed 's/old/new/gi' file.txt # global + case insensitive
sed -i 's/old/new/g' file.txt # edit file in-place
# Print specific column
awk '{print $2}' file.txt # column 2
awk '{print $1, $3}' file.txt # columns 1 and 3
awk -F ',' '{print $1}' data.csv # CSV: split by comma
# Conditional
awk '$3 > 100' file.txt # lines where column 3 > 100
# By delimiter
cut -d ',' -f2 data.csv # field 2 dari CSV
cut -d: -f1 /etc/passwd # usernames dari passwd
# By character position
cut -c1-10 file.txt # chars 1-10
cut -c5- file.txt # from char 5 to end # Sort
sort file.txt # alphabetical
sort -n numbers.txt # numerical
sort -rn numbers.txt # reverse numerical
sort -t ',' -k2 data.csv # sort by field 2 (comma delimited)
sort -u
# tr: Character Translation# Replace characters
echo "Hello" | tr 'a-z' 'A-Z' # HELLO
echo "hello world" | tr ' ' '-' # hello-world
# Delete characters
echo "h,e,l,l,o" | tr -d ',' # hello
# Squeeze repeats
# tee: Write to stdout AND fileecho "log entry" | tee -a logfile.txt # append to file AND print
command | tee output.txt # save output + see it # By name
find . -name "*.js"
find . -name "*.test.ts"
find . -iname "*.JS" # case insensitive
find /home -name "*.log"
# Numeric
chmod 755 file.sh # rwxr-xr-x (owner: all, group: rx, other: rx)
chmod 644 file.txt # rw-r--r-- (owner: rw, group: r, other: r)
chmod 600 .ssh/id_rsa # rw------- (owner only)
chmod 777 folder/ # rwxrwxrwx (everyone full access)
# Symbolic
chmod
chown user file.txt # change owner
chown user:group file.txt # change owner + group
chown -R user:group folder/ # recursive # Basic GET
curl https://api.example.com
curl -o output.json https://api.example.com/data # save to file
# POST dengan data
curl -X POST https://api.example.com/users \
# Download file
wget https://example.com/file.zip
wget -O custom-name.zip https://example.com/file.zip
# Recursive (mirror website)
wget -r -l 2 https://example.com/
# Continue partial download
wget -c https://example.com/large-file.zip
# Background download
wget -b https://example.com/large-file.zip
# Check connectivity
ping -c 4 google.com
# DNS lookup
dig example.com
dig +short example.com
nslookup example.com
# Trace route
traceroute google.com
# Check port
nc -zv
# ps
ps aux # semua proses
ps aux | grep node # cari proses node
ps -ef | grep python
# top / htop
top # interactive process viewer
htop # better version (install dulu) # By PID
kill 12345 # SIGTERM (graceful)
kill -9 12345 # SIGKILL (force)
kill -15 12345 # SIGTERM (explicit)
# By name
killall node
pkill -f "node server.js"
# Graceful shutdown dengan signal names
kill -SIGTERM
# Run in background
command &
# Run even after logout
nohup node server.js &
# Jobs
jobs # list background jobs
fg # bring last job to foreground
fg %1 # bring job 1 to foreground
bg %2 # resume job 2 in background
# Disk usage
df -h # filesystem usage
du -sh /folder # folder size
du -sh * # size of items in current dir
du -h --max-depth=1 # one level deep
# Memory
# Set untuk session ini
export NODE_ENV = production
export DATABASE_URL = "postgresql://localhost:5432/mydb"
export PATH = $PATH:/custom/path
# Baca variable
echo $HOME
echo $PATH
printenv # semua environment variables
env
# Pipe: output command A jadi input command B
cat file.txt | grep "error"
ps aux | grep node | grep -v grep
# Redirect output
command > file.txt # stdout ke file (overwrite)
# Connect
ssh user@server.com
ssh -p 2222 user@server.com # custom port
ssh -i ~/.ssh/custom_key user@server # custom key
# Run command remote
ssh user@server
# Generate key pair
ssh-keygen -t ed25519 -C "hi@galihpratama.com"
ssh-keygen -t rsa -b 4096 -C "hi@galihpratama.com"
# Copy public key ke server
ssh-copy-id user@server
# atau manual
cat ~/.ssh/id_ed25519.pub | ssh user@server
# Edit crontab
crontab -e # edit
crontab -l # list current jobs
crontab -r # remove all jobs
# Format: minute hour day-of-month month day-of-week command
# Jadwal contoh:
# ┌──────── minute (0-59)
# │ ┌────── hour (0-23)
# Count files by extension
find . -type f | sed 's/.*\.//' | sort | uniq -c | sort -rn
# Find largest files
du -a
Shell : Program yang ngejalanin command (bash, zsh, fish). Bash adalah default di kebanyakan Linux.
Pipe (|) : Operator yang nerusin stdout command A jadi stdin command B.
Redirect : Operator (>, >>, <, 2>) buat ngarahin stdin/stdout/stderr ke file atau dari file.
Globbing : Pattern matching buat nama file (.js, ??.txt, [a-z] ).
Process substitution : <(command) nyediain output command sebagai temporary file.
Exit code : Status akhir command. 0 = sukses, non-zero = error. Akses via $?.
Signal : Pesan ke proses. SIGTERM (15) = graceful stop, SIGKILL (9) = force kill.
Environment variable : Variable yang tersedia di semua proses yang di-spawn dari shell.
Crontab : Scheduler yang jalan command di interval tertentu.
new-dir
mkdir -p path/to/nested/dir # buat parent dirs sekaligus
mkdir dir1 dir2 dir3 # multiple dirs
# Copy
cp file.txt backup.txt
cp -r folder/ backup/ # recursive (folder)
cp -i file.txt dest/ # interactive (prompt sebelum overwrite)
cp -v file.txt dest/ # verbose
# Move/rename
mv old-name.txt new-name.txt
mv file.txt /other/path/
mv -i file.txt dest/ # prompt sebelum overwrite
# Delete
rm file.txt
rm -r folder/ # recursive
rm -rf folder/ # force, no prompt (HATI-HATI)
rm -i file.txt # confirm sebelum hapus
file.txt
# 1000 bytes pertama
# tail: baca N baris terakhir
tail file.txt # default 10 baris
tail -n 50 file.txt # 50 baris terakhir
tail -f app.log # follow (real-time)
tail -f -n 100 app.log # mulai dari 100 baris terakhir, follow
# less: paginated viewer (press q untuk keluar)
less file.txt
grep -n "pattern" file.txt # dengan line numbers
grep -c "pattern" file.txt # count matches
grep -w "word" file.txt # whole word match
grep -E "regex" file.txt # extended regex (alias egrep)
grep -o "pattern" file.txt # only matching part
grep -A 3 "error" log.txt # 3 lines After match
grep -B 3 "error" log.txt # 3 lines Before match
grep -C 3 "error" log.txt # 3 lines Context (before + after)
grep --color=auto "pattern" file # highlight match
sed -i.bak 's/old/new/g' file.txt # in-place + backup
# Delete lines
sed '/pattern/d' file.txt # delete lines matching pattern
sed '5d' file.txt # delete line 5
sed '1,5d' file.txt # delete lines 1-5
# Print specific lines
sed -n '5,10p' file.txt # print lines 5-10
sed -n '/start/,/end/p' file.txt # print between patterns
awk 'NR > 1' file.txt # skip header row
awk '/error/ {print}' log.txt # lines containing "error"
# Calculations
awk '{sum += $1} END {print sum}' nums.txt
awk '{sum += $3; count++} END {print sum/count}' data.txt
# Multiple conditions
awk '$2 == "active" && $3 > 1000' users.txt
file.txt
# unique (sort + dedupe)
# uniq (harus sort dulu)
sort file.txt | uniq # remove duplicates
sort file.txt | uniq -c # count occurrences
sort file.txt | uniq -d # show only duplicates
sort file.txt | uniq -u # show only unique lines
echo
"hello world"
|
tr
-s
' '
# hello world
# By type
find . -type f -name "*.md" # files only
find . -type d -name "node_modules" # directories only
# By size
find . -size +100M # larger than 100MB
find . -size -1k # smaller than 1KB
# By modification time
find . -mtime -1 # modified in last 24 hours
find . -mtime +7 # older than 7 days
find . -mmin -30 # modified in last 30 minutes
# By permission
find . -perm 777
find . -type f -perm 600
# Execute command on each result
find . -name "*.log" -exec rm {} \;
find . -name "*.js" -exec wc -l {} \;
find . -name "*.js" -exec grep "TODO" {} +
+x
script.sh
# add execute
chmod -w file.txt # remove write
chmod u+x script.sh # user: add execute
chmod g+w file.txt # group: add write
chmod o-r file.txt # other: remove read
chmod a+r file.txt # all: add read
chmod -R 755 folder/ # recursive
-H "Content-Type: application/json" \
-d '{"name": "Galih", "email": "hi@galihpratama.com"}'
# Headers
curl -H "Authorization: Bearer token123" https://api.example.com/protected
# Verbose (lihat request + response headers)
curl -v https://example.com
# Follow redirects
curl -L https://short.url/abc
# Download dengan progress bar
curl -O https://example.com/file.zip
curl -L -O https://example.com/large-file.iso
# Authentication
curl -u username:password https://api.example.com/
curl --cert client.pem https://secure.example.com/
# Set timeout
curl --max-time 30 https://slow-api.example.com/
# Multiple URLs
curl -O https://example.com/file1.txt -O https://example.com/file2.txt
# Download resume
curl -C - -O https://example.com/large-file.zip
# Check headers only
curl -I https://example.com
# Download from file list
wget -i urls.txt
example.com
443
# check if port 443 is open
nc -zv -w5 example.com 8080 # with 5s timeout
# Show open ports
ss -tlnp # TCP listening ports
netstat -tlnp # legacy alternative
# Show network interfaces
ip addr
ifconfig # legacy
# Show routing table
ip route
12345
kill -SIGINT 12345 # Ctrl+C
kill -SIGHUP 12345 # reload config
Ctrl+Z # suspend current process
Ctrl+C # kill current process
free
-h
# RAM usage
vmstat 1 # virtual memory stats every 1s
# CPU
uptime # load average
nproc # number of CPU cores
lscpu # CPU info
# System info
uname -a # kernel info
hostname # machine name
date # current date/time
whoami # current user
# alternative
# Load dari file
source .env # atau
. .env
# Set untuk satu command saja
NODE_ENV = test node app.js
# Unset
unset MY_VAR
command >> file.txt # stdout ke file (append)
command 2> error.log # stderr ke file
command > out.txt 2>&1 # stdout + stderr ke file yang sama
command & > combined.log # stdout + stderr (bash shortcut)
# Redirect input
command < input.txt
mysql < schema.sql
# /dev/null (black hole)
command > /dev/null 2>&1 # buang semua output
command 2> /dev/null # buang stderr saja
# Process substitution
diff <( ls dir1) <( ls dir2) # compare output dua commands
"ls -la /var/log"
ssh user@server "df -h"
# Port forwarding (local tunnel)
ssh -L 8080:localhost:80 user@server # localhost:8080 -> server:80
ssh -L 5432:db.internal:5432 user@bastion
# Reverse port forwarding
ssh -R 8080:localhost:80 user@server
# Dynamic port forwarding (SOCKS proxy)
ssh -D 1080 user@server
# Copy files
scp file.txt user@server:/path/to/dest/
scp user@server:/path/to/file.txt ./
scp -r folder/ user@server:/path/
# rsync (lebih efisien buat transfer besar)
rsync -avz --progress folder/ user@server:/dest/
rsync -avz --delete src/ user@server:dest/ # mirror (delete extra files)
rsync -avz -e "ssh -p 2222" src/ user@server:dest/
"mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"
# SSH config (~/.ssh/config)
Host myserver
HostName server.example.com
User galih
Port 2222
IdentityFile ~/.ssh/my_key
# Setelah itu tinggal
ssh myserver
# │ │ ┌──── day of month (1-31)
# │ │ │ ┌── month (1-12)
# │ │ │ │ ┌ day of week (0-7, 0 dan 7 = Sunday)
# │ │ │ │ │
# * * * * * command
* /5 * * * * /script/backup.sh # setiap 5 menit
0 2 * * * /script/cleanup.sh # jam 2 pagi setiap hari
0 0 * * 0 /script/weekly-report.sh # midnight setiap minggu
0 9 * * 1-5 /script/workday-task.sh # jam 9 pagi, Senin-Jumat
0 0 1 * * /script/monthly-report.sh # tanggal 1 setiap bulan
/path
|
sort
-rn
|
head
-20
# Kill process on specific port
kill -9 $( lsof -t -i:3000 )
# Extract column from CSV dan count unique
cut -d ',' -f3 data.csv | sort | uniq -c | sort -rn
# Replace text in multiple files
find . -name "*.js" -exec sed -i 's/oldName/newName/g' {} +
# Download dan extract tarball
curl -sL https://example.com/file.tar.gz | tar xz
# Create password
openssl rand -base64 32
# JSON pretty print
echo '{"a":1,"b":2}' | python3 -m json.tool
# Monitor file changes
watch -n 1 ls -la /path
# Quick HTTP server
python3 -m http.server 8000
npx serve -p 3000
# Send notification when command selesai
long-command ; notify-send "Done!"