Linux Tactic

Streamline Your Linux Terminal Workflow with Aliases

Introduction to the Alias Command

Have you ever found yourself typing the same long command repeatedly in the terminal? Have you ever wished there was a way to save time and effort while using the command line?

If so, then the alias command is your solution. This Linux terminal command allows you to create a custom shortcut for a command or sequence of commands.

In the manual (man pages), the alias command is defined as a utility that allows you to assign a string to a command or a sequence of commands. The string can be used as a shorthand for the command or group of commands.

In this article, we will take an in-depth look at the alias command and its features.

The Alias Command

Before we dive into creating aliases, let’s first understand what the alias command is and how it works. An alias is simply a custom shortcut for a command or sequence of commands in the terminal.

It makes it easier to remember and execute frequently used commands.

The syntax for defining an alias is simple:

“`alias shortname=’command’“`

Here, ‘shortname’ is the custom shortcut or alias that you want to create, and ‘command’ is the original command or sequence of commands that you want to assign to it.

Creating an Alias

Now, let’s see how to create an alias. Let’s say you frequently use the tar command to compress or extract files.

Instead of typing the entire command each time, you can define an alias for it.

“`alias tp=’tar -xzvf’“`

Here, we have defined an alias ‘tp’ for the tar command with the ‘-xzvf’ options.

Now, instead of typing ‘tar -xzvf’, we can simply type ‘tp’.

Overwriting an Existing Command with an Alias

What happens if you define an alias that has the same name as an existing command? In this case, the alias will overwrite the original command.

For example, let’s say you define the ‘ls’ command as an alias for ‘ls -la’. Now, whenever you type ‘ls’ on the terminal, it will execute the ‘ls -la’ command.

Checking the Available Aliases

To check the available aliases on your system, simply type the ‘alias’ command in the terminal. It will display a list of all the aliases defined on your system.

Conclusion

In summary, the alias command is a powerful utility that can save you time and effort while using the command line. It allows you to create custom shortcuts for frequently used commands or sequences of commands.

With the simple syntax of ‘alias shortname=’command”, you can define an alias in no time. Just make sure you don’t overwrite any existing commands with your aliases.

Finally, don’t forget to check the available aliases with the ‘alias’ command.

3) Making Aliases Persistent

Creating an alias is a great way to save time and reduce typing efforts. But what if you had to redefine the aliases each time you started a new terminal window?

Here is where the concept of persistency comes into play. In order to make aliases available consistently across terminal sessions, one needs to create and manage the configuration files properly.

The ~/.bashrc file

The most commonly used configuration file for defining aliases is the ~/.bashrc file. This file is executed by the system whenever you start a bash session on your system, and any aliases defined in this file are loaded automatically.

To create an alias in ~/.bashrc, open the file in a text editor and add the desired alias at the end of the file. For example, to define ‘tp’ alias for the ‘tar -xzvf’ command, type the following at the end of the ~/.bashrc file:

“`alias tp=’tar -xzvf’“`

Once you have saved the file, every time you open a new terminal window, the alias will be available for use.

Creating a bash_aliases file

Another way to create aliases persistently is to use the ~/.bash_aliases file. This can be created manually using a command-line text editor or via the terminal by running the following command:

“`

touch ~/.bash_aliases

“`

This will create an empty file named .bash_aliases in your home directory.

Next, you can add as many aliases to this file as you wish. Open the file in a text editor and add the desired aliases, one per line.

Here’s an example:

“`

alias ll=’ls -l’

alias gs=’git status’

“`

Remember to save the file once you have added your aliases. You can now access these aliases in any terminal window in your system.

Restarting the Terminal for changes

Whenever you make any changes to the ~/.bashrc file or the ~/.bash_aliases file, you need to reload the configuration files or restart the terminal for the changes to take effect. To reload the configuration files, you can run the following command:

“`source ~/.bashrc“`

Alternatively, you can just close the terminal window and open a new one to apply the changes.

4) Undoing Aliases

At times, you might define an alias that you no longer need or create an alias that conflicts with an existing command. In such cases, you may want to undo or remove the alias you created earlier.

Temporarily Undoing an Alias

The most basic way to undo an alias temporarily is to use the ‘unalias’ command. This command removes the alias, making the original command available again.

Here’s an example:

“`

$ alias tp=’tar -xzvf’

$ unalias tp

“`

Now, when you type ‘tp’ in the terminal, it will not execute the ‘tar -xzvf’ command. Instead, you will receive an error message indicating that the command ‘tp’ does not exist.

Permanently Undoing an Alias

To permanently remove an alias, you will need to modify the configuration file in which you defined the alias. If you used the ~/.bash_aliases file, open the file in a text editor and delete the line containing the alias definition.

If you defined the alias in the ~/.bashrc file, open the file in a text editor and remove the line you added earlier.

Once you have made the necessary changes, save the file and reload the configuration file by restarting the terminal window or running the following command:

“`source ~/.bashrc“`

Conclusion

Creating aliases is an excellent way to simplify your work on the command line. By creating aliases persistently, you can save time and effort by reducing the number of keystrokes.

You can store aliases in the ~/.bashrc file or create a separate ~/.bash_aliases file and reload the config files as necessary. If you ever need to undo an alias, you can temporarily remove it using the ‘unalias’ command or permanently delete it from the configuration file.

In conclusion, aliases in the Linux terminal are a powerful tool that can save time and effort. The alias command allows you to create custom shortcuts for frequently used commands, which can be defined in either the ~/.bashrc file or ~/.bash_aliases file.

To make the aliases available persistently, one must reload the configuration file by restarting the terminal or running the appropriate command. If ever needed, an alias can be temporarily undone using the ‘unalias’ command or can be permanently removed by modifying the configuration file.

Overall, understanding how to create and manage aliases is an important skill for anyone working with the Linux terminal.

Popular Posts