opened image

Effective work in the terminal: how to create and use aliases in Linux

The alias command in Linux is an extremely useful and sometimes indispensable tool that helps users save time when entering frequently used commands. Instead of typing long commands or remembering complex syntax every time, you can create short and convenient aliases that significantly simplify work in the terminal.

In this article, we will discuss what the alias command is, how it works, how to create temporary and permanent aliases, manage them, and provide several useful examples for everyday work. Regardless of your skill level, you will be able to use aliases to enhance your productivity.

 

How to find the list of current aliases in Linux

 

Linux-based operating systems come with a number of pre-installed aliases that make using the command line more convenient. To find out which aliases already exist in your system, you can execute the command through the terminal:

alias

This command outputs a list of all created and existing aliases configured for your shell. An example output may include aliases such as:

​​​​​​​

alias ll='ls -alF'

alias la='ls -A'

alias l='ls -CF'


 

These aliases shorten standard commands and provide more convenient options for working with files and directories. For example, instead of typing ls -alF, you can simply type ll to display a list of files in the directory with details.

 

 

Creating temporary aliases in Linux

 

Temporary aliases are aliases that work only in the current terminal session. They are not saved when the terminal is closed and disappear after the session ends. This is convenient if you need to temporarily create an alias to simplify the execution of a command.

Creating a temporary alias is very simple. Use the following syntax:

alias shortName="command"

For example, to create a temporary alias for navigating to the directory /var/www/html:

alias wr="cd /var/www/html"


Now, to move to this directory, you just need to type wr instead of the full command cd /var/www/html. It is important to remember that this alias will disappear as soon as you close the terminal.

 

Creating permanent aliases in Linux

 

If you want an alias to persist between terminal sessions, you need to add it to your shell's configuration file. Depending on the shell you are using, the configuration files may vary, for example:

  • For Bash: ~/.bashrc
  • For ZSH: ~/.zshrc
  • For Fish: ~/.config/fish/config.fish

The process of creating a permanent alias is as follows:

  1. Open the configuration file in a text editor. For example, for Bash:

vim ~/.bashrc

  1. Add the new alias to the end of the file. Example:

alias home="ssh -i ~/.ssh/mykey.pem [email protected]"

alias ll="ls -alF"


 

  1. You need to save the changes and close the file. To apply the changes in the current session, execute the command:

source ~/.bashrc


 

Now the alias will be available in every new terminal you open.

 

Using aliases in the current session

 

After adding an alias to the shell configuration file, you can immediately apply the changes without restarting the terminal. To do this, use the command:

source ~/.bashrc

This command reloads the configuration file and applies the new aliases in the current session. Now you can use your aliases without needing to restart the terminal or log in again.

 

How to delete an alias in Linux

 

If you no longer need a specific alias, you can use the unalias command. This command removes aliases either temporarily or permanently, depending on how they were created.

To delete a specific alias:

unalias alias_name

Example of deleting the alias wr:

unalias wr


If you want to delete all aliases in the current session, use the command:

unalias -a


Note that if you delete an alias that was added to the configuration file (for example, in .bashrc), it will reappear after restarting the shell unless it is removed from the configuration file.

 

Useful examples of aliases for Linux

 

For convenience and increased productivity, you can create aliases for frequently used commands. Here are some useful examples:

  1. Alias for quickly navigating to the home directory:

alias home="cd ~"


Now, to navigate to the home directory, you just need to type home.

  1. Alias for rebooting the system:

alias reboot="sudo reboot"


This alias allows you to reboot the system by entering just one command.

  1. Alias for updating the system (for Ubuntu/Debian):

alias update="sudo apt update && sudo apt upgrade"


​​​​​​​

 

With this command, you can quickly update all installed packages without entering each command separately.

  1. Alias for searching the system event log:

alias logs="journalctl -xe"

This alias is useful for quickly viewing the system event log when you need to determine the cause of an error.

  1. Alias for safely deleting files:

alias rm="rm -i"

This alias adds a confirmation prompt before deleting files, reducing the risk of accidental data deletion.

 

Additional benefits of using aliases in Linux

 

In addition to the basic examples of creating and deleting aliases, it is worth noting some additional features and benefits of using this command that will help you make your terminal work even more convenient and secure.

Aliases for improving security

One of the important reasons for using aliases is to enhance the security of terminal operations. For example, commands like rm can delete files irreversibly if used carelessly. Creating aliases with protective mechanisms can help avoid such situations.

 

alias rm="rm -i"

alias cp="cp -i"

alias mv="mv -i"

 

These aliases add a confirmation prompt before deleting, copying, or moving files, preventing accidental modification or deletion of data.

Aliases for configuring the development environment

Many developers create aliases to configure their development environment. This allows for faster work with projects, servers, and containers. For example, if you often work with Docker, creating aliases to shorten commands can significantly simplify interaction with containers.

 

alias dps="docker ps"

alias dstart="docker start"

alias dstop="docker stop"

alias drm="docker rm"

 

Now commands for working with Docker containers can be executed much faster using aliases.

Aliases for frequently used SSH connections

If you often need to connect to different servers via SSH, aliases for these connections can also save time and simplify your work. Creating aliases for SSH sessions eliminates the need to enter long commands with keys and addresses each time.

 

alias server1="ssh [email protected]"

alias server2="ssh [email protected]"

 

With these aliases, you can quickly connect to remote servers by simply typing server1 or server2.

Grouping commands with aliases

One useful trick is to create aliases for executing multiple commands simultaneously. This can be especially helpful for performing a sequence of tasks or automating processes.

Example:

alias update_all="sudo apt update && sudo apt upgrade -y && sudo apt autoremove"


 

This alias updates the system, removes unnecessary packages, and performs optimization in one line.

 

How to remember created aliases?

 

When you have many aliases, it can be difficult to remember them all. To avoid forgetting which aliases you have already created, you can use the alias command, as described above, or document all important aliases in a special file.

Some users create text files where they save all important aliases for quick reference when needed. For example, you can create a file aliases.txt in your home directory with descriptions of all key aliases that you regularly use.

 

 

Important recommendations for creating aliases

  • Avoid conflicts with existing commands. Do not create aliases with names that match important system commands. For example, if you create an alias named cd, it may lead to confusion when navigating directories.
  • Use meaningful names. Aliases should be easy to remember. Names like update or reboot are intuitive and reflect the essence of the command.
  • Regularly check aliases. Sometimes old aliases may become unnecessary or interfere with new settings. It is recommended to periodically review your list of aliases and remove those that are no longer needed.

 

 

Conclusion

The alias command in Linux is a simple yet powerful tool for increasing productivity in the command line. With it, you can create convenient shortcuts for frequently used commands, saving you time and making your work in the terminal more efficient. You can create both temporary and permanent aliases, and easily manage them using the alias and unalias commands.

Regular use of aliases will help you optimize everyday tasks and make your work more productive. We recommend experimenting with creating your own aliases tailored to your needs and integrating them into your workflows.