Why you should use aliases in the Linux terminal

Why you should use aliases in the Linux terminal

  • You can use aliases both temporarily and permanently by adding them to configuration files.

  • Aliases can simplify commands that you need to use often.

  • Aliases allow you to completely replace one command with a more powerful command without having to remember it.

The humble alias is a powerful ally when it comes to using the Linux terminal, but it’s an ally you can probably rely on more. Let’s look at some of the ways aliases can make your life easier when it comes to navigating the terminal.

You may already be using aliases without knowing it

For starters, you’re probably already using at least one alias without even knowing it. If you ever have the la command in the terminal to quickly list all files in a folder, for example, that’s a simple alias for ls -lAh or something similar depending on your distribution.

If you’re curious, you can easily see which aliases already exist in your terminal. Simply type the alias command without arguments for a list of your current aliases. Be warned: even for a relatively new installation, this can be a long list.

Screenshot of aliases on Debian 12 bookworm with oh-my-zsh.Screenshot of aliases on Debian 12 bookworm with oh-my-zsh.

Screenshot of aliases on Debian 12 bookworm with oh-my-zsh.

Aliases can simplify commonly used commands

The The Linux command line is incredibly powerfulbut even if you use certain commands all the time, it can be difficult to remember the order in which certain options appear. You may never remember where the x is in the text. tar -xvf command to decompress a .tar.gz file. This is easy to solve with an alias.

These kinds of aliases are the kind you probably want to keep, so you set them up in your shells configuration file. If you are using the bash shell, it will be located in ~/.bashrc. For zsh, your configuration file will be there ~/.zshrc. If you’re not sure which shell you have, it’s probably a bash.

To set an alias for the tar command above, put the following in your shell configuration file:

alias uncompress=“tar -xzf”

Then source the file by running it source ~/.bashrc or source ~/.zshrc and try out your new one uncompress alias.

It’s worth pointing out that you should be careful when naming aliases. Your text editor or shell won’t stop you from giving your alias the same name as another important command, which could potentially cause problems.

To check if an alias you want to use already exists, you can do the following:

which <command>

If you get not foundor a similar answer, the alias is safe to use.

Temporary aliases can save you time

While you want many of the aliases you create to be permanent, you probably don’t want them all to last forever. Whether you’re temporarily setting up an environment to compile a piece of software or performing some system administration tasks, temporary aliases can simplify things.

Setting up a temporary alias is easy. Instead of writing it in a shell configuration file, you simply create the alias in your shell, just like any other command.

alias awesome=“cd /etc/xdg/awesome/”

Instead of having to type the long path name every time, you simply type awesome to go to that folder. This can also be useful if you need to connect to multiple remote servers, because for example you can have an alias for an ssh connection to each address.

Once you’re done using this alias, you won’t have to worry about logging out of your shell to get rid of it. Luckily, there’s a handy command you can use to stop using an alias:

unalias <command>

You can also use this in your shell configuration in case you want to restore a system level alias.

Screenshot showing temporary aliases in the Linux terminal.Screenshot showing temporary aliases in the Linux terminal.

Screenshot showing temporary aliases in the Linux terminal.

Aliases can make risky assignments safer

In fact, one of the most powerful uses of aliases is to act as a guardrail around powerful commands that can wreak havoc on your system. While you may not have these for yourself, they are essential if you’re trying to teach someone the ways of the command line without scaring them off.

For example, by the -i flag to the rm command, or even commands like mv can prevent files from being accidentally deleted. All the flag does is make the command interactive, so the user gets a warning before the action is actually executed. For example, add the following ~/.bashrc:

alias rm=“rm -i”
alias mv=“mv -i”

Now those commands are a little safer. Of course, you can also set these commands temporarily, as we did above. If you’re dealing with sensitive files and want to make sure you don’t accidentally delete anything, setting these files for a short period of time is a good way to play it safe.

You can use aliases to group commands together

There are some assignments that you always perform together. Some of these can affect more than one person, such as running sudo update && sudo upgrade. Others may be more common in your own personal workflow, such as syncing your email and then running a tool like not much on it by mbsync && notmuch new .

To simplify the former, you could add something like the following to your file ~/.bashrc or ~/.zshrc:

alias up2date=”sudo apt update && sudo apt upgrade

For the other command you could use something like

alias mail_sync=“mbsync && notmuch new”

Of course you can group more than two commands. That said, you probably want to avoid putting too much functionality within a single alias, because you don’t want to launch a series of commands that connect your terminal for 15 minutes with the wrong keystroke.

You can completely replace commands with aliases

While the tried and true commands you’ve relied on in the terminal so far are just that, it’s not like they can’t be improved. Surely, with all the technical developments that have occurred, someone has a modern replacement for the ls order now, right?

The answer remains to be seen. First there was the exa command, which is now deprecated and has been replaced by eza. This command works as a drop-in replacement for the ls command, albeit one with a few extra features on top.

You can install the two commands side by side while they are running if you wish eza only if you feel like it. That said, most people who use the utility install it and then alias it to the old one ls command as follows:

alias ls=eza

Now the ls The command works as you have always used it, but with additional options. The old one for example ls never showed you information about Git repositories, but now you can.

Screenshot of the eza command with an alias for the ls command.Screenshot of the eza command with an alias for the ls command.

Screenshot of the eza command with an alias for the ls command.

Of course, this isn’t the only command you can alias in this way. If you are a frequent Neovim user, you can use an alias for the vim order nvimalthough in this case it might be better to do so set your environment variables accordingly.

By admin

Leave a Reply

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