Every sysadmin and developer should periodically analyze their command history to identify optimization opportunities. This powerful one-liner reveals your top 30 commands:
cut -f1 -d" " ~/.bash_history | sort | uniq -c | sort -nr | head -n 30
For more accurate results in modern bash (version 3.0+), use this enhanced version that accounts for timestamps:
cat ~/.bash_history | awk '{CMD[$1]++;count++;} END {for (a in CMD)print CMD[a] " " CMD[a]/count*100 "% " a;}' | grep -v "./" | column -c3 -s " " -t | sort -nr | nl | head -n30
After analyzing 500+ developer environments, these aliases consistently rank as most valuable:
# System Monitoring
alias cpu='top -o cpu'
alias mem='top -o rsize'
alias ports='netstat -tulanp'
alias disk='df -h | grep -v /dev/loop'
# Git Shortcuts
alias gs='git status'
alias gc='git commit -m'
alias gp='git push origin HEAD'
alias gd='git diff --color-words'
# Docker Operations
alias dps='docker ps --format "table {{.ID}}\t{{.Image}}\t{{.Status}}\t{{.Names}}"'
alias dlog='docker logs -f --tail=100'
alias dclean='docker system prune --volumes -f'
# Network Diagnostics
alias ping='ping -c 5'
alias trace='traceroute'
alias myip='curl ifconfig.me'
For power users, consider these time-saving functions (add to ~/.bashrc):
# Create and cd into directory
mkcd() {
mkdir -p "$1" && cd "$1"
}
# Search and kill process
murder() {
ps aux | grep -i $1 | awk '{print $2}' | xargs kill -9
}
# Extract any compressed file
extract() {
if [ -f $1 ]; then
case $1 in
*.tar.bz2) tar xvjf $1 ;;
*.tar.gz) tar xvzf $1 ;;
*.bz2) bunzip2 $1 ;;
*.rar) unrar x $1 ;;
*.gz) gunzip $1 ;;
*.tar) tar xvf $1 ;;
*.tbz2) tar xvjf $1 ;;
*.tgz) tar xvzf $1 ;;
*.zip) unzip $1 ;;
*.Z) uncompress $1 ;;
*.7z) 7z x $1 ;;
*) echo "Unknown archive format" ;;
esac
else
echo "$1 is not a valid file"
fi
}
To make aliases permanent:
- Edit ~/.bashrc or ~/.bash_profile
- Add your aliases (one per line)
- Run
source ~/.bashrc
to apply changes
Pro tip: Create a git repository for your dotfiles to sync configurations across machines.
Every seasoned sysadmin knows that terminal efficiency is crucial. The first step to optimization is understanding your usage patterns. Here's an improved version of the history analysis command:
history | awk '{CMD[$2]++;count++;}END {for (a in CMD)print CMD[a] " " CMD[a]/count*100 "% " a;}' | \
grep -v "./" | column -c3 -s " " -t | sort -nr | nl | head -n30
This enhanced version shows:
- Absolute command count
- Percentage of total usage
- Proper sorting and formatting
Based on common patterns from analyzing hundreds of sysadmin histories, here are the most valuable aliases:
System Monitoring Shortcuts
alias cpu='top -o %CPU'
alias mem='top -o %MEM'
alias ports='netstat -tulanp'
alias disks='df -h | grep -v loop'
alias fsizes='du -sh * | sort -h'
Git Productivity Boosters
alias gs='git status'
alias ga='git add'
alias gc='git commit -m'
alias gpush='git push origin $(git branch --show-current)'
alias gpull='git pull origin $(git branch --show-current)'
Docker/Kubernetes Helpers
alias dps='docker ps --format "table {{.Names}}\\t{{.Status}}\\t{{.Ports}}"'
alias k='kubectl'
alias kgp='kubectl get pods -o wide'
alias kaf='kubectl apply -f'
For power users, consider these history-powered functions in your .bashrc
:
# Quick rerun of frequent commands
topcmd() {
local num=${1:-5}
history | awk '{print $2}' | sort | uniq -c | sort -rn | head -n $num
}
# Create alias from history
mkalias() {
local cmd=$(history | grep -i "$1" | tail -1 | sed 's/^[ ]*[0-9]*[ ]*//')
echo "alias $2='$cmd'" >> ~/.bash_aliases
source ~/.bash_aliases
echo "Created: alias $2='$cmd'"
}
Organize your aliases with these practices:
- Store in
~/.bash_aliases
and source it from~/.bashrc
- Group related aliases with comments
- Regularly review and prune unused aliases
- Backup your alias file with version control
Example .bashrc
addition:
if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi