Chapter 6. Commands for Command Management

Alias (Create Command Shortcuts)

Alias (Create Command Shortcuts)
Tag:

The alias command is used to create command shortcuts. You can use the command to shorten your typing for frequently used commands or long complex commands. You can even combine multiple commands into one alias command.

Display registered alias

By running the alias command without any argument, you can display a registered alias. For example, when you run the alias command on Ubuntu OS on AWS Lightsail, you can see preregistered aliases.

Command Line - INPUT
alias
Command Line - RESPONSE
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l='ls -CF'
alias la='ls -A'
alias ll='ls -alF'
alias ls='ls --color=auto'

One of the most commonly used commands, alias ll, has already been registered. When you run the ll alias command, you can see the same result as when you run the ls -l command.

Command Line - INPUT
ll
Command Line - RESPONSE
total 72
drwxr-xr-x  19 root root  4096 Jan  8 08:29 ./
drwxr-xr-x  19 root root  4096 Jan  8 08:29 ../
lrwxrwxrwx   1 root root     7 Jul 16  2020 bin -> usr/bin/
drwxr-xr-x   3 root root  4096 Jan  8 09:11 boot/
drwxr-xr-x  15 root root  3180 Jan  5 13:52 dev/
:

Create a new alias

To create a new alias, you need to define an alias name and a command to be assigned to it. Use quotation marks to create an alias for a command with arguments separated by one or more spaces. For example, to create a new alias named md1 that executes "mkdir dir_1", run the command below.

Command Line - INPUT
alias md1="mkdir dir_1"

Run the alias and check the result with the ls command.

Command Line - INPUT
md1
ls
Command Line - RESPONSE
dir_1

Create a new alias with multiple commands

To create a new alias with multiple commands, use ; (semicolon) to combine commands. The example below shows how to create a new alias named mdfa to execute two commands – mkdir and touch.

Command Line - INPUT
alias mdfa="mkdir dir_a;touch dir_a/file_a"

Run the command and check the results with the ls command.

Command Line - INPUT
mdfa
ls
Command Line - RESPONSE
dir_1  dir_a
Command Line - INPUT
ls dir_a
Command Line - RESPONSE
file_a

You can also check how the commands are registered by running the alias command.

Command Line - INPUT
alias
Command Line - RESPONSE
:
alias ls='ls --color=auto'
alias md1='mkdir dir_1'
alias mdfa='mkdir dir_a;touch dir_a/file_a'

Delete an existing alias

To delete an existing alias, you can use the unalias command.
Delete alias md1.

Command Line - INPUT
unalias md1

Check if it was deleted by running the alias command.

Command Line - INPUT
alias
Command Line - RESPONSE
:
alias ls='ls --color=auto'
alias mdfa='mkdir dir_a;touch dir_a/file_a'

The alias command is used to create command shortcuts. You can use the command to shorten your typing for frequently used commands or long complex commands. You can even combine multiple commands into one alias command.

Display registered alias

By running the alias command without any argument, you can display a registered alias. For example, when you run the alias command on Ubuntu OS on AWS Lightsail, you can see preregistered aliases.

Command Line - INPUT
alias
Command Line - RESPONSE
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l='ls -CF'
alias la='ls -A'
alias ll='ls -alF'
alias ls='ls --color=auto'

One of the most commonly used commands, alias ll, has already been registered. When you run the ll alias command, you can see the same result as when you run the ls -l command.

Command Line - INPUT
ll
Command Line - RESPONSE
total 72
drwxr-xr-x  19 root root  4096 Jan  8 08:29 ./
drwxr-xr-x  19 root root  4096 Jan  8 08:29 ../
lrwxrwxrwx   1 root root     7 Jul 16  2020 bin -> usr/bin/
drwxr-xr-x   3 root root  4096 Jan  8 09:11 boot/
drwxr-xr-x  15 root root  3180 Jan  5 13:52 dev/
:

Create a new alias

To create a new alias, you need to define an alias name and a command to be assigned to it. Use quotation marks to create an alias for a command with arguments separated by one or more spaces. For example, to create a new alias named md1 that executes "mkdir dir_1", run the command below.

Command Line - INPUT
alias md1="mkdir dir_1"

Run the alias and check the result with the ls command.

Command Line - INPUT
md1
ls
Command Line - RESPONSE
dir_1

Create a new alias with multiple commands

To create a new alias with multiple commands, use ; (semicolon) to combine commands. The example below shows how to create a new alias named mdfa to execute two commands – mkdir and touch.

Command Line - INPUT
alias mdfa="mkdir dir_a;touch dir_a/file_a"

Run the command and check the results with the ls command.

Command Line - INPUT
mdfa
ls
Command Line - RESPONSE
dir_1  dir_a
Command Line - INPUT
ls dir_a
Command Line - RESPONSE
file_a

You can also check how the commands are registered by running the alias command.

Command Line - INPUT
alias
Command Line - RESPONSE
:
alias ls='ls --color=auto'
alias md1='mkdir dir_1'
alias mdfa='mkdir dir_a;touch dir_a/file_a'

Delete an existing alias

To delete an existing alias, you can use the unalias command.
Delete alias md1.

Command Line - INPUT
unalias md1

Check if it was deleted by running the alias command.

Command Line - INPUT
alias
Command Line - RESPONSE
:
alias ls='ls --color=auto'
alias mdfa='mkdir dir_a;touch dir_a/file_a'
Tag: