How to Create Bash Aliases

How to Create Bash Aliases

In this tutorial, we will show you how to create bash aliases on a Linux VPS running Ubuntu 16.04 as an operating system. In general, a Bash alias is simply a keyboard shortcut, an abbreviation, a means of avoiding typing a long command line command sequence. Follow the tutorial below and you should be able to create bash aliases in few minutes.

1. Create a Bash Alias

Creating and using bash aliases is very useful. Aliases can be defined directly on the command line, or in .bash_profile, or you can specify user-defined aliases to the ~/.bashrc file. The syntax of creating a bash alias is the following:

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

2. create aliases permanently

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

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

source ~/.bashrc

Alias examples

3. List All Files

To list all files, including hidden files (i.e. starting with .), using a long listing format and append indicator (one of */=>@|) to entries, set the following alias:

alias lh='ls -alF |less'
# lh
drwxr-xr-x  23 root root     4096 Apr  2 10:19 ./
drwxr-xr-x  23 root root     4096 Apr  2 10:19 ../
drwxr-xr-x   2 root root     4096 Mar  4 10:54 bin/
drwxr-xr-x   2 root root     4096 Oct  4  2012 boot/
drwxr-xr-x   6 root  500     1960 Apr  2 10:14 dev/
drwxr-xr-x 115 root root     4096 Apr  4 03:31 etc/
drwxr-xr-x   2 root root     4096 Jan 27  2013 home/
drwxr-xr-x  15 root root     4096 Mar  4 10:54 lib/
drwxr-xr-x   2 root root     4096 Mar 28 12:18 lib64/
drwxr-xr-x   2 root root     4096 Oct  4  2012 media/
drwxr-xr-x   2 root root     4096 Apr 24  2016 mnt/
drwxr-xr-x   2 root root     4096 Oct  4  2012 opt/
dr-xr-xr-x 101 root root        0 Apr  2 10:08 proc/
drwx------   7 root root     4096 Apr  2 10:20 root/
drwxr-xr-x  25 root root      780 Apr  4 09:37 run/
drwxr-xr-x   2 root root     4096 Apr  2 10:13 sbin/
drwxr-xr-x   2 root root     4096 Oct  4  2012 srv/
drwxr-xr-x   7 root root        0 Apr  2 10:08 sys/
drwxrwxrwt   9 root root     4096 Apr  4 18:05 tmp/
drwxr-xr-x  10 root root     4096 Oct  4  2012 usr/
drwxr-xr-x  12 root root     4096 Oct  3  2013 var/
(END)

4. Download Ubuntu OS Package

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

alias agu='sudo apt-get update'

5. Upgrade OS Packages

Upgrade OS packages currently installed on your Ubuntu 16.04 VPS from all configured repositories:

alias agg='sudo apt-get upgrade'

6. Active Processes Info

Display information about the active processes in a custom full wide format listing:

