Skip to content

How to Use rm Command

homepage-banner

The command line terminal is a convenient and fast tool for interfacing with the Linux operating system. However, issuing the same commands repeatedly can be time-consuming, especially if they are lengthy, hard to remember, or repetitive. To save time and reduce frustration, this guide shows you how to use the alias command to create customizable shortcuts for your most frequently used commands.

List Current Aliases

Most systems come with default aliases already configured. You can list them by running the alias command:

alias

Here is the output from the default installation of Ubuntu 20.04:

alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'
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'

Overwriting Command Names

It is typical to create aliases with unique names that do not conflict with other utilities or existing commands. You can also intentionally overwrite command names with alias so they always take specific options, or replace them with different names. For example, you can make the top command run htop instead, or you can have ls always run as ls -lah.

If you later want to run an original command without an alias, simply prepend \ to the command. For example:

\top

How to Create an Alias

There are two ways to create aliases for your use - temporary or permanent. Temporary aliases are only available until you close your current terminal session. Permanent aliases are saved to the shell configuration file and are available in every new session you create.

Create a Temporary Alias

To create a temporary alias, use the alias command followed by the shortcut and the command you want it to replace.

For example:

alias shortcut="your custom command"

For a concrete example, let’s say you want to create an alias to update and upgrade your system without having to type the entire command sudo apt update && sudo apt upgrade. You can create an alias called update for this purpose:

alias update="sudo apt update && sudo apt upgrade"

To have the top command run htop instead:

alias top="htop"

Create a Permanent Alias

Temporary aliases are only valid for the current terminal session. Once you close the session, they are no longer available. To make them permanent, you can save them in the shell configuration file. Depending on your shell, this file could be:

  • ~/.bashrc (Bash shell)
  • ~/.zshrc (Zsh shell)
  • ~/.bash_profile (MacOS)
  • ~/.bash_login (MacOS)
  • ~/.profile (MacOS and some other Unix-based systems)

The syntax for creating a permanent alias is the same as creating a temporary one. However, now you save it in the configuration file.

With your preferred text editor, open the appropriate configuration file, such as ~/.bashrc. Enter one alias per line. While you can add your aliases anywhere in this file, grouping them together makes them easier to reference and adjust.

File: ~/.bashrc

...
#aliases
alias update="sudo apt update && sudo apt upgrade"
alias top="htop"
...

Any newly added aliases will not be immediately available for use in current terminal sessions. They will be available for use in your next session.

If you wish to use them right away in a current session, use one of the following commands as appropriate:

source ~/.bashrc
source ~/.zshrc
source ~/.config/fish/config.fish

How to Remove Aliases

To remove an alias, use the unalias command:

unalias alias-name

For example, to remove the update temporary alias from above, enter:

unalias update

To remove all aliases:

unalias -a

Note

Removing all aliases also removes the system default aliases.

Helpful Examples

Here are some helpful alias examples that you may wish to save:

  1. To change quickly into a specific directory that you visit often, you can set an alias:
alias docs="cd /Users/exampleuser/mydirectory/docs"
  1. If you work in Python, you can use these two aliases to create a virtual environment quickly:
alias ve='python3 -m venv ./venv'
alias va='source ./venv/bin/activate'
  1. Use this alias to find your external IP quickly:
alias ipe="curl ipinfo.io/ip"
  1. If you use git and wish to see the differences between your current branch the development branch (change development to any other branch you wish to compare):
alias gdd="git diff --name-only $(git merge-base $(git rev-parse HEAD) development)"
  1. Again, for git, if you want to view a list of your most recent branches:
alias glh="git for-each-ref --sort=-committerdate refs/head | head"
Leave a message