Blog » Linux » How to Use alias Command in Linux: Boost Productivity with Shell Shortcuts
› how-to-use-alias-command-in-linux

How to Use alias Command in Linux: Boost Productivity with Shell Shortcuts

Table of Contents

What is the alias Command in Linux?

Learning how to use the alias command in Linux changed the way I work in the terminal. If you spend any time on the command line, you know how repetitive typing can get. The alias command solves this problem by letting you create custom shortcuts for longer commands.

The Simple Definition: Custom Command Shortcuts

An alias is simply a nickname for a command or series of commands. Instead of typing a long string every time, you create a short word that expands into the full command when you press enter. Think of it like speed dial for your terminal.

For example, instead of typing ls -la --color=auto every time you want to see hidden files, you can create an alias called ll that does the same thing. Type two letters instead of twenty. That’s the power of aliases.

Why Aliases Matter for Command-Line Productivity

Aliases boost your productivity in three key ways:

  • Reduced typing: Fewer keystrokes mean faster work and less strain on your hands
  • Fewer errors: Complex commands with multiple flags are easy to mistype. Aliases eliminate that risk
  • Consistent workflows: Your favorite options are baked into shortcuts you use automatically

When you automate tasks with cron jobs, aliases become even more valuable. They streamline your interactive work while cron handles the scheduled stuff.

RackNerd Mobile Leaderboard Banner

Get a VPS from as low as $11/year! WOW!

My First Alias: The ‘ll’ That Changed Everything

I remember sitting in front of my first Ubuntu install back in the 8.04 Hardy Heron days. I kept typing ls -l over and over. A friend on IRC told me about aliases, and I created my first one: alias ll='ls -la'.

That tiny shortcut clicked something in my brain. Suddenly the terminal felt like mine. I could shape it to fit how I actually work. I went from tolerating the command line to genuinely enjoying it.

How the alias Command Actually Works

Understanding the mechanics helps you create better aliases. The Bash manual’s aliases documentation explains the technical details, but here’s the practical version.

The Basic Syntax: alias name=’command’

Creating a bash alias follows a simple pattern:

alias shortcut='full command here'

The syntax has three parts:

  • alias: The built-in command that creates the shortcut
  • shortcut: The name you’ll type (no spaces allowed)
  • command: The actual command in single quotes

Want to see all your current aliases? Just type alias with no arguments:

alias

This prints every active alias in your session. On most systems, you’ll see some defaults already configured.

Shell Substitution: What Happens Behind the Scenes

When you type an alias, the shell replaces your shortcut with the full command before execution. It happens instantly and invisibly. The alias essentially becomes a text substitution macro.

This matters because aliases are interpreted first. If you type ll and have an alias for it, Bash swaps in ls -la before doing anything else. Understanding this order helps when aliases don’t behave as expected.

Creating Your First alias: Temporary Shortcuts

Let’s build some practical aliases. These work immediately but disappear when you close your terminal. We’ll make them permanent later.

Simple Navigation Aliases

Directory navigation eats up keystrokes. These shortcuts help:

alias ..='cd ..'
alias ...='cd ../..'
alias ....='cd ../../..'
alias home='cd ~'
alias docs='cd ~/Documents'

I use navigation aliases constantly. Going up two directories with ... feels natural now. My fingers just know it.

Enhanced ls Commands

The ls command has many useful flags that nobody wants to type repeatedly:

alias ll='ls -alF'
alias la='ls -A'
alias l='ls -CF'
alias lh='ls -lh'
Quick Reference:

  • ll – Long format with hidden files and file type indicators
  • la – All files except . and ..
  • l – Compact format with columns
  • lh – Long format with human-readable file sizes

System Update Shortcuts

Package management commands are perfect for aliases. On Debian/Ubuntu systems:

alias update='sudo apt update && sudo apt upgrade -y'
alias install='sudo apt install'
alias search='apt search'

On Arch-based systems:

alias update='sudo pacman -Syu'
alias install='sudo pacman -S'
alias search='pacman -Ss'

I use Arch on my main workstation (yes, I’m one of those people), and my update alias has probably saved me hours of typing over the years.

