Bash Aliases and Tricks for Sysadmin

A collection of bash aliases and functions that simplify daily work on a Linux server. Add them to /root/.bashrc or /root/.bash_aliases.

02

Basic setup

bash
nano ~/.bashrc

Add at the end of the file:

bash
# Load separate alias file (if you prefer to keep it separate)
if [ -f ~/.bash_aliases ]; then
    . ~/.bash_aliases
fi

To apply immediately without logout:

bash
source ~/.bashrc
03

Navigation and file aliases

bash
# Quick navigation
alias ..='cd ..'
alias ...='cd ../..'
alias ....='cd ../../..'
alias ~='cd ~'

# ls with colors and info
alias ls='ls --color=auto'
alias ll='ls -lah'
alias la='ls -A'
alias lt='ls -lath'        # sort by date, newest first

# grep with colors
alias grep='grep --color=auto'
alias egrep='egrep --color=auto'

# File operations with confirmation
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'

# Readable disk space
alias df='df -h'
alias du='du -h'
alias duh='du -sh *'       # show size of each item in current directory

# Create intermediate directories automatically
alias mkdir='mkdir -pv'
04

System and monitoring aliases

bash
# Quick system info
alias meminfo='free -h'
alias cpuinfo='lscpu'
alias diskinfo='lsblk -f'

# Processes sorted by CPU and RAM
alias pscpu='ps aux --sort=-%cpu | head -15'
alias psram='ps aux --sort=-%mem | head -15'

# Top in batch mode (single snapshot)
alias topcpu='top -bn1 | head -25'

# Active network connections
alias netstat='ss -tulpn'
alias ports='ss -tulpn | grep LISTEN'

# CPU temperature (requires lm-sensors)
alias temp='sensors 2>/dev/null || cat /sys/class/thermal/thermal_zone*/temp'

# Readable uptime
alias uptime='uptime -p'
05

systemd and service aliases

bash
# systemctl shortcuts
alias sc='systemctl'
alias scs='systemctl status'
alias scr='systemctl restart'
alias scst='systemctl start'
alias scsp='systemctl stop'
alias sce='systemctl enable'
alias scd='systemctl disable'
alias scl='systemctl list-units --type=service --state=running'

# Journal log live (last 50 lines + follow)
alias jf='journalctl -f'
alias jfe='journalctl -f -p err'       # errors only
alias jfu='journalctl -u'              # usage: jfu nginx

# Example: scs nginx, scr php8.2-fpm
06

Nginx, Apache, PHP aliases

bash
# Nginx
alias ng-test='nginx -t'
alias ng-reload='systemctl reload nginx'
alias ng-restart='systemctl restart nginx'
alias ng-log='tail -f /var/log/nginx/error.log'
alias ng-access='tail -f /var/log/nginx/access.log'

# Apache
alias ap-test='apache2ctl configtest'
alias ap-reload='systemctl reload apache2'

# PHP-FPM (adjust version)
alias php-restart='systemctl restart php8.2-fpm'
alias php-log='tail -f /var/log/php8.2-fpm.log'

# MySQL/MariaDB
alias mysql-root='mysql -u root -p'
alias mysql-size="mysql -u root -e \"SELECT table_schema AS 'DB', ROUND(SUM(data_length + index_length) / 1024 / 1024, 2) AS 'Size (MB)' FROM information_schema.tables GROUP BY table_schema;\""
07

Docker aliases

bash
# Docker shortcuts
alias dk='docker'
alias dkps='docker ps'
alias dkpsa='docker ps -a'
alias dkimg='docker images'
alias dklogs='docker logs -f'          # usage: dklogs container_name
alias dkexec='docker exec -it'         # usage: dkexec container_name bash
alias dkstop='docker stop'
alias dkrm='docker rm'
alias dkrmi='docker rmi'

# docker-compose shortcuts
alias dc='docker compose'
alias dcu='docker compose up -d'
alias dcd='docker compose down'
alias dcr='docker compose restart'
alias dcl='docker compose logs -f'
alias dcp='docker compose pull'

# Docker cleanup
alias dk-clean='docker system prune -f'
alias dk-cleanall='docker system prune -a -f --volumes'
08

Advanced bash functions

Functions can accept arguments, unlike aliases.

bash
# Search in system logs
logsearch() {
  journalctl --no-pager | grep -i "$1" | tail -50
}
# Usage: logsearch "failed"

# Quick file backup (adds timestamp)
bak() {
  cp "$1" "${1}.bak.$(date +%Y%m%d_%H%M%S)"
  echo "Backup created: ${1}.bak.$(date +%Y%m%d_%H%M%S)"
}
# Usage: bak /etc/nginx/nginx.conf

# Extract any archive
extract() {
  case "$1" in
    *.tar.bz2) tar xjf "$1" ;;
    *.tar.gz)  tar xzf "$1" ;;
    *.tar.xz)  tar xJf "$1" ;;
    *.bz2)     bunzip2 "$1" ;;
    *.gz)      gunzip "$1" ;;
    *.zip)     unzip "$1" ;;
    *.7z)      7z x "$1" ;;
    *.tar)     tar xf "$1" ;;
    *) echo "Format not recognized: $1" ;;
  esac
}
# Usage: extract archive.tar.gz

# Connect SSH and remember in command history
sshlog() {
  echo "Connecting to $1..."
  ssh "$@"
}

# Show open ports with the process using them
whoisport() {
  ss -tlpn | grep ":$1 "
}
# Usage: whoisport 80

# Find recently modified files
recent() {
  find "${1:-.}" -maxdepth "${2:-3}" -type f -newer /tmp/recent_mark 2>/dev/null || \
  find "${1:-.}" -maxdepth "${2:-3}" -type f -mmin -60
}

# Check if a service uses too much CPU
topper() {
  watch -n 1 "ps aux --sort=-%cpu | head -10"
}
09

Custom bash prompt

Add to ~/.bashrc for a colored prompt that shows user, host, path and last command exit code:

bash
# Colors
RED='\[\033[0;31m\]'
GREEN='\[\033[0;32m\]'
YELLOW='\[\033[1;33m\]'
BLUE='\[\033[0;34m\]'
CYAN='\[\033[0;36m\]'
RESET='\[\033[0m\]'

# Prompt: [exitcode] user@host:path$
PS1='$(RET=$?; if [ $RET != 0 ]; then echo "'${RED}'[$RET] "; fi)'
PS1+="${GREEN}\u${RESET}@${YELLOW}\h${RESET}:${CYAN}\w${RESET}\$ "
10

Complete .bashrc file (ready to use)

bash
# Apply all at once:
curl -s https://raw.githubusercontent.com/... > ~/.bash_aliases
source ~/.bash_aliases

Aliases defined in .bashrc or .bash_aliases are available only in interactive bash sessions. To make them available in scripts or cron, add them directly to the script or use source ~/.bashrc at the beginning.

11

Most used aliases: summary

AliasEquivalent to
llls -lah
portsss -tulpn | grep LISTEN
pscpups aux --sort=-%cpu | head -15
scs nginxsystemctl status nginx
scr nginxsystemctl restart nginx
jfu nginxjournalctl -u nginx -f
dkpsdocker ps
dc up -ddocker compose up -d
ng-logtail -f /var/log/nginx/error.log

DeluxHost, fondata nel 2023, offre soluzioni di hosting di alta qualità per diverse esigenze digitali. Forniamo hosting condiviso, VPS e server dedicati con sicurezza avanzata e datacenter globali.

© DeluxHost, Tutti i diritti riservati. | Partita IVA: IT17734661006
Tutti i sistemi operativi