Aliases in Linux: Creating Command Shortcuts

This post explains what aliases are in Linux and how to create them to save time at the command line. It covers temporary aliases, making them permanent in ~/.bashrc, practical examples, and how to remove them.

Aliases in Linux: Creating Command Shortcuts

If you find yourself typing the same long commands over and over, aliases are about to become your new best friend. An alias in Linux is simply a custom shortcut that maps a short, memorable name to a longer command. Think of it like creating your own keyboard shortcut, but for the terminal.

What Is an Alias?

An alias lets you define a word or abbreviation that expands into a full command when you type it. Instead of typing ls -lah --color=auto every single time you want a detailed directory listing, you can create an alias like ll and get the same result instantly.

Aliases are a shell feature, meaning they live inside your shell environment (most commonly bash or zsh). They are simple, powerful, and require zero programming knowledge to use.

Creating a Temporary Alias

The quickest way to create an alias is directly in your terminal using the alias command. The syntax is straightforward:

alias shortname='command you want to run'

For example, to create a shortcut for a detailed directory listing:

alias ll='ls -lah --color=auto'

Now just type ll and you will get a nicely formatted, human-readable file listing. You can verify your alias was created by typing:

alias ll

Which will output something like:

alias ll='ls -lah --color=auto'

To see all currently active aliases, just type alias with no arguments. You will likely see a list of aliases your system already defines by default.

The catch with temporary aliases: they disappear when you close your terminal session. To make them stick, you need to make them permanent.

Making Aliases Permanent

To keep your aliases across sessions, you need to add them to your shell's configuration file. For bash users, that file is ~/.bashrc. For zsh users, it is ~/.zshrc.

Open the file in a text editor:

nano ~/.bashrc

Scroll to the bottom and add your aliases, one per line:

# My custom aliases
alias ll='ls -lah --color=auto'
alias gs='git status'
alias ..='cd ..'
alias update='sudo apt update && sudo apt upgrade -y'

Save the file and then reload it so the changes take effect in your current session:

source ~/.bashrc

After this, your aliases will be available every time you open a new terminal.

Some Practical Alias Ideas

Here are a few genuinely useful aliases to get you started:

  • alias c='clear': clears the terminal with one letter
  • alias mkdir='mkdir -p': always creates parent directories automatically
  • alias rm='rm -i': prompts before deleting files, a great safety net for beginners
  • alias grep='grep --color=auto': highlights matches in search output
  • alias df='df -h': shows disk usage in a human-readable format

Removing an Alias

If you want to temporarily disable an alias for your current session, use the unalias command:

unalias ll

To remove it permanently, open your ~/.bashrc file again and delete the corresponding line, then reload with source ~/.bashrc.

A Quick Word of Caution

Be careful about aliasing existing commands in ways that change their default behavior unexpectedly, especially on shared systems. For example, aliasing ls to something that modifies its output might confuse other users or scripts that rely on default behavior. On your personal machine, though, go wild.

What's Next

Now that you know how to create aliases, the natural next step is exploring your shell's configuration files more deeply. Your ~/.bashrc is not just for aliases; it is where you can set environment variables, customize your prompt, and define shell functions. Functions are like more powerful aliases that can accept arguments and run multi-step logic. We will cover those in the next post in this series.