Making Aliases Permanent: The .bashrc vs .bash_aliases Debate

Temporary aliases are great for testing. But you want your shortcuts available every time you open a terminal. That means storing them in configuration files.

Method 1: Adding Aliases Directly to .bashrc

The ~/.bashrc file runs every time you open an interactive Bash shell. You can add aliases directly to this file:

nano ~/.bashrc

Scroll to the bottom and add your aliases:

# My custom aliases
alias ll='ls -alF'
alias update='sudo apt update && sudo apt upgrade -y'
alias gs='git status'

Save and exit. This works, but your .bashrc can get cluttered fast.

Method 2: The .bash_aliases File (Recommended)

A cleaner approach uses a dedicated file for aliases. Create ~/.bash_aliases:

nano ~/.bash_aliases

Add all your aliases here, organized with comments:

# Navigation
alias ..='cd ..'
alias ...='cd ../..'

# Listing
alias ll='ls -alF'
alias la='ls -A'

# System
alias update='sudo apt update && sudo apt upgrade -y'

# Git
alias gs='git status'
alias ga='git add'
alias gc='git commit -m'

For this to work, your .bashrc needs to source the file. Most distributions include this by default:

if [ -f ~/.bash_aliases ]; then
    . ~/.bash_aliases
fi

Check your .bashrc for this snippet. If it’s missing, add it.

Why I Prefer .bash_aliases: I’ve maintained my dotfiles for over a decade now. Keeping aliases separate makes backup, version control, and sharing easier. My .bash_aliases file is organized by category with clear comments. When I set up a new machine, I just copy that one file. Clean and simple.

How to Source Your Aliases Without Logging Out

After editing configuration files, you need to reload them. Don’t close your terminal. Just run:

source ~/.bashrc

This reloads your configuration and activates new aliases immediately. I probably run this command ten times a day when tweaking my setup.

Essential Aliases Every Linux User Should Have

After years of refining my dotfiles, certain aliases have become essential. Here are my most-used shortcuts.

Productivity Boosters

# Quick edits
alias bashrc='nano ~/.bashrc'
alias aliases='nano ~/.bash_aliases'

# History search
alias hg='history | grep'

# Disk usage
alias df='df -h'
alias du='du -h'

# Process management (pairs well with the ps command)
alias psg='ps aux | grep'

# Network
alias ports='netstat -tulanp'
alias myip='curl ifconfig.me'

The hg alias is particularly useful. Pair it with the grep command to search your command history fast. Forgot that complicated find command you ran last week? hg find brings it back.

Safety Aliases (The Ones That Saved My Data)

I have to tell you about the time I ran rm -rf in the wrong directory. It was 2011. I was cleaning up some temp files on my homelab server. One wrong keystroke and I started deleting my home directory. I killed the terminal fast, but the damage was done.

That experience taught me the value of safety aliases:

alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'

The -i flag prompts for confirmation before destructive actions. Now rm asks before deleting. Yes, it’s slightly annoying. It’s also saved my data multiple times.

Warning: These aliases only protect you in interactive shells. Scripts use the actual commands. When writing automation scripts, always use explicit paths and test thoroughly before running on important data.

Git Workflow Shortcuts

Git commands get repetitive. These aliases speed up my daily workflow:

alias gs='git status'
alias ga='git add'
alias gaa='git add --all'
alias gc='git commit -m'
alias gp='git push'
alias gl='git pull'
alias gd='git diff'
alias glog='git log --oneline --graph --decorate'

The glog alias gives a beautiful visual history of your branches. I use it constantly when working on open source projects.

Managing and Removing Aliases

Sometimes aliases need to go. Maybe you’re testing or the shortcut conflicts with something else.

Listing All Active Aliases

View every alias in your current session:

alias

For a specific alias, include the name:

alias ll

This shows exactly what the alias expands to. Useful when debugging unexpected behavior.

Removing a Specific Alias with unalias

Remove a single alias:

unalias ll

Remove all aliases at once:

unalias -a

These changes are temporary. The aliases return next time you source your configuration files or open a new terminal.

Bypassing an Alias When You Need the Original Command

Sometimes you need the actual command, not your aliased version. Two methods work:

