How to use alias command in Linux

Time:2021-11-29

Alias commands are often used in Linux, which can make the work on the Linux command line more smooth, but they may also be complex and difficult to remember. This article discusses how to make alias commands.
Generally, the alias command is suitable for the following scenarios:

Simplify overly long and complex commands
Remember commands with complex names
Save time by using commands you often use
How to create alias
For example, set an alias to list all files, including hidden files. The alias is La:

[[email protected] ~]# alias la=’ls -al’
Execute the alias LA to see:

[[email protected] ~]# la
total 68
dr-xr-x—. 3 root root 216 May 25 13:13 .
drwxr-xr-x. 19 root root 271 May 7 15:12 ..
-rw——-. 1 root root 1178 Dec 29 2019 anaconda-ks.cfg
-rw——-. 1 root root 14798 May 20 01:48 .bash_history
-rw-r–r–. 1 root root 18 May 11 2019 .bash_logout
-rw-r–r–. 1 root root 176 May 11 2019 .bash_profile
-rw-r–r–. 1 root root 176 May 11 2019 .bashrc
drwx—— 3 root root 17 Mar 11 11:17 .cache
-rw-r–r–. 1 root root 100 May 11 2019 .cshrc
-rw——- 1 root root 64 May 25 13:13 .lesshst
-rw-r–r– 1 root root 234 May 19 09:52 sample.html
-rw-r–r–. 1 root root 129 May 11 2019 .tcshrc
-rw-r–r– 1 root root 1178 May 19 09:32 test.txt
-rw——- 1 root root 15904 May 25 10:15 .viminfo
How to use alias command in Linux how to use alias command in Linux
If you want to use it permanently, you can write this command to the ~ /. Bashrc file.

[[email protected] ~]# echo “alias la=’ls -al'” >> ~/.bashrc
How to use alias command in Linux how to use alias command in Linux

How to list alias
Use the alias command to list all aliases that have been set in the system:

[[email protected] ~]# alias
How to use alias command in Linux how to use alias command in Linux

Check if the command type is an alias
To check whether the command is an alias, use the which command. The following example shows the alias.

[[email protected] ~]# which la
alias la=’ls -al’
/usr/bin/ls
How to delete alias
If you need to deactivate the alias, you can use the unalias command. To make the change permanent, you need to delete the corresponding alias in the ~ /. Bashrc file.

[[email protected] ~]# unalias la
Use alias for common commands
For frequently used commands, you can save more time by shortening them by using aliases. In the aliases shown below, we clear the screen with one letter, use ll to display the files in the directory, and view only the five files recently created or updated:

[[email protected] ~]# alias c=’clear’
[[email protected] ~]# alias ll=’ls -al’
[[email protected] ~]# alias new=’ls -1tr | tail -5′
Use alias to change the behavior of the command
For example, if you want the ping command to request only four times, you can use this alias to ensure that it makes only four Ping requests:

[[email protected] ~]# alias ping=’ping -c 4′
How to use alias command in Linux how to use alias command in Linux

Use alias to avoid the option of long strings
Aliases are useful for remembering longer command options. For example, to unzip a file, you can use the following alias:

[[email protected] ~]# alias untar=’tar -xvf’
How to use alias command in Linux how to use alias command in Linux

Use alias to view the history of commands
To view history using search terms:

[[email protected] ~]# alias rec=’history | grep’
You can see the relevant records retained in the history, for example:

[[email protected] ~]# rec alias
How to use alias command in Linux how to use alias command in Linux

Use alias to search for related commands
To find commands related to search terms, use the apropos command, but the command is a little strange. You can set an alias and use the following command:

[[email protected] ~]# alias ?=”apropos”
Use below? Search for commands related to compress:

[[email protected] ~]# ? compress
How to use alias command in Linux how to use alias command in Linux

Show your IP address
[[email protected] ~]# alias myip=’hostname -I’
How to use alias command in Linux how to use alias command in Linux

summary
Alias commands are often used in Linux, which can make the work on the Linux command line more smooth, but they may also be complex and difficult to remember. This article discusses how to make alias commands.