alias command all options with examples in Linux

What is alias command:

alias command as the name suggests used to create shortcuts for commands in Linux. With creating aliases to the commands we can use alias name instead of the actual command. It is very easy to remember alias names instead of actual command.

What are the uses of alias command:

Alias for commands will be useful when you want to use long commands every day. For Linux system administrators on their day to day work they have to execute multiple commands which might long commands or sometimes short commands. In case of using long commands they can create alias for those commands and run the commands by executing alias name.

In another scenario if you are a rare user of Linux server in that case there might be chance of forgetting the day to day commands. For this if you create an alias for all your day to day using commands so that you can only remember the alias names which is easy. This way we can use alias commands.

Alias command syntax:

alias [-p] [name=value]

name = The alias name that you want to set for the actual command

value = The actual command that you want to set alias

Below is a sample alias command the we are setting for ‘ls -lrth’ (List the entries in long list format in reverse order based on the modification time) command. If you want to execute ‘ls -lrth’ command you can simple use ‘list’ alias name in the command line and you will get the output of ‘ls -lrth’ command. Please check the all examples of ls (list) command in this article.

$ alias list='ls -lrth'
$ list

total 14K
-rw-------. 1 root root 1.1K Sep 21 04:43 anaconda-ks.cfg
-rw-r--r--. 1 root root 1.4K Sep 21 05:02 initial-setup-ks.cfg
drwxr-xr-x. 2 root root    6 Sep 21 05:04 Templates
drwxr-xr-x. 2 root root    6 Sep 21 05:04 Public
drwxr-xr-x. 2 root root    6 Sep 21 05:04 Downloads
drwxr-xr-x. 2 root root    6 Sep 21 05:04 Desktop
drwxr-xr-x. 2 root root    6 Sep 21 05:04 Videos
drwxr-xr-x. 2 root root    6 Sep 21 05:04 Pictures
drwxr-xr-x. 2 root root    6 Sep 21 05:04 Music
drwxr-xr-x. 2 root root    6 Sep 21 05:04 Documents
drwxr-xr-x. 4 root root   36 Sep 26 12:36 test_data
drwxr-xr-x. 3 root root   16 Sep 26 12:42 youu
-rw-r--r--. 1 root root  187 Oct  5 12:38 test_file.txt
-rw-r--r--. 1 root root    0 Oct  8 02:07 test.py

Alias command options:

Note: Before working with alias command or making any changes to alias command make sure to take the backup of current aliases to notepad or to a text file by using below command. This is to ensure that if anything goes wrong or default aliases are removed we can restore them from the backup.

Display the available aliases in the server:

To check the already available aliases in the server use -p option in alias command as below.

$ alias -p

alias cp='cp -i'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l.='ls -d .* --color=auto'
alias list='ls -lrth'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias rm='rm -i'

Create a new alias in the server:

To create a new alias for any command mention the name and value in alias command as below. In below alias we are setting ‘grep -i’ command to ‘search’ alias name.

$ alias search='grep -i'
$ search test test_file.txt

Test line 1
Test line 2
Test line 3
Test line 4
Test line 5
Test line 9
Test line 10

Create alias and display the current aliases in the server:

To create an alias and to display the current aliases instead of using two commands (alias, alias -p) we can user -p option along with alias name and value. Please check the example below.

$ alias -p search='grep -a'
$ alias -p display='cat'
$ alias -p ls='ls --color=auto'

How to remove alias commands:

To remove alias for any command use unalias command as below. In below command we are deleting the alias name ‘search’ which we set on previous example.

$ unalias search

Check whether ‘search’ alias has been removed or not.

$ alias -p
$ search Report test_file.txt

bash: search: command not found...

Remove all aliases in the server:

While using this option please take a backup of aliases by using alias -p command. With -a option we can delete all the aliases (either default or user defined) set in the server. Be extra sure while executing this command. Once you deleted aliases we cant restore those unless you have a backup of the aliases.

$ unalias -a

How to create permanent alias:

The aliases we are setting are temporary for that session. If you want to create permanent alias which can be used on multiple sessions configure it in user’s shell configuration file. User’s shell configuration file is available in user’s home directory.

$ vim ~/.bashrc
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
alias list='ls -lrth'

Examples of alias command:

Below are some of the useful examples of alias commands for day to day work.

Alias command for case insensitive search in using grep command:

By default grep command will search for exact match for the searching string. If you want to use search in case insensitive format use -i option in grep.

$ alias search='grep –i'

alias command to sort the list output based on last modified time:

$ alias list='ls -lrth'

Alias command to remove empty characters in a file using sed:

To remove empty characters in starting and ending of lines in a file use below command. It will be useful to remove the unnecessary spaces added when you copy a file from windows to Linux.

$ alias convert_file='sed "/^\s*$/d" '
$ convert_file reports.txt

New data
Test line 1
Test line 2
Test line 3
Test line 4
Test line 5

Empty a file:

To remove the file contents use /dev/null command.

$ alias empty_file='cat /dev/null > '

Learn more about all the cat command examples here

$ empty_file reports.txt

Check which files containing more disk size in MB, GB format:

$ alias disk_usage='du -sh * | grep M'
$ alias disk_usage='du -sh * | grep G'

If you want to check the all the du (disk usage) command examples check this article on du command.

$ disk_usage

881M    cache
182M    lib
22M     log

Check crontab entries:

To check the crontab entries set the alias with below command.

$ alias cron='crontab -l'

Restart a service:

For rhel 7 and above versions

$ alias srv_restart='systemctl restart '

To restart httpd service use below command

$ srv_restart httpd

Check the service status:

For rhel 7 and above versions

$ alias check_status='systemctl status '
$ check_status httpd

Leave a Comment