How to Create and Use Alias Command in Linux

Table of Content
    Add a header to begin generating the table of contents
    Learn How to create and use alias Command in Linux
      Add a header to begin generating the table of contents

      Many advanced operations in Linux require lengthy commands, increasing the risk of errors. To avoid typing long commands repeatedly, aliases can be created. Aliases allow users to customize commands by creating shortcuts that are efficient and easier to remember.

      In this tutorial, we’ll learn how to create and use the alias command in Linux.

      What is an alias Command

      The alias command allows users to create shortcuts for commands that may be lengthy or difficult to remember. By using an alias, you can replace a command with any string of characters of your choice.

      After creating an alias, you no longer need to type the entire command in the terminal. Instead, you can simply enter the shortcut name you set for the command. The system will then automatically recognize the alias and execute the corresponding command for you.

      Create alias Command in Linux

      There are two ways to make an alias. Now, we’ll discuss both methods for creating an alias command in Linux.

      Temporary Alias

      A temporary alias is created when you want to use it only for the current terminal session. The system’s memory will automatically remove the alias once you exit the session. Let’s create a temporary alias.

      First, open the terminal and use the following syntax to create a temporary alias for a command:

      $ alias shortname='command'

      Let’s break down the above syntax:

      • Shortname: The shortcut name you want to use instead of entering the full command.
      • Command: The command for which you desire to create an alias.

      For instance, we can create an alias for the sudo apt update command as uu for the current terminal session by running the following alias command:

      $ alias uu='sudo apt update'
      creating temporary alias for "sudo apt update" and using it as a temporary alias

      As we can see, after creating an alias, when we entered the shortcut name, the system automatically recognized the alias and executed the command. Thus, you’ve successfully created the temporary alias.

      Permanent Alias

      A permanent alias is created when you want the shortcut for long-term use. The system’s memory won’t remove it unless you delete it manually. To make an alias permanent, you’re required to include it in the configuration file of your shell. Follow the steps below to do so.

      First, open the shell configuration file by running the command mentioned below:

      $ nano ~/.bashrc

      Afterward, create an alias for your desired command by following the syntax of the alias command:

      alias shortname='command'

      Then, save the alias in the configuration file by pressing Ctrl + O, followed by Enter. Subsequently, press Ctrl + X to exit the editor:

      adding shortcut of "sudo apt update" to nano text editor as permanent alias

      Moving further, you need to source the file to apply the changes and use the alias. To do so, run the following command:

      $ source ~/.bashrc
      using the created permanent alias in terminal

      As we can observe, the permanent alias is working. Thus, the creation of the permanent alias was successful.

      Remove Alias

      Now, we’ll discuss how to remove temporary and permanent aliases. First, let’s go over the removal of a temporary alias. To do so, run the following command:

      $ unalias shortname

      In the above syntax, replace the shortname with the alias of the command you previously created.

      NOTE: This only removes the alias for the current session. The system’s memory will automatically clear the alias in any subsequent sessions.

      removing the permanent alias by using the aforementioned command "unalias".

      Next, we’ll discuss how to remove a permanent alias. To do so, simply delete the alias definition from the shell configuration file.:

      $ nano ~/.bashrc

      Now, remove the alias of the desired command from the file, then save the changes to apply them:

      $ source ~/.bashrc

      This way, you can successfully remove the permanent alias.

      Conclusion

      All in all, the alias command is an essential tool for Linux users, as it allows you to transform lengthy commands into shorter, more memorable ones. This helps reduce the risk of errors to almost zero.

      In this tutorial, we’ve discussed both temporary and permanent aliases, along with their implementation and removal. A temporary alias is created for use only in the current terminal session, whereas a permanent alias can be used long-term by adding it to the shell configuration file.

      Latest Post

      Contents
        Add a header to begin generating the table of contents
        Learn How to create and use alias Command in Linux
        Latest Post
        Do You Enjoy My Blog

        Supports from reader you like keeps this blog running. By a cup of coffee for me , its a affordable way to show your appreciation

        Previous Post
        ABOUT THE AUTHOR
        Maryam Nasir

        I'm currently pursuing my Bachelor's degree. Alongside my studies, I specialize in jotting down my knowledge about Linux by effectively and practically using my hobby of writing.

        Scroll to Top