Linux

Linux Alias Command with Example

Introduction

Creating custom shortcuts or abbreviations for longer commands or command sequences can be easily done using the alias command in Linux. These aliases can make command-line tasks simpler, reduce typing, and boost productivity. This guide provides an introduction to the alias command and includes examples to demonstrate its usage. 

However, it is important to be cautious when using aliases, especially if they overwrite existing command names or become too complex. It’s worth noting that aliases are specific to the shell session in which they are defined unless they are added to the shell configuration files for persistence across sessions.

What is An Alias in Linux?

What is An Alias in Linux?

In the world of Linux, aliasing can be a game-changer. This powerful feature streamlines command-line interactions, making it easier and more efficient to execute repetitive or complex commands. Linux users can benefit greatly from the convenience and speed that aliasing provides, allowing them to work more productively with fewer keystrokes and less frustration.

Linux Alias Syntax

The basic syntax for creating an alias is alias new_command=’original_command’. For instance:

alias ll='ls -l'
Linux Alias Syntax

This creates an alias ll for the ls -l command.

Linux Alias Syntax

Create Aliases in Linux

There are two types of aliases to create in Linux: temporary ones can be added using the alias command, while permanent ones require editing system files.

Create a Temporary Alias in Linux

Create a Temporary Alias in Linux

 In Linux, you can create a temporary alias directly in the terminal session without making it persist across sessions.  Use the alias command directly in the terminal to create a temporary alias that will only exist for the duration of your current shell session.

Example:

Let’s create a temporary alias lt for ls -lt (listing files by modification time):

alias lt='ls -lt'
Create a Temporary Alias in Linux

To create a temporary alias in the current terminal session, you can use the following command: “alias lt=’ls -lt'”. This will create an alias named “lt” that executes the “ls -lt” command whenever you enter “lt”. However, it’s important to note that temporary aliases created in this manner are only available within the current shell session. Once you close the terminal or start a new session, these aliases will no longer be available.

Create a Permanent Alias in Linux

Create a Permanent Alias in Linux

To permanently create an alias, you must add it to your shell configuration file. Depending on the shell you’re using, use:

Bash shell: ~/.bashrc

Zsh shell: ~/.zshrc

Fish shell: ~/.config/fish/config.fish

Steps to Create a Permanent Alias in Bash:

Open the Bash Configuration File:

For most Linux distributions, the Bash configuration file is located at ~/.bashrc. It can be edited using text editors such as nano, vim, or gedit.

nano ~/.bashrc

Add the Alias:

In the opened file, add your alias in the format alias new_command=’original_command’.

For instance, let’s create a permanent alias ll for ls -l:

alias ll='ls -l'
Steps to Create a Permanent Alias in Bash:

Save the Changes:

In Nano, you can save changes with Ctrl + X, Y, and Enter. Follow specific instructions for other editors.

Apply the Changes:

After saving the “.bashrc” file, you need to either restart your terminal or apply the changes to the current session using the source command.

source ~/.bashrc
Steps to Create a Permanent Alias in Bash:

Verify the Alias:

Now, the “ll” alias (or any other aliases you define in ~/.bashrc) will be available in all new terminal sessions.

List All Aliases in Linux

List All Aliases in Linux

To view a list of all the aliases defined in Linux, you can use the ‘alias’ command without providing any arguments. To do this, open the terminal and type ‘alias’, followed by the Enter key. The command will then display a list of all the currently defined aliases along with their respective definitions. Here is an example of how you can use the ‘alias’ command:

Alias

When you execute the “alias” command in the terminal, it will display a list of all the aliases defined in the current shell session. Each line will show the name of the alias, followed by its corresponding command or definition.

Remove Aliases in Linux

Remove Aliases in Linux

To delete an alias in Linux, use the unalias command followed by the alias name. The syntax is as follows:

unalias alias_name
Remove Aliases in Linux

To remove an alias, you need to replace alias_name with the actual name of the alias that you want to remove. For instance, if you wish to remove an alias called “lt,” you would use this command. Once the command is executed, the specified alias will be deleted, and you won’t be able to use it again in that particular shell session.

Also Read: How to List Users and Groups in Linux? {4 Easy Ways}

Conclusion

Aliases in Linux are a useful feature that allows users to create customized shortcuts for commands. This enhances productivity and makes the system more user-friendly. 

Temporary aliases are session-specific, meaning they are only effective for the duration of the current session. They can be created using the alias command directly in the terminal. 

Permanent aliases, on the other hand, can be established by adding them to shell configuration files such as ~/.bashrc for Bash users. When creating aliases, it is important to use caution to avoid overwriting existing command names or creating overly complex aliases. The unalias command can be used to remove aliases, which provides flexibility in managing defined shortcuts. 

Overall, aliases streamline command-line interactions, offering convenience and efficiency to Linux users. The alias command in Linux empowers users to create customized shortcuts for commands, significantly enhancing productivity and ease of use. Temporary aliases are session-specific and created using the alias command directly in the terminal. Meanwhile, permanent aliases can be established by adding them to shell configuration files like ~/.bashrc for Bash.

Arpit Saini

He is the Chief Technology Officer at Hostbillo Hosting Solution and also follows a passion to break complex tech topics into practical and easy-to-understand articles. He loves to write about Web Hosting, Software, Virtualization, Cloud Computing, and much more.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

[sc name="footer"][/sc]