How to create command shortcuts using aliases in Linux

[ad_1]
The word “alias” is reminiscent of spies, secret identities, and mysterious images. In fact, the alias is actually just “another name”. In Linux, aliases are used to create commands/shortcuts. In fact, it can be a long and cumbersome command with difficult switches (options) that are hard to remember.
In this tutorial, we will learn how to create our own aliases, which can be used every time a terminal window is opened. The best thing is that they are really easy to make.
For this tutorial, we are using Ubuntu 21.04, but this tutorial can use any version of Linux and raspberry pie.
What is the structure of Linux aliases?
An alias is a single command that can run more complex commands. Let us consider this simple example.
alias hi='echo "Hello World"'
We have the alias command itself, followed by the alias, which is the name of the command we wish to create. Then we use = assignment, and then specify the command in “”. In this example, we alias “hi” so that it prints “Hello World” to the terminal. Therefore, when we use an alias, it will actually run the command linked to it.
Creating an alias in the terminal is only a temporary measure. Once the terminal is closed, or we shut down the computer, the alias will be lost. In some ways, this is a good feature if we need to test a new alias or just need a short-term alias. But what if we want our alias to last forever? To do this, we need to edit a hidden file and add an alias that we can use every time we open the terminal or open the PC.
Edit .bashrc
The .bashrc file is hidden in our home directory, but this does not mean that we cannot edit the file.
1. Use your favorite text editor, Open .bashrc, Found in your home directory. If you cannot see the file, press CTRL + H to show hidden files.
2. Scroll to the bottom of the file and Add comment lineWe use it to quickly identify what the following code will do, in this case our alias.
#Bash Aliases
3. Reuse the “Hello World” example. Add this line to the end of the file.
alias hi='echo "Hello World"'
4. save document.
5. Open a new terminal window and Run command.
$ hi
Create new alias
Sometimes we need to list the contents of a directory, including all hidden files. The ls command has many parameters for displaying hidden files, formatting output, sorting by file size, etc.
We will create a new alias called “list”, which has parameters to display content using the following parameters.
–Human readable Make the output readable
– size Sort by size
– Classification Apply the indicator to the output, for example it shows a directory with /
-A sort of Show all files, even hide them.
1. Use your favorite text editor, Open .bashrc file.
2. Scroll to the bottom of the file. Edit “Hello World” example Match the following.
alias list="ls --human-readable --size --classify -a"
3. save document.
4. Open a new terminal window and Run command.
$ list
Sometimes we forget the correct switch to extract files from the .tar archive. Of course we can use the desktop, but sometimes we need to do this from the terminal, for example, if we use SSH to connect. This alias will help us eliminate the need to use the switch sjo, we only need to call the alias with the archive name.
1. Go to the last line in .bashrc.
2. Add this alias, xtar, it will use the -vf switch to extract the specified .tar archive, which will extract the files in the archive to the current directory.
alias xtar="tar -xvf"
3. save document, and Open a new terminal window.
4. Run the xtar command in the directory Using .tar archive, here we run the command in the directory containing Files.tar and extract the contents to the current directory.
$ xtar Files.tar
Search your command history using Linux aliases
Searching our command history usually requires pressing the up arrow a hundred times to find a command, usually a command with only a few characters. Using the history command we can list the entire command history, but usually we need to find a specific command. To this end, we can pipe the output of the history command to grep, which is a tool for finding 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, it will print all the commands we have used. Tube | Found by pressing SHIFT and . The pipeline sends the output to grep.
alias gistory='history | grep'
3. save document, and Open a new terminal window.
4. Run the gistory command In any directory and Specify some text What you are looking for. In our example, we look for ls.
$ gistory ls
As you can see in our example, we get the list every time the “ls” command is used and the options used with it.
These are just a few examples where you can use aliases to speed up and simplify command line usage in Linux. If you often do anything at the Linux command prompt, you can simplify it by creating custom aliases that are easy to remember.
[ad_2]