|

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
CommandDescription
aliasShow all existing aliases
alias lmh='cd ~/cli; ls'Create a temporary alias
nano ~/.bashrcEdit shell config file
source ~/.bashrcApply changes
unalias lmhRemove 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):

AliasExpands To
llls -alF
lals -A
lls -CF
lsls --color=auto
grepgrep --color=auto

Check a specific alias:

$ alias ll
alias ll='ls -alF'

Creating Temporary Aliases

alias lmh='cd ~/cli; ls'

Syntax:

alias name='command'
PartDescription
aliasThe command
nameThe alias name
commandThe 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:

FileWhen It’s Loaded
~/.bashrcEvery interactive bash session
~/.bash_profileLogin shells (rarely for aliases)
~/.profileLogin shells (alternative)
/etc/bash.bashrcSystem-wide (all users)

For most cases, use ~/.bashrc.


Adding the Alias

  1. Open the file:
nano ~/.bashrc
  1. Scroll to the end of the file (or a section marked “Aliases”)
  2. Add your alias:
# Custom aliases
alias lmh='cd ~/cli; ls'
alias ll='ls -la'
alias gs='git status'
alias ..='cd ..'
  1. 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
CommandDescription
unalias lmhRemove alias lmh
unalias -aRemove all aliases
\lmhBypass 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

CommandDescription
aliasList all aliases
alias name='cmd'Create an alias
alias nameShow a specific alias
unalias nameRemove an alias
unalias -aRemove all aliases
\cmd or command cmdBypass an alias

Shell Config Files

FilePurpose
~/.bashrcInteractive bash sessions
~/.bash_profileLogin shells
~/.profileLogin shells (fallback)
/etc/bash.bashrcSystem-wide

Reload Config

CommandDescription
source ~/.bashrcReload bashrc
. ~/.bashrcShorthand
(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

PitfallProblemSolution
Forgot to reloadAliases don’t worksource ~/.bashrc
Double quotesVariables expand too earlyUse single quotes
Spaces around =ErrorNo spaces: alias x='cmd'
Alias lost after restartNot savedAdd to ~/.bashrc
Overwrote important commandBad behaviorAvoid naming conflicts
Complex logic in aliasDoesn’t workUse 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"
}
AspectAliasFunction
ComplexitySimple substitutionFull shell logic
ArgumentsAppended at endFull control
Control flowNoYes (if, for, while)
Local variablesNoYes
Best forShortcutsComplex 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

ActionCommand
View aliasesalias
Create temporary aliasalias name='cmd'
Edit confignano ~/.bashrc
Apply changessource ~/.bashrc
Remove aliasunalias name
Remove all aliasesunalias -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 ~/.bashrc applies changes immediately
  • unalias removes an alias
  • Single quotes prevent early variable expansion
  • ; separates multiple commands in an alias
  • Use \cmd or command cmd to 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!