Terminal Aliases – Command-Line Shortcuts You Must Know

Bash allows you to create terminal aliases to speed up your workflows, reduce the amount of typing required, and make your routine tasks more efficient. The terminal is the key to everything in Linux, and we all must have used it sometime in our journey, either to troubleshoot or just to take a look at the beautiful Neofetch output.

But, sometimes typing long commands can be exhausting, and it can waste some of your valuable time if you forgot to run that command as sudo, and now you have to type it out all over again.

That’s where aliases come in. Bash (or its alternatives like Zsh, Fish, Ksh etc.) is a powerful tool through which you can make certain words or letters act as a ‘trigger’ to run certain commands, essentially saving you a lot of time.

In this article, we are going to show you how to modify the bashrc (or Zshrc, and so on) to add certain aliases for your Operating System.

Identify The Shell In Use

Most of the distributions ship with Bash as their default, however Manjaro ships with zsh as their default shell. Type the following commands in your terminal to know which shell are you using :

ps $$
Output of ps $$ command
Output of ps $$ command

Or type the following

grep `whoami` /etc/passwd | cut -d':' -f7

You will know which shell you are using.

Backup your .bashrc

Before we change anything, backup your original Bashrc so that if anything goes wrong, you can always come back to it. Open a terminal and type the following commands :

cp ~/.bashrc ~/.bashrc.bak

Modify the .bashrc file to add terminal aliases

Open up a terminal and type the following commands if you have the default text editor nano :

nano .bashrc

Go to the very end and press enter, we can now start adding aliases.

Opening Bashrc in Nano
Opening Bashrc in Nano

Distribution Independent Terminal Aliases

You can add these aliases irrespective of which distribution you have, they will work just fine in each distribution.

# Power related commands
alias shutdown="sudo shutdown -P now"
alias reboot="sudo shutdown -r now"

# Colorize Grep output
alias grep="grep --color=auto"

# ls command
alias ls="ls -C --color=auto"
alias lm="ls -lhA --color=auto | more" # For big folders and Hidden files
alias ll="ls -lh --color=auto" # To show permissions of files

# Make mount command output pretty and readable
alias mount='mount |column -t'

# Stop after sending 5 pings
alias ping='ping -c 5'

# A quick way to get out of current directory
alias ..='cd ..'
alias ...='cd ../../../'
alias ....='cd ../../../../'
alias .....='cd ../../../../'
alias .4='cd ../../../../'
alias .5='cd ../../../../..'

# Date and Time
alias now="date +"%T""
alias nowtime=now
alias nowdate="date +\"%d-%m-%Y\""

# Miscellaneous
alias mkdir="mkdir -pv" # Creates parent directories if needed
alias hist="history"
alias home="cd ~/"
alias root="sudo su"
alias rm="rm -i" # Interactive mode delete
alias diskspace="du -S | sort -n -r |more" # To find out what’s taking so much space
# To show size of all the folders in the current directory
alias folders="find . -maxdepth 1 -type d -print | xargs du -sk | sort -rn"

# Add this to easily extract compressed files, use extract <filename> to extract, this will only work if you have all the packages installed
extract () {
   if [ -f $1 ] ; then
       case $1 in
           *.tar.bz2)   tar xvjf $1    ;;
           *.tar.gz)    tar xvzf $1    ;;
           *.bz2)       bunzip2 $1     ;;
           *.rar)       unrar x $1       ;;
           *.gz)        gunzip $1      ;;
           *.tar)       tar xvf $1     ;;
           *.tbz2)      tar xvjf $1    ;;
           *.tgz)       tar xvzf $1    ;;
           *.zip)       unzip $1       ;;
           *.Z)         uncompress $1  ;;
           *.7z)        7z x $1        ;;
           *)           echo "don't know how to extract '$1'..." ;;
       esac
   else
       echo "'$1' is not a valid file!"
   fi
 }

Press Ctrl + O to save the file, and press Ctrl + X to exit. Now, to apply these changes, you can always restart the terminal app, but you can also type the following to apply the changes:

source .bashrc

Distribution Specific Terminal Aliases for Linux

From now on, pick your distribution and add aliases accordingly.

Ubuntu and Ubuntu-based distributions

Once again, open a terminal, and type

nano .bashrc

Navigate to the last and add the following script :

# Update
alias update="sudo apt update && sudo apt full-upgrade"
alias remove="sudo apt-get autoremove"

Arch and Arch-based distributions

Open the .bashrc file and copy and paste the following script at the end of the file :

# Update
alias update="sudo pacman -Syu”
# Autoremove
alias remove="sudo pacman -Rns"

Save the file and exit

RHEL and RHEL-based distributions

Again, open .bashrc and type the following script at the end of the file :

# Update
alias update="sudo yum update”

Save the file and exit.

Custom Bashrc on Fedora
Custom Bashrc on Fedora

Summary

We hope these Bashrc modifications will save you a lot of time, and you don’t have to type long commands in a terminal ever again. You can always add new aliases anytime you want, just make sure that you always have a backup in case you mess something up.