Set up custom shell aliases and functions to streamline your development workflow, reduce typing, and boost terminal productivity with reusable commands and automation shortcuts.
Prerequisites
- Command line access
- Text editor (nano, vim, or code editor)
- Basic understanding of terminal navigation
What this solves
Shell aliases and functions transform repetitive terminal commands into short, memorable shortcuts that save time and reduce errors. Instead of typing long commands repeatedly, you create custom shortcuts that execute complex operations with just a few keystrokes, making your development workflow faster and more efficient.
Step-by-step configuration
Identify your shell type
Before creating aliases, determine which shell you're using to know which configuration file to modify.
echo $SHELL
ps -p $$
Back up existing shell configuration
Create backups of your current shell configuration files to prevent data loss.
cp ~/.bashrc ~/.bashrc.backup
cp ~/.zshrc ~/.zshrc.backup 2>/dev/null || echo "No zshrc found"
cp ~/.profile ~/.profile.backup 2>/dev/null || echo "No profile found"
Create dedicated aliases file
Organize your aliases in a separate file for better management and portability across different systems.
mkdir -p ~/.config/shell
touch ~/.config/shell/aliases
touch ~/.config/shell/functions
Configure essential system aliases
Add common system administration and navigation shortcuts that work across all distributions.
# System management aliases
alias ll='ls -alF'
alias la='ls -A'
alias l='ls -CF'
alias ..='cd ..'
alias ...='cd ../..'
alias ....='cd ../../..'
System information
alias df='df -h'
alias du='du -h'
alias free='free -h'
alias ps='ps aux'
alias top='htop'
Safety aliases
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
Network shortcuts
alias ports='netstat -tulanp'
alias myip='curl -s https://ipinfo.io/ip'
alias ping='ping -c 5'
Process management
alias psg='ps aux | grep'
alias killall='killall -v'
System logs
alias logs='sudo journalctl -f'
alias syslog='sudo tail -f /var/log/syslog'
Add development-specific aliases
Create shortcuts for common development tasks, version control, and project management.
# Git shortcuts
alias gs='git status'
alias ga='git add'
alias gc='git commit -m'
alias gp='git push'
alias gl='git pull'
alias gb='git branch'
alias gco='git checkout'
alias gd='git diff'
alias glog='git log --oneline --graph --decorate'
Docker shortcuts
alias dps='docker ps'
alias dpa='docker ps -a'
alias di='docker images'
alias dprune='docker system prune -f'
alias dstop='docker stop $(docker ps -q)'
Package management shortcuts
alias install='sudo apt install'
alias search='apt search'
alias update='sudo apt update && sudo apt upgrade'
alias autoremove='sudo apt autoremove'
Text editor shortcuts
alias v='vim'
alias n='nano'
alias code='code .'
Directory shortcuts
alias projects='cd ~/projects'
alias downloads='cd ~/Downloads'
alias docs='cd ~/Documents'
Python development
alias py='python3'
alias pip='pip3'
alias venv='python3 -m venv'
alias activate='source venv/bin/activate'
Node.js development
alias ni='npm install'
alias ns='npm start'
alias nt='npm test'
alias nb='npm run build'
Create useful shell functions
Functions provide more flexibility than aliases by accepting parameters and executing complex logic.
# Create and enter directory
mkcd() {
mkdir -p "$1" && cd "$1"
}
Extract various archive formats
extract() {
if [ -f "$1" ]; then
case "$1" in
*.tar.bz2) tar xjf "$1" ;;
*.tar.gz) tar xzf "$1" ;;
*.bz2) bunzip2 "$1" ;;
*.rar) unrar x "$1" ;;
*.gz) gunzip "$1" ;;
*.tar) tar xf "$1" ;;
*.tbz2) tar xjf "$1" ;;
*.tgz) tar xzf "$1" ;;
*.zip) unzip "$1" ;;
*.Z) uncompress "$1" ;;
*.7z) 7z x "$1" ;;
*) echo "'$1' cannot be extracted" ;;
esac
else
echo "'$1' is not a valid file"
fi
}
Find process by name
findproc() {
ps aux | grep -i "$1" | grep -v grep
}
Create backup of file
backup() {
cp "$1" "$1.backup.$(date +%Y%m%d_%H%M%S)"
echo "Backup created: $1.backup.$(date +%Y%m%d_%H%M%S)"
}
Quick web server
serve() {
local port=${1:-8000}
python3 -m http.server "$port"
}
Git clone and enter directory
gclone() {
git clone "$1" && cd "$(basename "$1" .git)"
}
Show directory size
dirsize() {
du -sh "${1:-.}" | sort -hr
}
Search for text in files
search() {
grep -r --include="*.$2" "$1" .
}
System resource monitoring
sysinfo() {
echo "=== System Information ==="
echo "Hostname: $(hostname)"
echo "Uptime: $(uptime -p)"
echo "Load: $(uptime | awk '{print $10,$11,$12}')"
echo "Memory: $(free -h | awk '/^Mem:/ {print $3"/"$2}')"
echo "Disk: $(df -h / | awk 'NR==2 {print $3"/"$2" ("$5" used)"}')"
}
Configure shell to load custom files
Modify your shell configuration to automatically load the aliases and functions files on startup.
# Add to the end of ~/.bashrc
if [ -f ~/.config/shell/aliases ]; then
source ~/.config/shell/aliases
fi
if [ -f ~/.config/shell/functions ]; then
source ~/.config/shell/functions
fi
Enable programmable completion
if ! shopt -oq posix; then
if [ -f /usr/share/bash-completion/bash_completion ]; then
. /usr/share/bash-completion/bash_completion
elif [ -f /etc/bash_completion ]; then
. /etc/bash_completion
fi
fi
Add zsh-specific configuration
If using zsh, configure it to load your custom aliases and functions with enhanced features.
# Add to ~/.zshrc if using zsh
if [ -f ~/.config/shell/aliases ]; then
source ~/.config/shell/aliases
fi
if [ -f ~/.config/shell/functions ]; then
source ~/.config/shell/functions
fi
Enable completion system
autoload -Uz compinit
compinit
History configuration
HISTSIZE=10000
SAVEHIST=10000
HISTFILE=~/.zsh_history
setopt SHARE_HISTORY
setopt HIST_IGNORE_DUPS
Create environment-specific aliases
Add aliases that adapt to your specific development environment and commonly used tools.
# Add environment-specific aliases
Database shortcuts
alias pgstart='sudo systemctl start postgresql'
alias pgstop='sudo systemctl stop postgresql'
alias pgstatus='sudo systemctl status postgresql'
alias mysqlstart='sudo systemctl start mysql'
alias mysqlstop='sudo systemctl stop mysql'
Service management
alias nginx-reload='sudo systemctl reload nginx'
alias nginx-test='sudo nginx -t'
alias apache-reload='sudo systemctl reload apache2'
alias redis-cli='redis-cli -p 6379'
Log monitoring
alias nginx-error='sudo tail -f /var/log/nginx/error.log'
alias nginx-access='sudo tail -f /var/log/nginx/access.log'
alias apache-error='sudo tail -f /var/log/apache2/error.log'
File permissions shortcuts
alias fix-www='sudo chown -R www-data:www-data'
alias fix-user='sudo chown -R $USER:$USER'
Security shortcuts
alias ports-open='ss -tuln'
alias firewall-status='sudo ufw status'
alias fail2ban-status='sudo fail2ban-client status'
Backup shortcuts
alias backup-home='tar -czf ~/backup-$(date +%Y%m%d).tar.gz ~'
alias backup-etc='sudo tar -czf /tmp/etc-backup-$(date +%Y%m%d).tar.gz /etc'
Set up command completion
Enable tab completion for your custom aliases to make them even more efficient to use.
sudo apt install -y bash-completion
Apply changes to current session
Reload your shell configuration to make the new aliases and functions available immediately.
source ~/.bashrc
or for zsh users
source ~/.zshrc
Verify your setup
Test your aliases and functions to ensure they're working correctly in your terminal session.
# Test basic aliases
ll
gs
ps
Test functions
mkcd test-directory
sysinfo
extract --help
List all available aliases
alias
Show function definitions
declare -f mkcd
source ~/.bashrc to reload the configuration.Advanced customization
Create project-specific aliases
Set up aliases that automatically activate when entering specific project directories.
# Auto-activate project environment
cd_project() {
cd "$1"
if [ -f "venv/bin/activate" ]; then
source venv/bin/activate
echo "Virtual environment activated"
fi
if [ -f ".env" ]; then
set -a; source .env; set +a
echo "Environment variables loaded"
fi
}
Quick project setup
project_init() {
mkcd "$1"
git init
echo "# $1" > README.md
echo "node_modules/" > .gitignore
echo "__pycache__/" >> .gitignore
echo ".env" >> .gitignore
echo "Project $1 initialized"
}
Add conditional aliases
Create aliases that adapt based on available tools and system configuration.
# Conditional aliases based on available tools
if command -v bat > /dev/null 2>&1; then
alias cat='bat'
fi
if command -v exa > /dev/null 2>&1; then
alias ls='exa'
alias ll='exa -la'
fi
if command -v fd > /dev/null 2>&1; then
alias find='fd'
fi
if command -v rg > /dev/null 2>&1; then
alias grep='rg'
fi
Docker Compose shortcuts (adapt to version)
if command -v docker-compose > /dev/null 2>&1; then
alias dc='docker-compose'
alias dcup='docker-compose up -d'
alias dcdown='docker-compose down'
elif command -v docker > /dev/null 2>&1 && docker compose version > /dev/null 2>&1; then
alias dc='docker compose'
alias dcup='docker compose up -d'
alias dcdown='docker compose down'
fi
Best practices for shell aliases
| Practice | Description | Example |
|---|---|---|
| Use memorable names | Choose intuitive abbreviations | gs for git status |
| Avoid conflicts | Don't override system commands | Use ll not ls |
| Include safety flags | Add confirmation for destructive operations | alias rm='rm -i' |
| Group logically | Organize aliases by purpose | Git, Docker, System sections |
| Document complex ones | Add comments for complex aliases | # Git log with graph |
sudo, su, or passwd as this can create security risks and unexpected behavior.Common issues
| Symptom | Cause | Fix |
|---|---|---|
| Aliases not working after login | Shell config not loaded | Add source command to ~/.profile or ~/.bash_profile |
| Function not found error | Function file not sourced | Check source command in shell config file |
| Alias works in terminal but not in scripts | Aliases not available in non-interactive shells | Use full commands in scripts or source aliases explicitly |
| Changes not persisting | Wrong config file modified | Check $SHELL and modify correct config file |
| Completion not working | Bash completion not installed | Install bash-completion package |
| Function syntax error | Incorrect shell syntax | Use bash -n filename to check syntax |
Next steps
- Configure Linux user and group management with sudo access control
- Configure Linux environment variables and PATH management for development workflows
- Set up Zsh with Oh My Zsh and Powerline themes for enhanced terminal experience
- Configure tmux session management for development productivity
- Set up Vim development environment with plugins and customization
Automated install script
Run this to automate the entire setup
#!/usr/bin/env bash
set -euo pipefail
# Shell Aliases and Functions Installer
# Configures productivity aliases and functions for development workflows
# Colors for output
readonly RED='\033[0;31m'
readonly GREEN='\033[0;32m'
readonly YELLOW='\033[1;33m'
readonly BLUE='\033[0;34m'
readonly NC='\033[0m' # No Color
# Global variables
SHELL_TYPE=""
CONFIG_FILE=""
BACKUP_DIR="$HOME/.config/shell/backups"
ALIASES_DIR="$HOME/.config/shell"
# Print colored output
print_status() {
echo -e "${GREEN}[INFO]${NC} $1"
}
print_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
print_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
# Cleanup function for error handling
cleanup() {
if [[ $? -ne 0 ]]; then
print_error "Installation failed. Check backups in $BACKUP_DIR"
fi
}
trap cleanup ERR
# Detect distribution and package manager
detect_distro() {
if [[ -f /etc/os-release ]]; then
. /etc/os-release
case "$ID" in
ubuntu|debian)
PKG_MGR="apt"
PKG_INSTALL="apt install -y"
;;
almalinux|rocky|centos|rhel|ol|fedora)
PKG_MGR="dnf"
PKG_INSTALL="dnf install -y"
;;
amzn)
PKG_MGR="yum"
PKG_INSTALL="yum install -y"
;;
*)
print_error "Unsupported distribution: $ID"
exit 1
;;
esac
else
print_error "Cannot detect distribution"
exit 1
fi
}
# Detect shell type and configuration file
detect_shell() {
local current_shell
current_shell=$(basename "$SHELL")
case "$current_shell" in
bash)
SHELL_TYPE="bash"
CONFIG_FILE="$HOME/.bashrc"
;;
zsh)
SHELL_TYPE="zsh"
CONFIG_FILE="$HOME/.zshrc"
;;
*)
print_warning "Unsupported shell: $current_shell. Using bash defaults."
SHELL_TYPE="bash"
CONFIG_FILE="$HOME/.bashrc"
;;
esac
}
# Create backup directory and backup files
create_backups() {
print_status "[1/8] Creating backups..."
mkdir -p "$BACKUP_DIR"
chmod 755 "$BACKUP_DIR"
local timestamp
timestamp=$(date +%Y%m%d_%H%M%S)
# Backup existing configuration files
for file in ~/.bashrc ~/.zshrc ~/.profile; do
if [[ -f "$file" ]]; then
cp "$file" "$BACKUP_DIR/$(basename "$file").backup.$timestamp"
print_status "Backed up $file"
fi
done
}
# Create directory structure
create_directories() {
print_status "[2/8] Creating directory structure..."
mkdir -p "$ALIASES_DIR"
chmod 755 "$ALIASES_DIR"
touch "$ALIASES_DIR/aliases"
touch "$ALIASES_DIR/functions"
chmod 644 "$ALIASES_DIR/aliases"
chmod 644 "$ALIASES_DIR/functions"
}
# Install required packages
install_packages() {
print_status "[3/8] Installing required packages..."
local packages="curl wget htop"
# Add distribution-specific packages
case "$PKG_MGR" in
apt)
sudo apt update
packages="$packages net-tools"
;;
dnf|yum)
packages="$packages net-tools"
;;
esac
sudo $PKG_INSTALL $packages
}
# Create system aliases
create_system_aliases() {
print_status "[4/8] Creating system aliases..."
cat > "$ALIASES_DIR/aliases" << 'EOF'
# System management aliases
alias ll='ls -alF'
alias la='ls -A'
alias l='ls -CF'
alias ..='cd ..'
alias ...='cd ../..'
alias ....='cd ../../..'
# System information
alias df='df -h'
alias du='du -h'
alias free='free -h'
alias ps='ps aux'
alias top='htop'
# Safety aliases
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
# Network shortcuts
alias ports='netstat -tulanp'
alias myip='curl -s https://ipinfo.io/ip'
alias ping='ping -c 5'
# Process management
alias psg='ps aux | grep'
alias killall='killall -v'
# System logs (distribution-aware)
alias logs='sudo journalctl -f'
EOF
# Add distribution-specific aliases
case "$PKG_MGR" in
apt)
cat >> "$ALIASES_DIR/aliases" << 'EOF'
# Package management shortcuts (Debian/Ubuntu)
alias install='sudo apt install'
alias search='apt search'
alias update='sudo apt update && sudo apt upgrade'
alias autoremove='sudo apt autoremove'
alias syslog='sudo tail -f /var/log/syslog'
EOF
;;
dnf)
cat >> "$ALIASES_DIR/aliases" << 'EOF'
# Package management shortcuts (RHEL/Fedora)
alias install='sudo dnf install'
alias search='dnf search'
alias update='sudo dnf update'
alias autoremove='sudo dnf autoremove'
alias syslog='sudo tail -f /var/log/messages'
EOF
;;
yum)
cat >> "$ALIASES_DIR/aliases" << 'EOF'
# Package management shortcuts (Amazon Linux)
alias install='sudo yum install'
alias search='yum search'
alias update='sudo yum update'
alias syslog='sudo tail -f /var/log/messages'
EOF
;;
esac
}
# Create development aliases
create_dev_aliases() {
print_status "[5/8] Creating development aliases..."
cat >> "$ALIASES_DIR/aliases" << 'EOF'
# Git shortcuts
alias gs='git status'
alias ga='git add'
alias gc='git commit -m'
alias gp='git push'
alias gl='git pull'
alias gb='git branch'
alias gco='git checkout'
alias gd='git diff'
alias glog='git log --oneline --graph --decorate'
# Docker shortcuts
alias dps='docker ps'
alias dpa='docker ps -a'
alias di='docker images'
alias dprune='docker system prune -f'
alias dstop='docker stop $(docker ps -q)'
# Text editor shortcuts
alias v='vim'
alias n='nano'
alias code='code .'
# Directory shortcuts
alias projects='cd ~/projects'
alias downloads='cd ~/Downloads'
alias docs='cd ~/Documents'
# Python development
alias py='python3'
alias pip='pip3'
alias venv='python3 -m venv'
alias activate='source venv/bin/activate'
# Node.js development
alias ni='npm install'
alias ns='npm start'
alias nt='npm test'
alias nb='npm run build'
EOF
}
# Create shell functions
create_functions() {
print_status "[6/8] Creating shell functions..."
cat > "$ALIASES_DIR/functions" << 'EOF'
# Create and enter directory
mkcd() {
mkdir -p "$1" && cd "$1"
}
# Extract various archive formats
extract() {
if [ -f "$1" ]; then
case "$1" in
*.tar.bz2) tar xjf "$1" ;;
*.tar.gz) tar xzf "$1" ;;
*.bz2) bunzip2 "$1" ;;
*.rar) unrar x "$1" ;;
*.gz) gunzip "$1" ;;
*.tar) tar xf "$1" ;;
*.tbz2) tar xjf "$1" ;;
*.tgz) tar xzf "$1" ;;
*.zip) unzip "$1" ;;
*.Z) uncompress "$1" ;;
*.7z) 7z x "$1" ;;
*) echo "'$1' cannot be extracted" ;;
esac
else
echo "'$1' is not a valid file"
fi
}
# Find process by name
findproc() {
ps aux | grep -i "$1" | grep -v grep
}
# Create backup of file
backup() {
cp "$1" "$1.backup.$(date +%Y%m%d_%H%M%S)"
echo "Backup created: $1.backup.$(date +%Y%m%d_%H%M%S)"
}
# Show system information
sysinfo() {
echo "System Information:"
echo "==================="
echo "Hostname: $(hostname)"
echo "OS: $(cat /etc/os-release | grep PRETTY_NAME | cut -d'"' -f2)"
echo "Kernel: $(uname -r)"
echo "Uptime: $(uptime -p)"
echo "Memory: $(free -h | grep Mem | awk '{print $3"/"$2}')"
echo "Disk: $(df -h / | tail -1 | awk '{print $3"/"$2" ("$5" used)"}')"
}
EOF
}
# Configure shell to load aliases and functions
configure_shell() {
print_status "[7/8] Configuring shell..."
# Create configuration file if it doesn't exist
touch "$CONFIG_FILE"
chmod 644 "$CONFIG_FILE"
# Check if our configuration is already present
if ! grep -q "# Custom aliases and functions" "$CONFIG_FILE"; then
cat >> "$CONFIG_FILE" << EOF
# Custom aliases and functions
if [ -f ~/.config/shell/aliases ]; then
source ~/.config/shell/aliases
fi
if [ -f ~/.config/shell/functions ]; then
source ~/.config/shell/functions
fi
EOF
print_status "Configuration added to $CONFIG_FILE"
else
print_warning "Configuration already exists in $CONFIG_FILE"
fi
}
# Verify installation
verify_installation() {
print_status "[8/8] Verifying installation..."
local errors=0
# Check if files exist
for file in "$ALIASES_DIR/aliases" "$ALIASES_DIR/functions"; do
if [[ ! -f "$file" ]]; then
print_error "Missing file: $file"
((errors++))
fi
done
# Check if configuration is in shell config
if ! grep -q "Custom aliases and functions" "$CONFIG_FILE"; then
print_error "Configuration not found in $CONFIG_FILE"
((errors++))
fi
if [[ $errors -eq 0 ]]; then
print_status "Installation completed successfully!"
print_status "Restart your shell or run: source $CONFIG_FILE"
print_status "Aliases and functions are available in: $ALIASES_DIR"
print_status "Backups are stored in: $BACKUP_DIR"
else
print_error "Installation completed with $errors errors"
exit 1
fi
}
# Main installation function
main() {
print_status "Starting shell aliases and functions installation..."
detect_distro
detect_shell
create_backups
create_directories
install_packages
create_system_aliases
create_dev_aliases
create_functions
configure_shell
verify_installation
print_status "Installation complete! Detected: $ID with $SHELL_TYPE shell"
}
# Run main function
main "$@"
Review the script before running. Execute with: bash install.sh