How to create command shortcuts with aliases in Linux



The word “alias” conjures up images of espionage, secret identities and mystery. In reality, an alias is really just an “alternate name for”. In Linux, an alias is used to create a command / shortcut which in reality can be a long and heavy command with hard to remember switches (options).

In this tutorial, we’ll learn how to create our own aliases that will be ready to use whenever we open a terminal window. And the best thing is that they are really easy to do.

For this tutorial we are using Ubuntu 21.04, but the tutorial can be followed with any version of Linux and also the Raspberry pie.

What is the structure of a Linux alias?

(Image credit: Tom’s Hardware)

An alias is a single command that can execute a more complex command. Consider this simple example.

alias hi='echo "Hello World"'

We have the alias command itself followed by the alias, the name of the command we want to create. We then assign using =, then specify the command inside ”. In this example, we create an alias “hi” so that it prints “Hello World” to the terminal. So when we use the alias, it actually executes the command it is linked to.

Creating aliases in the terminal is only a temporary measure. Once the terminal is closed, or the computer is turned off, the alias is lost. In some ways, this is a good feature, if we need to test a new alias, or if we only need an alias for a short time. But what if we want our aliases to last forever? For this we have to edit a hidden file, adding our aliases which will be ready to use every time we open a terminal or turn on the PC.

Modification of .bashrc

(Image credit: Tom’s Hardware)

The .bashrc file is hidden in our home directory, but that doesn’t mean we can’t edit the file.

1. Using your favorite text editor, open .bashrc, found in your home directory. If you can’t see the file, press CTRL + H to reveal the hidden files.

2. Scroll down to the bottom of the file and add a comment line. We use it to quickly identify what the code below will do, in this case our aliases.

#Bash Aliases

3. Reuse the “Hello World” example. Add this line at the end of the file.

alias hi='echo "Hello World"'

4. Save file.

5. Open a new terminal window and execute command.

$ hi

(Image credit: Tom’s Hardware)

Creation of new aliases

Sometimes we need to list the contents of a directory including 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 display the content using the following parameters.

– human readable Make the output human readable

–Cut Sort by size

–to classify Apply a flag to the output, for example it displays directories with a /

-a Show all files, even hidden ones.

1. Using your favorite text editor, open the .bashrc file.

2. Scroll to the bottom of the file. Modify the “Hello World” example to match the following.

alias list="ls --human-readable --size --classify -a"

3. Save file.

4. Open a new terminal window and execute command.

$ list

Sometimes we forget the correct switches to extract a file from a .tar archive. Of course, we can use the desktop, but there will come a time when we will have 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 sjo switches which we just call the alias with the archive name.

1. Go to the last line of your .bashrc.

2. Add this alias, xtar, which will extract a .tar archive specified using the -vf switch which will extract files from an archive to the current directory.

alias xtar="tar -xvf"

3. Save file, and open a new terminal window.

4. Run the xtar command in a directory with a .tar archive, here we have executed the command in a directory containing Files.tar and extracted the contents to the current directory.

$ xtar Files.tar

(Image credit: Tom’s Hardware)

Use a Linux alias to find your command history

Searching our order history typically involves pressing the up arrow a hundred times to find an order, often an order 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. To do this, we can direct the output of the history command into a grep, a tool that searches for phrases / words in the output. This alias, the story, 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 the commands we have used. The pipe | is found by pressing SHIFT and . The pipe sends the output to grep.

alias gistory='history | grep'

3. Save file, and open a new terminal window.

4. Run the gistory command in any directory and specify text you are looking for. In our example, we searched for ls.

$ gistory ls

(Image credit: Tom’s Hardware)

As you can see in our example, we got a list of each 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 is something you do frequently at the Linux Command Prompt, you can simplify it by creating custom aliases that you will easily remember.


Leave A Reply

Your email address will not be published.