Method 1: Backslash prefix

\ls

Method 2: The command keyword

command ls

Both run the real ls binary, ignoring any aliases. I use the backslash method most often. It’s faster to type.

This matters when your safety alias with -i gets annoying. Want to delete fifty files without confirming each one? Use \rm carefully.

Alias Security and Best Practices

Aliases can create security problems if you’re not careful. This matters especially in shared environments.

Never Override Critical System Commands

You can alias ls to do anything. That’s powerful and dangerous. In shared systems, a malicious user could create aliases that hide activity.

“If a user creates a shell alias that alters the behavior of a commonly used command, it can lead to confusion and potential security vulnerabilities. For example, a user could create an alias for the ls command that hides certain files or directories by default.” – EITCA Academy

Use the which command to verify what actually runs when you type a command. This helps identify if an alias or function is intercepting your input.

The Shared Environment Problem

On multi-user systems, aliases live in each user’s home directory. That’s normally fine. Problems arise when:

  • Someone modifies system-wide shell configs
  • Dotfiles get shared without review
  • Hidden files mask malicious aliases

Always review configuration files from external sources. That cool dotfiles repo you found? Read through it before sourcing anything.

Quote Your Commands Properly

Use single quotes around alias definitions unless you specifically need variable expansion:

# Single quotes - literal text
alias today='date +%Y-%m-%d'

# Double quotes - variables expand when alias is CREATED
alias home="cd $HOME"

With single quotes, variables expand when you run the alias. With double quotes, they expand when you define it. This distinction matters for dynamic commands.

When Aliases Aren’t Enough: Functions vs Aliases

Aliases have one big limitation: they can’t accept arguments in the middle of a command. Arguments always go at the end.

For example, this alias works:

alias findf='find . -name'
# Usage: findf "*.txt"

But you can’t create an alias where the argument goes before other options. That requires a bash function:

mkcd() {
    mkdir -p "$1" && cd "$1"
}

This function creates a directory and immediately enters it. The $1 represents the first argument you pass.

When to Use Functions Instead:

  • You need arguments in the middle of a command
  • You need conditional logic
  • You need to combine multiple commands with error handling
  • You need loops or variables

Functions go in the same configuration files as aliases. I keep mine in a separate ~/.bash_functions file that gets sourced from .bashrc. Once you’re comfortable with aliases, exploring functions is the natural next step.

Managing your shell configuration connects to broader system administration skills. When you’re ready to level up your Linux security, learning about SSH key generation and the ps command for process monitoring will round out your toolkit.

Frequently Asked Questions

Do aliases work in shell scripts?

Not by default. Bash disables alias expansion in non-interactive shells. Scripts should use full commands anyway for portability and clarity. If you really need aliases in a script, add shopt -s expand_aliases at the top.

Can I use spaces in alias names?

No. Alias names cannot contain spaces. Use underscores or hyphens instead: my_alias or my-alias.

Why isn’t my alias working?

Common causes: you forgot to source your configuration file, there’s a typo in the alias definition, or quotes are mismatched. Run alias to check if it’s actually defined.

Are there community collections of useful aliases?

Yes. Check out this curated collection of bash aliases for inspiration. Just review any aliases before adding them to your system.

Start Building Your Alias Collection

The alias command in Linux is one of those tools that seems simple but transforms how you work. Start with a few shortcuts for commands you type constantly. Add safety aliases for destructive operations. Organize everything in .bash_aliases with clear comments.

Your terminal should fit your workflow, not the other way around. Aliases are the first step toward a truly personalized command-line experience. Once you’ve mastered them, shell functions and more advanced Bash configuration open up even more possibilities.

Ready to keep leveling up your Linux skills? Check out our guides on the ls command for mastering directory listings, or learn to automate tasks with cron jobs for hands-free system maintenance.

author avatar
Alexa Velinxs
I'm Alexa Velinxs, a cryptocurrency trading expert passionate about demystifying digital assets for both beginners and seasoned investors. Through my writing, I share actionable strategies, market insights, and practical tips to help you navigate the crypto landscape with confidence. Let's explore the future of finance together.
Related Posts