How to create aliases in Bash

In this tutorial, we’ll show you how to create aliases in Bash on a Linux VPS running Ubuntu 16.04 as the operating system. In general, an alias in Bash is just a keyboard shortcut, an abbreviation, a means to avoid typing a long line of commands.

How to create an alias

Creating and using Bash aliases is very helpful. Aliases can be defined directly on the command line or in .bash_profile, or you can define specific custom aliases in the ~ / .bashrc file. The syntax for creating a Bash alias is as follows:

alias name="unix command /with some options/parameters"

If you want to create aliases permanently, edit the ~ .bashrc file, add aliases at the end of the file, and save it.

The changes to .bashrc will be active after you log out and log back in again, or you can use the following command to execute the file and enable the newly created aliases instantly:

source ~/.bashrc

Examples of aliases

To list all files including hidden files, use long listing format and add an indicator (one of * / => @ |) to write, set the following alias:

alias lh="ls -alF |less"
# lh
drwxr-xr-x  23 root root     4096 Jan  4 09:24 ./
drwxr-xr-x  23 root root     4096 Apr 11 09:43 ../
drwxr-xr-x   2 root root     4096 Dec  7 10:12 bin/
drwxr-xr-x   2 root root     4096 Jan  7 2012 boot/
drwxr-xr-x   6 root  500     1960 Apr  2 10:42 dev/
drwxr-xr-x 115 root root     4096 Apr  7 03:24 etc/
drwxr-xr-x   2 root root     4096 Dec 27 2013 home/
drwxr-xr-x  15 root root     4096 Mar  6 09:45 lib/
drwxr-xr-x   2 root root     4096 Jan 23 10:21 lib64/
drwxr-xr-x   2 root root     4096 Oct  6 2014 media/
drwxr-xr-x   2 root root     4096 Apr 24 2015 mnt/
drwxr-xr-x   2 root root     4096 Oct  4 2016 opt/
dr-xr-xr-x 101 root root        0 Dec 15 09:45 proc/
drwx------   7 root root     4096 Jan  1 09:16 root/
drwxr-xr-x  25 root root      780 Dec 13 08:23 run/
drwxr-xr-x   2 root root     4096 Apr  2 09:11 sbin/
drwxr-xr-x   2 root root     4096 Oct 12 2015 srv/
drwxr-xr-x   7 root root        0 Dec 14 09:12 sys/
drwxrwxrwt   9 root root     4096 Jan  1 14:22 tmp/
drwxr-xr-x  10 root root     4096 Jan 12 2016 usr/
drwxr-xr-x  12 root root     4096 Oct  2 2016 var/
(END)

Download information about Ubuntu OS packages from configured repositories via sources.list:

alias agu='sudo apt-get update'

Updating installed operating system packages on your Ubuntu 16.04 VPS on all configured repositories:

alias agg='sudo apt-get upgrade'

Displaying information about active processes in a custom list in wide-angle format:

alias psw="ps auwwwwxf |less"
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND                                                                                    
root         2  0.0  0.0      0     0 ?        S    Apr03   0:00 [kthreadd]                                                                                   
root         3  0.0  0.0      0     0 ?        S    Apr03   0:16  _ [ksoftirqd/0]                                                                            
root         5  0.0  0.0      0     0 ?        S<   Apr03   0:00  _ [kworker/0:0H]                                                                           
root         7  0.0  0.0      0     0 ?        S    Apr03   0:00  _ [migration/0]                                                                            
root         8  0.0  0.0      0     0 ?        S    Apr03   0:00  _ [rcu_bh]                                                                                 
root         9  0.0  0.0      0     0 ?        S    Apr03   2:00  _ [rcu_sched]                                                                              
root        10  0.0  0.0      0     0 ?        S    Apr03   0:01  _ [watchdog/0]                                                                             
root        11  0.0  0.0      0     0 ?        S    Apr03   0:01  _ [watchdog/1]                                                                             
...
(END)

Find the top 10 processes consuming memory on the server:

alias ram='while read command percent rss; do if [[ "${command}" != "COMMAND" ]]; then rss="$(bc <<< "scale=2;${rss}/1024")"; fi; printf "%-26s%-8s%sn" "${command}" "${percent}" "${rss}"; done < <(ps -A --sort -rss -o comm,pmem,rss | head -n 11)'

To enable this alias, you need to install the “bc” package on your server, install it on your server if you haven’t already:

apt-get install bc
# ram
COMMAND                   %MEM    RSS
mysqld                    12.1    124.03
python                    6.9     71.13
apache2                   1.8     18.61
named                     1.7     18.18
apache2                   0.7     7.46
apache2                   0.7     7.39
apache2                   0.7     7.35
apache2                   0.7     7.35
apache2                   0.7     7.31
apache2                   0.7     7.28

Create an alias to reboot the server:

alias rst="sudo shutdown –r now"

Changing the default output for the “df” command and printing in powers of 1024:

alias df="df -h"

Change the default data of the “ping” command and stop after sending ECHO_REQUEST 4 packets:

alias ping='ping -c 4'

Removing aliases is as easy as adding them. To remove a specific alias use:

# unalias lh
# lh
-bash: lh: command not found

To reset all aliases, use the following command:

# unalias -a

Sidebar