facebook youtube pinterest twitter reddit whatsapp instagram

What is alias and How Do I Use It in Linux?

The alias command can be used to represent a longer command or series of commands in Linux. Aliases allow you to type a shorter and easier-to-remember name instead of the full command, which can save time and make it easier to use the command in the terminal.

To create an alias in Linux, you can use the alias command followed by the short name that you want to use for the alias and the command or series of commands that it should represent.

Using alias

For example, if you wanted to create an alias for the ls -l command, which lists the contents of a directory in long format, you could use the following alias command:

alias ll='ls -l'

This creates an alias called ll that represents the ls -l command. Now, whenever you type ll in the terminal and press Enter, it will be replaced with the ls -l command and the command will be executed.

You can create as many aliases as you want, and you can use them to represent any command or series of commands that you use frequently in the terminal.

To see a list of all the aliases that have been defined on your system, you can use the alias command without any options or arguments.

To remove an alias that you have created, you can use the unalias command followed by the name of the alias that you want to remove.

Removing alias

For example, to remove the ll alias that we created earlier, you could use the following unalias command:

unalias ll

This will remove the ll alias from your system, and you will no longer be able to use it in the terminal.

Persisting alias Changes Across Sessions

To make an alias persist across sessions in Linux, you will need to add it to your shell's configuration file.

The configuration file for your shell is a file that is executed whenever you open a new terminal or shell session. By adding your alias to this file, it will be automatically defined whenever you open a new shell session, so you won't have to define it manually every time you want to use it.

The name and location of the configuration file for your shell will depend on the type of shell that you are using. Some common shells and their corresponding configuration files are:

  • Bash: ~/.bashrc or ~/.bash_profile
  • Zsh: ~/.zshrc
  • Fish: ~/.config/fish/config.fish

To add an alias to your shell's configuration file, you can use a text editor to open the appropriate file and add the alias command for your alias to the end of the file.

For example, if you were using Bash and wanted to create an alias for the ls -l command, you could add the following line to your ~/.bashrc file:

alias ll='ls -l'

Save the file and close the text editor, and the next time you open a new shell session, the ll alias will be defined and ready to use.

Keep in mind that the changes you make to your shell's configuration file will only apply to new shell sessions.

If you have already opened a shell session and want to use the alias that you just defined, you will need to source the configuration file to load the changes into your current shell session. You can do this by running the following command:

source ~/.bashrc

Replace ~/.bashrc with the path to your shell's configuration file if it is different. This will load the changes that you made to the configuration file, and the ll alias will be available for use in your current shell session.


Related Post(s)

  • The ac Command in Unix & Linux

    The ac command is a Unix utility that is used to display statistics about users' connect time. It displays the total connect time for all users or for a specific user. The connect time is the amount o

  • What is Linux adduser and addgroup commands

    The adduser and addgroup commands are Linux utilities that are used to create new user and group accounts on a Linux system. The adduser command is used to create a new user account, while the addgrou

  • What is unlias & How Do I Use It in Linux?

    The unalias command is a Linux utility that is used to remove an alias that has been defined on the system. To use the unalias command, you simply need to specify the name of the alias that you want

  • What is apt-get and How Do I Use It?

    apt-get is a command line tool used for managing packages on a Debian-based Linux system. It is a part of the Advanced Packaging Tool (apt) system, which is used to manage the installation, removal, a

  • What is ar Command in Linux and How Do I Use It?

    ar is a command line tool used for creating, modifying, and extracting files from archives. An archive is a single file that contains multiple files, often in a compressed format. GNU ar is part of the GNU Compiler Collection (GCC) and is commonly us

  • What is as Command in Linux and How Do I Use It?

    The as command in Linux is a tool for assembling (compiling) assembly language source code into machine code that can be executed on a computer. Assembly language is a low-level programming language