Linux Bash
Linux Bash
Expert guidance for Linux bash operations, scripting, and system administration.
Core Commands
File Operations
ls -lah- List files with details, including hiddenfind /path -name "pattern"- Find files by namegrep -r "pattern" /path- Recursive text searchchmod +x file- Make file executablechown user:group file- Change ownershiptar -czf archive.tar.gz dir/- Create compressed archivetar -xzf archive.tar.gz- Extract archive
Process Management
ps aux | grep process- Find running processestop/htop- Monitor system resourceskill -9 PID- Force kill processnohup command &- Run command in backgroundjobs- List background jobsfg %1- Bring job to foreground
System Information
df -h- Disk usage human-readabledu -sh dir/- Directory sizefree -h- Memory usageuname -a- System informationlsb_release -a- Distribution info
Text Processing
cat file | head -n 10- First 10 linestail -f log.txt- Follow log filesed 's/old/new/g' file- Replace textawk '{print $1}' file- Print first columnsort file | uniq- Remove duplicates
Shell Scripting Best Practices
Script Template
#!/bin/bash
set -euo pipefail # Exit on error, undefined vars, pipe failures
# Script description
readonly SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
main() {
# Your logic here
echo "Hello, World!"
}
main "$@"
Error Handling
# Check command success
if ! command -v tool &> /dev/null; then
echo "Error: tool not found" >&2
exit 1
fi
# Trap errors
trap 'echo "Error on line $LINENO"' ERR
Variables
# Constants (uppercase)
readonly CONFIG_FILE="/etc/app.conf"
# Variables (lowercase)
user_input="$1"
# Arrays
files=("file1.txt" "file2.txt")
for file in "${files[@]}"; do
echo "$file"
done
Common Patterns
Check if file exists
if [[ -f "file.txt" ]]; then
echo "File exists"
fi
Loop through files
for file in *.txt; do
echo "Processing $file"
done
Read user input
read -p "Enter value: " user_value
Function definition
my_function() {
local arg1="$1"
echo "Received: $arg1"
}
Tips
- Quote variables: Always use
"$variable"to prevent word splitting - Use [[ ]] for tests: More features than [ ]
- Shellcheck: Use
shellcheck script.shto validate scripts - Exit codes: Use
$?to check last command status - Debugging: Use
set -xto trace execution
Safety First
- Always test scripts in safe environment first
- Use
set -eto exit on errors - Validate input parameters
- Quote file paths to handle spaces
- Use absolute paths when possible
More from oriolrius/pki-manager-web
keycloak
Expert guidance for Keycloak identity and access management including realm configuration, client setup, user federation, authentication flows, role-based access control, and integration with applications. Use this when setting up authentication, configuring SSO, managing users and roles, or integrating Keycloak with applications.
60trpc
Expert guidance for tRPC (TypeScript Remote Procedure Call) including router setup, procedures, middleware, context, client configuration, and Next.js integration. Use this when building type-safe APIs, integrating tRPC with Next.js, or implementing client-server communication with full TypeScript inference.
37next.js
Expert guidance for Next.js framework including App Router, Server Components, routing, data fetching, API routes, middleware, and deployment. Use this when building Next.js applications, working with React Server Components, or implementing Next.js features.
25sqlite
Expert guidance for SQLite database with better-sqlite3 Node.js driver including database setup, queries, transactions, migrations, performance optimization, and integration with TypeScript. Use this when working with embedded databases, better-sqlite3 driver, or SQLite operations.
24backlog.md
Expert guidance for Backlog.md CLI project management tool including task creation, editing, status management, acceptance criteria, search, and board visualization. Use this when managing project tasks, creating task lists, updating task status, or organizing project work.
20oauth2
Expert guidance for OAuth 2.0 protocol including authorization flows, grant types, token management, OpenID Connect, security best practices, and implementation patterns. Use this when implementing authentication/authorization, working with OAuth providers, securing APIs, or integrating with third-party services.
12