Most of the users like to use shortcuts for running commands. There are many commands in Ubuntu that we need to execute regularly. It will be very helpful for us if we can run those common commands by typing shortcut commands. Using bash aliases, Ubuntu users can easily create shortcut commands of the large commands those are used frequently. Bash aliases not only make the task easier but also save the time of the users. The user can declare alias temporary or permanently. The temporary aliases can be used as long as the session of the user exists. If the user wants to use shortcut commands every time the session starts, then he or she has to create permanent alias by using ~/.bashrc and ~/.bash_profile files. This tutorial shows how you can create and use bash aliases in Ubuntu by using some examples.

Example-1: Temporary bash alias declaration

Declaring a temporary bash alias is a very simple task. alias command is used to create a shortcut of any command. For example, the ls -la command is a very commonly used command to display the list of all files and folders with permission. You can create the shortcut of this command by using the alias command. Suppose the shortcut command will be L. Run the following commands to create the shortcut of ls -la and test the command.

Check the output of the ls -ls command

Create alias command for ls -la

Test the shortcut of ls -ls

Output:


After executing the above commands, the output of the “L” command that has been created by the `alias` command and “ls -la” commands are the same.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/How-to-use-bash-aliases-1.png" data-lazy- height="478" src="data:image/svg xml,” width=”1299″>

If the user closes the session and starts a new session again, the alias command will not work.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/How-to-use-bash-aliases-2.png" data-lazy- height="216" src="data:image/svg xml,” width=”1207″>

Example-2: Permanent bash alias declaration

To solve the above problem, you can create permanent alias command. Suppose you want to create a shortcut of the “mkdir” command with the alias name “C” and use it permanently. Open ~/.bashrc file in any editor, add alias command in that file, save the file and run the `source` command to re-execute the file with added alias command.

Run the following command to open the ~/.bashrc file in nano editor.

Add the following `alias` command in the file and save it.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/How-to-use-bash-aliases-3.png" data-lazy- height="523" src="data:image/svg xml,” width=”1450″>

Re-execute the file to make the command active.

Run the following commands to test the shortcut command of “mkdir”.

Output:


The following output will appear after executing the above commands.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/How-to-use-bash-aliases-4.png" data-lazy- height="305" src="data:image/svg xml,” width=”1420″>

Example-3: Use of `alias` for `cd` command

The `cd` command is used to change the current working directory. The way to use the `alias` command for different types of `cd` commands is shown in this example. The `cd ..` command is used to set the directory one level up from the current directory. Run the following commands to create the alternative command of `cd ..` and test the created `alias` command.

$ alias p_dir=‘cd ..’


$ p_dir

Output:


The following output will appear after executing the above commands.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/How-to-use-bash-aliases-5.png" data-lazy- height="176" src="data:image/svg xml,” width=”1197″>

The `cd ../../` command is used to set the directory two levels up from the current directory. Run the following commands to create the alternative command of `cd ../../` and test the created `alias` command.

$ alias 2p_dir=‘cd ../../’


$ 2p_dir

Output:

The following output will appear after executing the above commands.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/How-to-use-bash-aliases-6.png" data-lazy- height="174" src="data:image/svg xml,” width=”1195″>

Example-4: Use of `alias` for `bc` command

The `bc` command is used for mathematical operations with fractional data. The way to create alternative command of the `bc` command has been shown in this example.

Run the following commands to create and test the `alias` command of the `bc` command.

$ alias cal=‘bc -l’


$ echo “scale=2; 37/2” | cal

Output:

The following output will appear after executing the above commands.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/How-to-use-bash-aliases-7.png" data-lazy- height="219" src="data:image/svg xml,” width=”1199″>

Example-5: Use of `alias` for root privilege

The `sudo -i` command is used to set the root privilege. The way to create the alternative command of the `sudo -i` command has been shown in this example.

Run the following commands to create and test the `alias` command of the `sudo -i` command.

$ alias admin=‘sudo -i’


$ admin

Output:

The following output will appear after executing the above commands.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/How-to-use-bash-aliases-8.png" data-lazy- height="214" src="data:image/svg xml,” width=”1195″>

Example-6: Use of `alias` to find the specific command from history

The `grep` command is mainly used to search the specific content in a file or text, and the `history` command is used to keep the history of the previously used commands. Sometimes it requires finding out the history of previously used specific commands by using `grep`. If this task needs multiple times, then creating the alias command to find the specific command from the history using `grep` is a good option. The use of the `alias` command to find the specific command from history using `grep` has shown in this tutorial.

Run the following commands to create and test the `alias` command to find the particular command from the history.

$ alias f_cmd=‘history|grep’


$ f_cmd cat


$ f_cmd pwd

Output:

The following output will appear after executing the above commands.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/How-to-use-bash-aliases-9.png" data-lazy- height="498" src="data:image/svg xml,” width=”1185″>

Example: Use of `alias` to count the total number of files of the current directory

There are many ways to count the total number of files of the current directory in bash. The simple way to count the total files of the current directory is to use `find` and `wc` commands. By creating an `alias` command to do this task makes the task easier.

Run the following command to create and test the `alias` command to count the total number of files of the current directory.

$ ls


$ alias totalFiles=‘find . -type f | wc -l’


$ totalFiles

Output:

The following output will appear after executing the above commands.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/How-to-use-bash-aliases-10.png" data-lazy- height="272" src="data:image/svg xml,” width=”1199″>

Example-3: Remove bash alias

The `unalias` command is used to remove the previously created alias command. After using this command, the alias will not work. So, if you think you don’t want the shortcut command anymore, then you can use the alias command to remove it permanently.

Run the following commands to check the use of the `unalias` command to remove the previously created shortcut command.

$ alias d=’date


$ d


$ unalias d


$ d

Output:

The following output will appear after executing the above commands.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/How-to-use-bash-aliases-11.png" data-lazy- height="303" src="data:image/svg xml,” width=”1198″>

Remove or comment on the line that is used for creating the `alias` command in the ~/.bashrc file and re-execute it for deleting the permanent alias.

Conclusion:

You can use the `alias` command for various purposes for creating a shortcut of the commands. This tutorial will help understand the basic use of the `alias` command so that the bash users can easily apply this command to create a shortcut of the regularly used commands.