snippetbashTippending
Shell one-liners for common file operations
Viewed 0 times
one-linerfindsedreplacewcquick-server
Problem
Common file operations require remembering specific command combinations and flags.
Solution
Useful shell one-liners:
# Find large files
find . -type f -size +100M -printf '%s %p\n' | sort -rn | head -20
# macOS: find . -type f -size +100M -exec ls -lh {} \; | sort -k5 -rh
# Find recently modified files
find . -type f -mmin -60 # Modified in last 60 minutes
find . -type f -mtime -1 # Modified in last 24 hours
# Count lines of code (excluding dependencies)
find src -name '.ts' -o -name '.tsx' | xargs wc -l | tail -1
# Or: cloc src/ (if installed)
# Find and replace in files
grep -rl 'old_text' src/ | xargs sed -i '' 's/old_text/new_text/g'
# Linux (no '' after -i): grep -rl 'old' src/ | xargs sed -i 's/old/new/g'
# Watch file changes and run command
while true; do
fswatch -1 src/ && make test
done
# Or use: entr, watchexec, nodemon
# Diff two directories
diff -rq dir1/ dir2/
# Generate random password
openssl rand -base64 32 | tr -d '/+=' | head -c 24
# Port forwarding
ssh -L 5432:localhost:5432 remote-server # Local port forward
ssh -R 8080:localhost:3000 remote-server # Remote port forward
# Quick HTTP server
python3 -m http.server 8000 # Serves current directory
npx serve . # Node alternative
# Find large files
find . -type f -size +100M -printf '%s %p\n' | sort -rn | head -20
# macOS: find . -type f -size +100M -exec ls -lh {} \; | sort -k5 -rh
# Find recently modified files
find . -type f -mmin -60 # Modified in last 60 minutes
find . -type f -mtime -1 # Modified in last 24 hours
# Count lines of code (excluding dependencies)
find src -name '.ts' -o -name '.tsx' | xargs wc -l | tail -1
# Or: cloc src/ (if installed)
# Find and replace in files
grep -rl 'old_text' src/ | xargs sed -i '' 's/old_text/new_text/g'
# Linux (no '' after -i): grep -rl 'old' src/ | xargs sed -i 's/old/new/g'
# Watch file changes and run command
while true; do
fswatch -1 src/ && make test
done
# Or use: entr, watchexec, nodemon
# Diff two directories
diff -rq dir1/ dir2/
# Generate random password
openssl rand -base64 32 | tr -d '/+=' | head -c 24
# Port forwarding
ssh -L 5432:localhost:5432 remote-server # Local port forward
ssh -R 8080:localhost:3000 remote-server # Remote port forward
# Quick HTTP server
python3 -m http.server 8000 # Serves current directory
npx serve . # Node alternative
Why
Shell one-liners save hours compared to GUI tools. They're composable, scriptable, and work on any Unix system.
Revisions (0)
No revisions yet.