Linux CLI HowTo 12 ๐ง How to create your aliases
How to List All Existing Aliases
To see all currently defined aliases in your shell session:
- Simply type
aliasand press Enter - This will display all aliases in the format:
alias name='command' - Example output might show:
alias ll='ls -la'oralias gs='git status'
How to Create a Temporary Alias
To create an alias that only lasts for the current session:
- Type
aliasfollowed by your desired alias name and command - Use the format:
alias aliasname='command' - Example:
alias lmh='cd ~/cli; ls' - The semicolon separates multiple commands in sequence
- This alias will disappear when you close your terminal or restart your computer
How to Create a Permanent Alias
To make an alias available every time you start your shell:
- Open your shell configuration file using a text editor like nano:
nano ~/.bashrc - Navigate to the end of the file
- Add your alias on a new line:
alias lmh='cd ~/cli; ls' - Save the file by pressing Ctrl+X, then Y, then Enter
- Apply the changes by running:
source ~/.bashrcor restart your terminal
How to Remove an Alias
To delete an existing alias:
- Use the
unaliascommand followed by the alias name - Example:
โก๏ธunalias lmh - This removes only that specific alias from your current session
- The removal is temporary unless you make it permanent in your configuration file
How to Create Multiple Aliases
To add several aliases at once:
- Edit your configuration file with
nano ~/.bashrc - Add multiple lines of aliases:
alias ll='ls -la'
alias gs='git status'
alias ..='cd ..'
- Save and source the file to apply changes
How to Use Aliases Effectively
To create useful aliases for daily tasks:
- Identify frequently used command combinations
- Create short, memorable names that reflect what the alias does
- Example aliases for common tasks:
alias cdl='cd ~/cli; ls -la'(change directory and list)alias h='history'(short history command)alias dfh='df -h | grep -v tmpfs'(disk usage with filter)
How to Check Your Aliases Are Working
To verify your aliases work correctly:
- Type the alias name and press Enter
- The command should execute as if you typed the full command
- Example: After creating
alias lmh='cd ~/cli; ls', typinglmhshould change to cli directory and list files
How to Edit Existing Aliases
To modify an existing alias:
- Open your configuration file:
nano ~/.bashrc - Find the line with your existing alias
- Modify the command part of the alias
- Save and source the file again
- Example: Change from
alias ll='ls -la'toalias ll='ls -lah'
How to Create an Alias for Complex Commands
For complicated commands with multiple options:
- Use quotes to contain the entire command
- Example:
alias backup='tar -czf backup-$(date +%Y%m%d).tar.gz' - This creates a compressed backup with today’s date in the filename
- The alias makes complex archiving simple and repeatable
How to View Your Configuration File
To see all your aliases in one place:
- Open your configuration file:
nano ~/.bashrc - Scroll through to find all lines starting with
alias - This shows all your permanent aliases
- You can also use
grep alias ~/.bashrcto filter just the alias lines
How to Backup Your Aliases
To preserve your aliases for later use:
- Create a backup of your configuration file:
cp ~/.bashrc ~/.bashrc.backup - Or extract just your aliases with:
grep alias ~/.bashrc > my_aliases.txt - This allows you to restore aliases if needed or share them with others
How to Create an Alias with Parameters
For aliases that might need to accept arguments:
- Use functions instead of simple aliases for more complex scenarios
- Example function in your
.bashrc:
backup() {
tar -czf "backup-$1.tar.gz" "$2"
}
- This allows you to pass parameters when using the alias
How to Test Aliases Before Adding
To ensure your aliases work as expected:
- Test them temporarily without adding to configuration
- Use
alias testalias='command'and try it out - Once satisfied, add it permanently to your
.bashrc - This prevents breaking your shell if you make a mistake in the command
How to Remove All Aliases
To clear all aliases from current session:
- Type
unalias -ato remove all aliases - This is useful when debugging or resetting your shell environment
- Note: This only affects the current session, not permanent configuration
How to Find Your Shell Configuration File
To locate where your aliases should be stored:
- Check which shell you’re using:
echo $SHELL - For bash:
~/.bashrcor~/.bash_profile - For zsh:
~/.zshrc - The file location depends on your default shell type
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!