How to Create Command Shortcuts with Aliases in Linux
The name’s Linux, GNU Linux.
The word “alias” conjures up images of espionage, secret identities and mystery. In reality an alias is really just “another name for.” In Linux, an alias is used to create a command / shortcut that in reality can be a long and unwieldy command with difficult switches (options) that are difficult to remember.
In this tutorial we will learn how to create our own aliases that will be ready for use every time we open a terminal window. And the best thing is that they are really easy to make.
For this tutorial we are using Ubuntu 21.04, but the tutorial can be followed with any version of Linux and also the Raspberry Pi.
What is the structure of a Linux alias?
An alias is a single command that can run a more complex command. Let's consider this simple example.
alias hi='echo "Hello World"'
We have the alias command itself followed by the alias, the name of the command that we wish to create. We then assign using = and then specify the command inside of ‘ ‘. In this example we are aliasing “hi” so that it prints “Hello World” to the terminal. So when we use the alias it really runs the command which it is linked to.
Creating aliases in the terminal is but a temporary measure. Once the terminal is closed, or we turn off the computer, the alias is lost. In some ways this is a good feature, should we need to test a new alias or only need an alias for a short amount of time. But what if we want to make our aliases last forever? For that, we need to edit a hidden file, adding our aliases which will be ready for use each time we open a terminal or turn on the PC.
Editing .bashrc
The .bashrc file is hidden in our home directory, but that doesn’t mean we cannot edit the file.
Stay On the Cutting Edge: Get the Tom's Hardware Newsletter
Get Tom's Hardware's best news and in-depth reviews, straight to your inbox.
1. Using your preferred text editor, open .bashrc, found in your home directory. If you cannot see the file press CTRL + H to reveal hidden files.
2. Scroll to the bottom of the file and add a comment line. We use this to quickly identify what the code below it will do, in this case our aliases.
#Bash Aliases
3. Reuse the “Hello World” example. Add this line to the end of the file.
alias hi='echo "Hello World"'
4. Save the file.
5. Open a new terminal window and run the command.
$ hi
Creating new aliases
Sometimes we need to list the contents of a directory, including any hidden files. The ls command has many arguments, to show hidden files, format the output, sort by file size etc.
We will create a new alias called “list” which will have arguments to show contents using the following parameters.
--human-readable Make the output human readable
--size Sort by size
--classify Apply an indicator to to the output, for example it shows directories with a /
-a Show all files, even hidden.
1. Using your preferred text editor, open the .bashrc file.
2. Scroll to the bottom of the file. Edit the “Hello World” example to match the following.
alias list='ls --human-readable --size --classify -a'
3. Save the file.
4. Open a new terminal window and run the command.
$ list
Create a Linux Alias to Extract the Files
Sometimes we forget the correct switches to extract a file from a .tar archive. Sure we can use the desktop, but there will come a time where we need to do it from the terminal, for example, if we are using an SSH connection. This alias will help us by removing the need to use the switches sjo we just call the alias along with the name of the archive.
1. Go to the last line in your .bashrc.
2. Add this alias, xtar, which will extract a specified .tar archive using the -vf switch which will extract the files in an archive to the current directory.
alias xtar='tar -xvf'
3. Save the file, and open a new terminal window.
4. Run the xtar command in a directory with a .tar archive, here we ran the command in a directory which contained Files.tar, and extracted the contents to the current directory.
$ xtar Files.tar
Use a Linux Alias to Search Your Command History
Searching through our command history usually entails pressing the up arrow a hundred times to find a command, often a command with only a few characters. Using the history command we can list the entire command history but often we need to find a specific command. For this we can pipe the output of the history command into a grep, a tool which looks for expressions / words in the output. This alias, history, will do just that; all we need is the alias and the word to search for.
1. Go to the last line in your .bashrc.
2. Add this alias, gistory, which will print all of the commands that we have used. The pipe | is found by pressing SHIFT and \. The pipe sends the output to grep.
alias gistory='history | grep'
3. Save the file, and open a new terminal window.
4. Run the gistory command in any directory and specify some text that you are looking for. In our example we looked for ls.
$ gistory ls
As you can see in our example, we got a list of every time we used the “ls” command and the options we used with it.
These are just a few examples of ways you can use aliases to speed up and simplify your command line use in Linux. If there’s anything you do frequently at the Linux command prompt, you can simplify it by creating custom aliases that you can easily remember.
Les Pounder is an associate editor at Tom's Hardware. He is a creative technologist and for seven years has created projects to educate and inspire minds both young and old. He has worked with the Raspberry Pi Foundation to write and deliver their teacher training program "Picademy".