Situatie
Consider the following command that prints the top ten directories that take up the most disk space:
# du -h /path/to/directory | sort -hr | head -n 10
This is a frequent check, and you can imagine the effort required to type this command in the terminal and scripts. Repetitively typing or copying the same command lowers productivity and introduces a hassle you can easily avoid with the alias command.
By making aliases for the commands you use frequently, you can save yourself a couple of hours every month. Similar to personalized shortcuts, aliases refer to a command (or group of commands) that can be used with or without personalized parameters.
You are likely utilizing aliases on your Linux system without being aware of the fact.
This article will introduce you to the alias command. We’ll show you how to create command aliases with the Linux alias command. Next, we’ll go into the details of making command aliases permanent on your system. Finally, we’ll show you how to remove command aliases.
But first, let’s start with a short introduction to the idea of command aliases in Linux.
Solutie
In Linux, an alias is a shortcut that leads to a command. In technical terms, an alias is a user-defined string that replaces another string (consisting of commands and parameters) when called by a command in the Linux shell.
Aliases are usually used in place of long commands to improve productivity and lower the possibility of typing errors. Users can use long commands without having to remember them. When defined, the user enters the alias, and the shell replaces it with the actual command.
Prerequisites to working with the Linux alias Command
Before working with the Linux alias command, you will need the following:
- A computer running a mainstream Linux distribution
- An account that has sudo access
- Command line or terminal access
- Your preferred text editor
The idea of using aliases for commands is not restricted to power users. Novice users can also set up aliases for frequently used commands. However, before going into the details of setting up command aliases, let’s look at some aliases that are usually available by default on a Linux system.
List the Currently Defined Linux Aliases
You can view a list of aliases present on your profile using the alias command.
# alias
The output shows the default aliases available for the logged-in user on our test Ubuntu system. For instance, you can see that the command alias ll is used for the ls -alF command.
We recommend using the -p flag with the alias command so that the output is presented in a format where you can copy the command and reuse it:
# alias -p
Create Aliases in Linux
You can set up a single character to create an alias that will function as the paired command. Here are the two types of aliases in Linux:
- Temporary: Use the alias command to create these aliases.
- Permanent: Edit system files to add permanent command aliases.
Create a Temporary Alias
Use the alias command to make a temporary command alias that is removed when the current terminal session ends. For instance, here’s the command to substitute the letter c as an alias for the clear command:
# alias c='clear'
You can add command arguments and parameters when creating an alias. This is a great way to add the commonly used flags to a command. For instance, the following command sets move as an alias for the mv command and add the -i flag to enable a request for authorization before overwriting:
# alias move='mv -i'
A common use of temporary aliases is creating a shortcut for executing scripts. For this, assign the script’s path to an alias. For instance, consider the following command:
# alias frename='Example/Test/file_rename.sh'
After executing this example command, you can use the alias frename to run the file_rename.sh bash script.
Create a Permanent Alias
Temporary aliases disappear when you close the terminal session. This means you need to declare these aliases every time you start a new terminal session.
You can avoid this hassle by adding permanent aliases. For this, you can edit the shell configuration file and add an entry for the alias. Here are the configuration files for the popular shells:
- Bash shell: ~/.bashrc
- Zsh shell: ~/.zshrc
- Fish shell: ~/.config/fish/config.fish
We will edit ~/.bashrc, the configuration file for the Bash shell in the Nano editor for this demonstration.
Start by running the following command in the terminal:
# sudo nano ~/.bashrc
Scroll down to the section for alias definition.
Add your aliases with a detailed comment. Remember to add each alias on a separate line.
In our example:
Save the configuration file with Ctrl+X and then press Y and Enter.
You need to restart the terminal session so that the system loads the updated config file. Alternatively, use the source command to load the configuration file if you wish to use the aliases in the current session:
# source ~/.bashrc
Remove Aliases in Linux
You can use the unalias command to remove a previously set command alias. The syntax of the command is as follows:
# unalias [name]
To demonstrate the command, we will use the following command to remove the alias frename we set in a previous section.
# unalias frename
If you wish to unset all command aliases, use the -a flag with the command:
# unalias -a
As you can see, the unalias -a command removed all command aliases from the system.
Leave A Comment?