Key Takeaways
- You can use aliases both temporarily and more permanently by adding them to config files.
- Aliases can simplify commands you need to use frequently.
- With aliases, you can completely replace one command with a more powerful command without needing to memorize it.
The humble alias is a powerful ally when it comes to using the Linux terminal, but it’s one you could probably stand to rely on more. Let’s take a look at a few of the ways aliases can make your life easier when it comes to getting around the terminal.
You May Already Be Using Aliases Without Knowing
To start, you’re probably already using at least one alias without knowing it. If you ever type the la
command in the terminal to quickly list all of the files in a directory, for example, that’s a simple alias to ls -lAh
or something similar, depending on your distribution.
If you’re curious, it’s simple to see what aliases already exist in your terminal. Simply type the alias
command without any arguments for a list of your current aliases. Be warned: even on a relatively fresh install, this can be a long list.
Aliases Can Simplify Frequently Used Commands
The Linux command line is incredibly powerful, but even if you use certain commands all the time, it can be tough to remember which order certain options go in. Maybe you can never remember where the ‘x’ goes in the tar -xvf
command to uncompress a .tar.gz file. This is simple to address with an alias.
These types of aliases are the kind you likely want to persist, so you’ll set them in your shell’s configuration file. If you use the bash shell, this is located in ~/.bashrc
. For zsh, your configuration file will be in ~/.zshrc
. If you aren’t sure which shell you have, it’s likely bash.
To set an alias for the tar command above, you’d put the following in your shell configuration file:
alias uncompress="tar -xzf"
Then, source the file by running source ~/.bashrc
or source ~/.zshrc
and try out your new uncompress
alias.
It’s worth pointing out here that you need to be careful with naming aliases. Your text editor or shell won’t stop you from naming your alias the same as some other important command, which could potentially cause problems.
To check if an alias you’re thinking of using already exists, you can run the following:
which command>
If you get
, or a similar response, the alias is safe to use.
Temporary Aliases Can Save You Time
While you’ll want many of the aliases you create to be permanent, you probably don’t want them all to stick around in perpetuity. Whether you’re temporarily setting up an environment to compile a piece of software or handling a few system administration tasks, temporary aliases can simplify things.
Setting a temporary alias is easy. Instead of writing it in a shell config file, you just create the alias right in your shell, as you would any other command.
alias awesome="cd /etc/xdg/awesome/"
Then instead of having to type the long path name each time, simply type awesome
to switch to that directory. This could also be useful if you need to connect to several remote servers, as you could have an alias for an ssh connection to each address, for example.
Once you’re done using this alias, you don’t need to worry about logging out of your shell just to get rid of it. Fortunately, there’s a handy command you can use to stop using any alias:
unalias command>
You can also use this in your shell config in case you want to revert a system-level alias.
Aliases Can Make Risky Commands Safer
One of the most powerful uses of aliases is actually to act as guardrails around powerful commands that could wreak havoc on your system. While you may not these for yourself, they’re indispensable if you’re trying to teach someone the ways of the command line without scaring them off.
For example, adding the -i
flag to the rm
command, or even commands like mv
can prevent files from accidental deletion. All the flag does is make the command interactive, so it will prompt the user before actually carrying out the action. For example, add the below to ~/.bashrc
:
alias rm="rm -i"
alias mv="mv -i"
Now those commands are slightly safer. Of course, you can also set these commands temporarily as we did above. If you’re dealing with sensitive files and want to ensure you don’t accidentally delete anything, setting these for a short time is a great way to play it safe.
You Can Use Aliases to Group Commands Together
There are some commands that you always run together. Some of these might be common between multiple people, like running sudo update && sudo upgrade
. Others might be more common to your own personal workflow, like syncing your mail and then running a tool like notmuch on it by running mbsync
.
To simplify the first, you could add something like the following to your ~/.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 together. That said, you likely want to avoid placing too much functionality inside of a single alias, as you don’t want to kick off a chain of commands that will tie up your terminal for 15 minutes with the wrong keystroke.
You Can Replace Commands Entirely With Aliases
While the tried and true commands that you’ve relied on in the terminal until now are just that, it’s not like they can’t be improved. Surely, with all the technical advances that have come along, someone has come up with a modern replacement for the ls
command by now, right?
The answer, it turns out, is yet. First there was the exa
command, which is now unmaintained and has been superseded by eza. This command operates as a drop-in replacement for the ls
command, albeit one with several more features on top.
You can install the two commands side-by-side if you wish, running eza
only when you feel like it. That said, most people who use the tool install it, then alias it to the old ls
command like so:
alias ls=eza
Now the ls
command will work how you’ve always used it, but with extra options. For example, the old ls
never showed you info on Git repositories, but now it can.
This isn’t the only command that you can alias this way, of course. If you’re a frequent Neovim user, you can alias the vim
command to nvim
, though in this case it may just be better to set your environment variables accordingly.