How to Use the Linux alias Command to Create Custom Terminal Shortcuts

The Problem with Verbose Commands

Working in the Linux terminal is incredibly powerful, but it requires typing. A lot of typing. If you are a developer or a system administrator, you likely have specific, complex commands that you run dozens of times a day. For example, updating your system packages might require typing sudo apt update && sudo apt upgrade -y. Connecting to a specific remote server might require ssh -i ~/.ssh/production_key [email protected] -p 2222.

Typing these long strings out manually every single time is not only tedious, but it also increases the likelihood of a syntax error. Even if you use the “Up” arrow to search through your command history, finding that one specific command you ran three days ago is frustrating.

The solution is the Linux alias command. This built-in utility allows you to create custom, memorable abbreviations for long, complex commands. It effectively allows you to build your own personalized vocabulary for the Linux terminal.

Creating a Temporary Alias

Let’s start by creating a simple alias for the command we use to check the status of our firewall. Usually, this requires typing sudo ufw status verbose.

We can shorten this to a single, memorable word: firewall.

  1. Open your terminal.
  2. Type the following syntax: alias [name]="[command]"
  3. For our example, type: alias firewall="sudo ufw status verbose" and press Enter.

Now, type the word firewall and press Enter. The terminal will instantly expand your alias and execute the full sudo ufw status verbose command.

Crucial Note: This alias is temporary. If you close your terminal window or end your SSH session, the alias will be forgotten.

Creating Permanent Aliases

To make an alias permanent so it survives reboots and terminal closures, we must save it inside your shell configuration file. For most modern Linux distributions (and macOS), the default shell is Bash or Zsh.

Step 1: Open Your Configuration File

If you are using Bash, your configuration file is located in your home directory and is named .bashrc.

  1. Open the file using the nano text editor: nano ~/.bashrc

Step 2: Add Your Aliases

  1. Use your arrow keys to scroll all the way to the bottom of the file.
  2. It is good practice to leave a comment so you remember what you added. Type: # My Custom Aliases
  3. On the next line, type your alias exactly as you did before: alias update="sudo apt update && sudo apt upgrade -y"
  4. Add another one for navigating to a deep directory: alias weblog="cd /var/log/apache2/ && ls -lh"

Step 3: Save and Apply

  1. Press Ctrl + O (the letter O, not zero) to save the file in nano, then press Enter to confirm.
  2. Press Ctrl + X to exit nano.
  3. For the changes to take effect immediately, you must “reload” the configuration file. Type: source ~/.bashrc

Your custom commands, update and weblog, are now permanent additions to your terminal environment.

Common and Highly Useful Aliases

Here are a few popular aliases used by professional Linux administrators that you should consider adding to your .bashrc file:

1. The Safe Delete

The standard rm command deletes files instantly without asking. This alias forces it to prompt you for confirmation before deleting anything, preventing catastrophic mistakes.

alias rm="rm -i"

2. The Better Directory Listing

The standard ls command is boring. This alias forces it to use color coding, show file sizes in human-readable formats (MB instead of bytes), and list everything vertically.

alias ll="ls -lh --color=auto"

3. The Network Trouble-shooter

Finding your exact IP address often requires sifting through the verbose output of ip a. This alias filters the output to show just your actual IP.

alias myip="curl ifconfig.me"

Conclusion

The alias command is the fastest way to dramatically improve your speed in the Linux terminal. By taking five minutes to encode your most frequently used, complex commands into simple, memorable abbreviations within your .bashrc file, you turn the Linux command line into a personalized, highly efficient operating environment.

Leave a Reply

Your email address will not be published. Required fields are marked *

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.