What Is Alias In Linux? How To Use And Create Permanent Aliases?

Replace long commands with your own fancy short name.

Share on twitter
Tweet
Share on whatsapp
WhatsApp
Share on facebook
Share

Typing texts and memorizing commands is one of the downsides of being a command-line enthusiast. And if you need to type and remember the same long command, it might reduce your productivity in a terminal.

What if you can replace a long command with your own fancy short name, or group multiple commands? Yes, alias is a method that can help you use the command line efficiently.

What Does Alias Mean In Linux?

Alias is a substitute name that refers to another command or a group of commands. It helps in creating a shortcut string for commands that are long to type or memorize.

For example, if you want to sync your local directory with your remote directory, you can use the below command.

$ rsync -a <path-to-local-directory> [email protected]:<destination_directory>

But if you perform directory synchronization frequently, it might become tiring for you to type this long command (unless you’re using a shell with auto-suggest features).

So, to make the regular sync task easy, what you can do is create an alias for rsync command.

$ alias remote="rsync -a <dir-path> [email protected]:<dir-path>"

And next time you want to transfer and sync your local content to a remote system, you can type “remote” in your terminal.

Based on the availability, alias is of two types: Temporary and Permanent. Temporary alias is available only for the current terminal session. Once you close your terminal, you can no longer use your custom aliases.

On the contrary, if you create a permanent alias in Linux, you can immediately use it in a new session or even after reboot.

How To Create And Use Alias In Linux?

Alias is a command you can use to create new aliases using alias new-name=value syntax. No gap between name and value.

For instace, if you like exa utility for listing files but still wants to use ls command, you can make “ls” alias of “exa” and print exa output using ls command.

$ alias ls='exa -lh'
$ ls
ls alias
ls alias

Once you create a bunch of aliases, you can also check or list it by simply running alias command:

$ alias
List all aliases

Later, if you want to delete or unset any of your alias, you can use unalias command with alias name passed as an argument.

$ unalias ls

You can also remove all aliases using -a option to unalias command.

$ unalias -a

How To Create Permanent Aliases In Linux?

Do you want to use your aliases even after closing the terminal and rebooting to system? If yes, you need to create a permanent alias.

To do that, you need to put your alias in the ~/.bashrc file by either opening it in a editor or by running commands:

$ echo "alias up='sudo apt update && sudo apt upgrade'" >> ~/.bashrc
$ source ~/.bashrc

If you’re using shell other than bash, you might need to put aliases in the respective shell config file such as ~/.zshrc for ZSH and ~/.config/fish/config.fish for Fish shell.

Store Aliases In A Separate File

If you use large number of aliases, it’s always better to have a separate file for it. You can create bash_aliases dot file in your home directory and put all aliases here.

$ touch ~/.bash_aliases
# add aliases to the above file
bash_aliases
bash_aliases

Once you add alias to “bash_aliases” file, you also need to add below code in the ~/.bashrc file.

if [ -e ~/.bash_aliases ]; then
source ~/.bash_aliases
fi
Sarvottam Kumar

Sarvottam Kumar

Sarvottam Kumar is a software engineer by profession with interest and experience in Blockchain, Angular, React and Flutter. He loves to explore the nuts and bolts of Linux and share his experience and insights of Linux and open source on the web/various prestigious portals.

New In Geek

Scroll to Top