Linux CLI 10 ๐ง create your aliases
Aliases let you create custom shortcuts for commands โ long commands, frequent combinations, or anything you type often. They save time and reduce typos.
Overview
alias
alias lmh='cd ~/cli; ls'
nano ~/.bashrc
# add at the end:
alias lmh='cd ~/cli; ls'
# save
source ~/.bashrc
unalias lmh
| Command | Description |
|---|---|
alias | Show all existing aliases |
alias lmh='cd ~/cli; ls' | Create a temporary alias |
nano ~/.bashrc | Edit shell config file |
source ~/.bashrc | Apply changes |
unalias lmh | Remove an alias |
What Are Aliases?
An alias is a shortcut for a longer command (or a sequence of commands).
Without aliases:
$ cd ~/cli
$ ls -la
$ cd ~/projects/current
$ git status
With aliases:
$ cli # โ cd ~/cli; ls
$ gs # โ git status
$ ll # โ ls -la
Key points:
- Aliases are shell features โ not separate programs
- They exist only in the current shell session unless saved
- They can override existing commands (with caution!)
- They’re evaluated before the command runs
Viewing Existing Aliases
alias
Example output:
$ alias
alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l='ls -CF'
alias la='ls -A'
alias ll='ls -alF'
alias ls='ls --color=auto'
Common pre-existing aliases (from /etc/bash.bashrc or ~/.bashrc):
| Alias | Expands To |
|---|---|
ll | ls -alF |
la | ls -A |
l | ls -CF |
ls | ls --color=auto |
grep | grep --color=auto |
Check a specific alias:
$ alias ll
alias ll='ls -alF'
Creating Temporary Aliases
alias lmh='cd ~/cli; ls'
Syntax:
alias name='command'
| Part | Description |
|---|---|
alias | The command |
name | The alias name |
command | The actual command(s) |
Rules:
- Use single quotes to prevent immediate expansion
- No spaces around
= - The alias name is usually lowercase (convention)
Examples
Simple aliases:
alias ll='ls -la' # Long listing
alias la='ls -A' # Show hidden files
alias ..='cd ..' # Parent directory
alias ...='cd ../..' # Two levels up
alias c='clear' # Clear screen
alias h='history' # Command history
Aliases with arguments:
alias grep='grep --color=auto' # Colorize grep output
alias df='df -h' # Human-readable disk usage
alias du='du -h' # Human-readable disk usage
Aliases with multiple commands (; separator):
alias lmh='cd ~/cli; ls' # Change dir + list
alias update='sudo apt update; sudo apt upgrade' # System update
alias gitlog='git log --oneline --graph --all' # Git history
Aliases with pipes:
alias biggest='du -sh * | sort -rh | head -10' # Top 10 largest files
alias ports='netstat -tulanp | grep LISTEN' # Open ports
Note: The
;separator runs commands sequentially โ the second command runs after the first, regardless of success.
Temporary vs Permanent
Temporary (current session only):
$ alias ll='ls -la'
$ ll
# Works now
# But after closing terminal โ gone!
Why this matters:
- Great for testing an alias before saving it
- Useful for one-off session customizations
- Lost on shell exit, terminal close, or system restart
Making Aliases Permanent
To make an alias persist across sessions, add it to your shell’s configuration file.
nano ~/.bashrc
Shell config files:
| File | When It’s Loaded |
|---|---|
~/.bashrc | Every interactive bash session |
~/.bash_profile | Login shells (rarely for aliases) |
~/.profile | Login shells (alternative) |
/etc/bash.bashrc | System-wide (all users) |
For most cases, use ~/.bashrc.
Adding the Alias
- Open the file:
nano ~/.bashrc
- Scroll to the end of the file (or a section marked “Aliases”)
- Add your alias:
# Custom aliases
alias lmh='cd ~/cli; ls'
alias ll='ls -la'
alias gs='git status'
alias ..='cd ..'
- Save and exit:
Ctrl + XโYโEnter
Applying the Changes
After editing ~/.bashrc, you need to reload it:
source ~/.bashrc
Or the shorthand:
. ~/.bashrc
Why? The file is read once when the shell starts. Editing it doesn’t affect the current session until you reload.
Alternative: Open a new terminal โ it will read the updated file.
Removing Aliases
unalias lmh
| Command | Description |
|---|---|
unalias lmh | Remove alias lmh |
unalias -a | Remove all aliases |
\lmh | Bypass the alias for one command |
Examples:
# Remove a specific alias
$ unalias ll
# Remove all aliases
$ unalias -a
# Bypass an alias temporarily
$ \ls # Runs the real ls, not the alias
$ command ls # Also bypasses alias
Note: unalias removes the alias from the current session only. If you want to permanently remove it, delete it from ~/.bashrc and reload.
Complete Example Session
# ============================================
# PART 1: VIEW EXISTING ALIASES
# ============================================
$ alias
alias alert='notify-send ...'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l='ls -CF'
alias la='ls -A'
alias ll='ls -alF'
alias ls='ls --color=auto'
# Check a specific alias
$ alias ll
alias ll='ls -alF'
# ============================================
# PART 2: CREATE A TEMPORARY ALIAS
# ============================================
# Create a simple alias
$ alias lmh='cd ~/cli; ls'
# Use it
$ lmh
# Changes to ~/cli and lists files
# Create a few more
$ alias gs='git status'
$ alias ..='cd ..'
$ alias c='clear'
# Test them
$ gs
On branch main
Nothing to commit, working tree clean
$ .. # Go up one directory
$ c # Clear screen
# ============================================
# PART 3: VERIFY TEMPORARY ALIASES
# ============================================
$ alias
alias ..='cd ..'
alias c='clear'
alias gs='git status'
alias lmh='cd ~/cli; ls'
# ... plus existing aliases
# ============================================
# PART 4: MAKE PERMANENT
# ============================================
# Open bashrc
$ nano ~/.bashrc
# Scroll to the end, add:
# --------------------------------------------------
# Custom aliases
# --------------------------------------------------
alias lmh='cd ~/cli; ls'
alias gs='git status'
alias ..='cd ..'
alias c='clear'
alias ll='ls -la'
alias la='ls -A'
alias grep='grep --color=auto'
# --------------------------------------------------
# Save: Ctrl+X, then Y, then Enter
# ============================================
# PART 5: APPLY CHANGES
# ============================================
$ source ~/.bashrc
# Verify they work
$ gs
On branch main
...
$ lmh
# Changes directory and lists
# ============================================
# PART 6: OPEN NEW TERMINAL
# ============================================
# Close and reopen the terminal
# The aliases should still be there!
$ alias gs
alias gs='git status'
# โ
Persisted!
# ============================================
# PART 7: REMOVE AN ALIAS
# ============================================
# Remove from current session
$ unalias gs
$ gs
gs: command not found
# โ
Removed
# But it's still in ~/.bashrc
$ grep gs ~/.bashrc
alias gs='git status'
# To remove permanently, edit ~/.bashrc
$ nano ~/.bashrc
# Delete the line, save, and reload
$ source ~/.bashrc
# Now gs is gone permanently
# ============================================
# PART 8: BYPASS AN ALIAS
# ============================================
$ alias ls='ls --color=auto'
$ \ls # Runs real ls (no colors)
$ command ls # Also bypasses alias
Quick Reference
Basic Alias Commands
| Command | Description |
|---|---|
alias | List all aliases |
alias name='cmd' | Create an alias |
alias name | Show a specific alias |
unalias name | Remove an alias |
unalias -a | Remove all aliases |
\cmd or command cmd | Bypass an alias |
Shell Config Files
| File | Purpose |
|---|---|
~/.bashrc | Interactive bash sessions |
~/.bash_profile | Login shells |
~/.profile | Login shells (fallback) |
/etc/bash.bashrc | System-wide |
Reload Config
| Command | Description |
|---|---|
source ~/.bashrc | Reload bashrc |
. ~/.bashrc | Shorthand |
| (open new terminal) | Fresh shell reads config |
Best Practices
โ Do This:
# Use single quotes for aliases
alias ll='ls -la'
# Test temporary aliases first
alias myalias='long command'
# Test it...
# If good, add to ~/.bashrc
# Group aliases with comments
# In ~/.bashrc:
# ===== Navigation =====
alias ..='cd ..'
alias ...='cd ../..'
alias home='cd ~'
# ===== Git =====
alias gs='git status'
alias ga='git add'
alias gc='git commit'
# ===== System =====
alias update='sudo apt update && sudo apt upgrade'
alias ports='netstat -tulanp | grep LISTEN'
# Use \ or command to bypass when needed
\ls # Real ls
command ls # Real ls
โ Don’t Do This:
# Don't use double quotes for aliases that contain $
alias myalias="echo $HOME" # โ $HOME expanded NOW
alias myalias='echo $HOME' # โ
Expanded when alias runs
# Don't override commands you use
alias ls='rm -rf' # โ DANGEROUS!
alias cd='ls' # โ Breaks navigation
# Don't create confusing names
alias x='ls -la' # โ ๏ธ Too vague
alias l='ls -la' # โ
Common convention
# Don't forget to reload after editing
nano ~/.bashrc
# ... add aliases ...
# Forgot: source ~/.bashrc
# Aliases won't work until you reload!
# Don't use alias for complex logic
alias myfunc='if [ -f file ]; then ...' # โ Use a function!
Common Pitfalls
| Pitfall | Problem | Solution |
|---|---|---|
| Forgot to reload | Aliases don’t work | source ~/.bashrc |
| Double quotes | Variables expand too early | Use single quotes |
Spaces around = | Error | No spaces: alias x='cmd' |
| Alias lost after restart | Not saved | Add to ~/.bashrc |
| Overwrote important command | Bad behavior | Avoid naming conflicts |
| Complex logic in alias | Doesn’t work | Use a shell function |
Aliases vs Functions
When aliases aren’t enough, use functions:
Alias (simple):
alias ll='ls -la'
Function (complex):
mkcd() {
mkdir -p "$1"
cd "$1"
}
| Aspect | Alias | Function |
|---|---|---|
| Complexity | Simple substitution | Full shell logic |
| Arguments | Appended at end | Full control |
| Control flow | No | Yes (if, for, while) |
| Local variables | No | Yes |
| Best for | Shortcuts | Complex operations |
Rule of thumb: If your alias needs if, for, while, or variable manipulation โ use a function instead.
Real-World Examples
1. Web Developer Aliases
# In ~/.bashrc
alias ll='ls -la'
alias serve='python3 -m http.server 8000'
alias npmr='npm run'
alias gs='git status'
alias gp='git push'
alias gl='git log --oneline --graph --all'
alias db='docker build -t myapp .'
alias dr='docker run -p 3000:3000 myapp'
2. System Administrator Aliases
# In ~/.bashrc (or /etc/bash.bashrc for all users)
alias ll='ls -la'
alias df='df -h'
alias du='du -h'
alias free='free -h'
alias ports='netstat -tulanp | grep LISTEN'
alias update='sudo apt update && sudo apt upgrade -y'
alias logs='sudo journalctl -xe'
alias services='systemctl list-units --type=service'
3. Quick Navigation
# In ~/.bashrc
alias ..='cd ..'
alias ...='cd ../..'
alias ....='cd ../../..'
alias home='cd ~'
alias docs='cd ~/Documents'
alias down='cd ~/Downloads'
alias proj='cd ~/projects'
alias cli='cd ~/cli'
4. Safety Aliases
# In ~/.bashrc
alias rm='rm -i' # Interactive remove
alias cp='cp -i' # Interactive copy
alias mv='mv -i' # Interactive move
alias mkdir='mkdir -p' # Create parents
โ ๏ธ Warning: Interactive safety aliases are great for beginners but can be annoying for experienced users who know what they’re doing.
Visual: How Aliases Work
You type: ll
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Shell checks for alias "ll" โ
โ โ Found! Expands to: ls -alF โ
โโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Shell runs: ls -alF โ
โ (the real command) โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Without alias, the shell would search:
1. Alias list
2. Function list
3. Builtins
4. PATH for executable
Summary
| Action | Command |
|---|---|
| View aliases | alias |
| Create temporary alias | alias name='cmd' |
| Edit config | nano ~/.bashrc |
| Apply changes | source ~/.bashrc |
| Remove alias | unalias name |
| Remove all aliases | unalias -a |
| Bypass alias | \cmd or command cmd |
Key takeaways:
- Aliases are shortcuts for longer commands
- Temporary aliases exist only in the current session
- Permanent aliases go in
~/.bashrc source ~/.bashrcapplies changes immediatelyunaliasremoves an alias- Single quotes prevent early variable expansion
;separates multiple commands in an alias- Use
\cmdorcommand cmdto bypass an alias - For complex logic, use a function instead of an alias
Remember: Aliases are one of the simplest and most powerful productivity boosters on the command line. Start with a few โ like ll for ls -la and .. for cd .. โ and add more as you discover patterns in your workflow. Put them in ~/.bashrc, and they’ll be there every time you open a terminal!
Stop using slow, ad-bloated tool sites! ๐คฎ
๐ Search “KandZ Tools” on Google to use many professional utilities for free.
KandZ.me is the ultimate minimalist hub for:
โ
Finance (Mortgage, Interest, Inflation)
โ
Tech (Base64, JSON, Dev Suite, IP)
โ
Health (BMI, BMR, TDEE)
โ
Productivity (Timer, Workspace, QR)
โก๏ธ Fast & Private
๐ No data leaves your device
๐ 100% Free
๐ Use it now: https://tools.kandz.me
๐ Bookmark itโyouโll need it later!