What is alias?
The alias command creates shortcuts for longer commands or sets of commands. It's one of the most powerful tools for customizing your shell experience and improving productivity. Aliases are temporary by default (session only) but can be made permanent by adding them to your shell configuration file (~/.bashrc, ~/.zshrc, etc.).
Example 1List All Current Aliases
alias
Shows all currently defined aliases in your shell session. This is useful for seeing what shortcuts are already available.
alias ll='ls -alF'
alias la='ls -A'
alias l='ls -CF'
alias grep='grep --color=auto'
Example 2Create a Simple Alias
alias ll='ls -lah'
Creates an alias 'll' that runs 'ls -lah' (long listing format, all files including hidden, human-readable sizes). This is one of the most common aliases.
Note: No spaces around the = sign! The syntax is: alias name='command'
Example 3Safety Aliases (Confirmation Prompts)
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
These aliases add interactive mode (-i) to potentially destructive commands, prompting you for confirmation before deleting, copying over, or moving files.
Pro Tip: Many system administrators add these to their .bashrc as a safety net against accidental file deletion.
Example 4Navigation Shortcuts
alias ..='cd ..'
alias ...='cd ../..'
alias ....='cd ../../..'
alias home='cd ~'
Speed up directory navigation with quick shortcuts. Instead of typing 'cd ..' repeatedly, just type '..' or '...' to move up multiple directory levels.
Example 5System Information Aliases
alias ports='netstat -tulanp'
alias meminfo='free -m -l -t'
alias cpuinfo='lscpu'
alias diskspace='df -h'
Create memorable shortcuts for system monitoring commands. These make it easier to check ports, memory usage, CPU info, and disk space without remembering complex options.
Example 6Git Shortcuts
alias gs='git status'
alias ga='git add'
alias gc='git commit'
alias gp='git push'
alias gl='git log --oneline'
For developers who use Git frequently, these aliases save enormous amounts of typing. Turn 'git status' into just 'gs'.
Example 7Update System Alias
# For Debian/Ubuntu
alias update='sudo apt update && sudo apt upgrade -y'
# For RHEL/CentOS/Fedora
alias update='sudo dnf update -y'
Create a single command to update your entire system. The && ensures the second command only runs if the first succeeds.
Warning: The -y flag automatically answers "yes" to prompts. Use cautiously on production systems.
Example 8Show a Specific Alias
alias ll
Display the definition of a specific alias. Useful when you know an alias exists but can't remember what it does.
alias ll='ls -lah'
Example 9Remove an Alias
unalias ll
Remove an alias from the current session. This doesn't affect permanent aliases in your configuration files.
Pro Tip: To temporarily bypass an alias without removing it, prefix the command with a backslash: \ls
Example 10Aliases with Parameters (Functions)
# This won't work as expected:
alias findf='find . -name'
# Use a function instead:
findf() {
find . -name "$1"
}
Aliases can't directly take parameters. For commands that need arguments in the middle, use a shell function instead. Add functions to your .bashrc just like aliases.
Example 11Clear Screen and List Files
alias cls='clear && ls'
Chain multiple commands together. This alias clears the screen and then lists directory contents - useful for a fresh view.
Example 12Extract Archives
alias extract='tar -xvf'
alias untar='tar -xvf'
alias targz='tar -xzvf'
Simplify archive extraction with memorable names. No more remembering whether it's -xzvf or -xvf.
Example 13Find Large Files
alias big='du -ah | sort -rh | head -20'
Quickly find the 20 largest files/directories in the current location. Incredibly useful for finding what's eating up disk space.
Example 14Network Testing
alias ping='ping -c 5'
alias fastping='ping -c 100 -i 0.2'
alias myip='curl ifconfig.me'
Limit ping to 5 packets by default, create a fast ping option, and easily get your public IP address.
Example 15Make Aliases Permanent
# Add to ~/.bashrc or ~/.zshrc
echo "alias ll='ls -lah'" >> ~/.bashrc
# Then reload the configuration
source ~/.bashrc
Aliases created in the terminal only last for that session. To make them permanent, add them to your shell configuration file and reload it.
Note: Use ~/.zshrc if you're using Zsh shell, ~/.bashrc for Bash. Check with: echo $SHELL
📚 Additional Information
Common Alias Patterns
| Category | Alias | Command |
|---|---|---|
| Listing | ll | ls -lah |
| Listing | la | ls -A |
| Navigation | .. | cd .. |
| Safety | rm | rm -i |
| Git | gs | git status |
| System | update | sudo apt update && sudo apt upgrade |
Best Practices
- Keep it memorable: Use short, intuitive names that you'll remember
- Don't override system commands: Avoid aliasing to names like 'ls', 'cd', etc. unless intentional
- Document your aliases: Add comments in your .bashrc so you remember what they do
- Group related aliases: Keep navigation aliases together, git aliases together, etc.
- Test before making permanent: Try aliases in the terminal first before adding to .bashrc
Where to Store Aliases
- ~/.bashrc - For Bash shell users (most common)
- ~/.zshrc - For Zsh shell users
- ~/.bash_aliases - Separate file sourced by .bashrc (cleaner organization)
- ~/.profile - Login shell configuration (less common for aliases)
Alias vs Function: When to Use Which?
- Use alias when: Command is simple, no parameters needed, or parameters only at the end
- Use function when: Need parameters in the middle, complex logic, conditionals, or loops
Debugging Aliases
# See all aliases
alias
# See specific alias
alias ll
# Check if command is an alias
type ll
# Temporarily disable an alias
\ll # Backslash bypasses the alias
# Remove an alias
unalias llAdvanced: Conditional Aliases
# Different aliases based on OS
if [[ "$OSTYPE" == "darwin"* ]]; then
# macOS
alias ls='ls -G'
else
# Linux
alias ls='ls --color=auto'
fiReal-World Sysadmin Aliases
# Quick log viewing
alias logs='sudo tail -f /var/log/syslog'
alias apachelog='sudo tail -f /var/log/apache2/error.log'
# Service management
alias restart-web='sudo systemctl restart apache2'
alias check-web='sudo systemctl status apache2'
# Security
alias listening='sudo netstat -tlnp'
alias fw='sudo iptables -L -n -v'
# Backups
alias backup-home='rsync -av --progress /home /backup/'Fun and Useful Aliases
# History with timestamps
alias h='history | tail -20'
# Make directories and cd into them
mkcd() { mkdir -p "$1" && cd "$1"; }
# Weather report
alias weather='curl wttr.in'
# Generate random password
alias genpass='openssl rand -base64 16'
# Show path in readable format
alias path='echo $PATH | tr ":" "\n"'Important Notes
Security Note: Be careful with aliases that include sudo or passwords. Never hardcode sensitive information in aliases.
Pro Tip: Many developers maintain their dotfiles (including .bashrc with aliases) in a Git repository for easy backup and sharing across machines.