alias psw="ps auwwwwxf |less"
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.3  37648  3880 ?        Ss   Apr02   0:01 init -z
root         2  0.0  0.0      0     0 ?        S    Apr02   0:00 [kthreadd/1308]
root         3  0.0  0.0      0     0 ?        S    Apr02   0:00  \_ [khelper/1308]
root        47  0.0  0.3  42176  3992 ?        Ss   Apr02   0:01 /lib/systemd/systemd-journald
root        50  0.0  0.1  41664  1452 ?        Ss   Apr02   0:00 /lib/systemd/systemd-udevd
root       209  0.0  0.1  47572  1624 ?        Ss   Apr02   0:00 /sbin/rpcbind -f -w
root       212  0.0  0.1  28492  1480 ?        Ss   Apr02   0:00 /lib/systemd/systemd-logind
root       213  0.0  0.1  26012  1288 ?        Ss   Apr02   0:00 /usr/sbin/cron -f
syslog     215  0.0  0.1 262488  1824 ?        Ssl  Apr02   0:00 /usr/sbin/rsyslogd -n
message+   216  0.0  0.1  42852  1840 ?        Ss   Apr02   0:00 /usr/bin/dbus-daemon --system --address=systemd: --nofork --nopidfile --systemd-activation
root       258  0.0  0.2  65464  2492 ?        Ss   Apr02   0:00 /usr/sbin/sshd -D
bind       259  0.0  1.7 287416 18620 ?        Ssl  Apr02   0:00 /usr/sbin/named -f -u bind
mysql      284  0.0 12.1 1355252 127012 ?      Ssl  Apr02   1:33 /usr/sbin/mysqld
root       297  0.0  0.1  18100  1084 ?        Ss   Apr02   0:00 /usr/sbin/dovecot
dovecot    307  0.0  0.0   9464   704 ?        S    Apr02   0:00  \_ dovecot/anvil
root       308  0.0  0.0   9596   932 ?        S    Apr02   0:00  \_ dovecot/log
root       349  0.0  0.2 100188  2284 ?        Ss   Apr02   0:00 /usr/sbin/saslauthd -a pam -c -m /var/run/saslauthd -n 2
root       359  0.0  0.2 100188  2272 ?        S    Apr02   0:00  \_ /usr/sbin/saslauthd -a pam -c -m /var/run/saslauthd -n 2
root       364  0.0  0.0  12788   852 tty2     Ss+  Apr02   0:00 /sbin/agetty --noclear tty2 linux
root       365  0.0  0.0  12788   856 tty1     Ss+  Apr02   0:00 /sbin/agetty --noclear --keep-baud console 115200 38400 9600 vt220
root       378  0.0  0.0  15000   832 ?        Ss   Apr02   0:00 /usr/sbin/xinetd -pidfile /run/xinetd.pid -stayalive -inetd_compat -inetd_ipv6
root       407  0.0  0.4 108080  4664 ?        Ss   Apr02   0:08 sendmail: MTA: accepting connections
root       416  0.0  1.8 336564 19064 ?        Ss   Apr02   0:04 /usr/sbin/apache2 -k start
www-data 23498  0.0  0.7 337108  7528 ?        S    00:28   0:00  \_ /usr/sbin/apache2 -k start
www-data 23499  0.0  0.7 337116  7460 ?        S    00:28   0:00  \_ /usr/sbin/apache2 -k start
www-data 23500  0.0  0.7 337088  7488 ?        S    00:28   0:00  \_ /usr/sbin/apache2 -k start
www-data 23501  0.0  0.7 337036  7572 ?        S    00:28   0:00  \_ /usr/sbin/apache2 -k start
www-data 23502  0.0  0.7 337112  7640 ?        S    00:28   0:00  \_ /usr/sbin/apache2 -k start
www-data 24170  0.0  0.7 337116  7528 ?        S    01:23   0:00  \_ /usr/sbin/apache2 -k start
www-data 27193  0.0  0.6 336656  7284 ?        S    09:50   0:00  \_ /usr/sbin/apache2 -k start
www-data 30187  0.0  0.5 336596  5520 ?        S    17:41   0:00  \_ /usr/sbin/apache2 -k start
root     30766  0.0  0.0  27492   600 ?        Ss   18:02   0:00 pts/0
root     30767  0.0  0.1  18192  2064 pts/0    Ss   18:02   0:00  \_ -bash
root     31860  0.0  0.1  34504  1588 pts/0    R+   18:41   0:00      \_ ps auwwwwxf
root     31861  0.0  0.0   6688  1000 pts/0    S+   18:41   0:00      \_ less
(END)

7. Find Memory-Hungry Processes

Find the top 10 memory-hungry processes on your server:

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

To enable this alias, you need to have ‘bc’ package installed on your server, so install it on your server if it is not already done so:

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

8. Restart Server

Create an alias to restart the server:

alias rst='sudo shutdown –r now'

9. Change the Default Output of the ‘df’ Command

Change the default output of the ‘df’ command and print sizes in powers of 1024:

alias df='df -h'

10. Change the Default Output of the ‘ping’ Command

Change the default output of the ‘ping’ command and stop after sending 4 ECHO_REQUEST packets:

alias ping='ping -c 4'

11. Remove Alias

Removing aliases is as simple as adding them. To unset a specific alias, use:

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

12. Unset all aliases

To unset all aliases, use the following command:

# unalias -a

Of course, you don’t have to create bash aliases if you use one of our managed VPS hosting plans, in which case you can simply ask our expert Linux admins to create bash aliases for you. They are available 24×7 and will take care of your request immediately.

PS. If you liked this post on how to create bash aliases, please share it with your friends on the social networks using the buttons or simply leave a reply below. Thanks.

1 thought on “How to Create Bash Aliases”

Leave a Comment