How to Create Productive Bash Command Aliases

How to Create Productive Bash Command Aliases

Typing commands have never been easier using aliases. You can now cut down typing time with these aliases, work smartly, and increase productivity at the command prompt.

If you find yourself typing the same command over and over again, then you'll know how redundant and time-consuming (laziness) these little commands are to type.

Fortunately, you can get rid of that by shortening the length of a command you type by creating an alias.

Command Aliases are powerful commands that make our daily routine easier and faster as a Software Developer, System Admin, Data Scientist, Dev Ops as long as we live on the command line.

This is actually one of the most useful and powerful features of various command shells. You probably use the same commands over and over on a daily basis if you use the command line for almost everything. With aliases, you can make these everyday tasks easier to run.

The alias command allows you to run any command or group of commands (with options and filenames) by using an abbreviated word or single characters that are defined in either .bashrc, .zschrc, .tcshrc, .cshrc or any other file that's sourced.

How to Create A Command Alias

Command Aliases are simply shortcutting names for running commands. They consist of one word or even one letter that you can use instead of typing a longer command.

To create an alias, we'll be using the alias command which is used to create aliases and below is the syntax:

alias name='command'
alias name='command arg1 arg2'

In the syntax above:

  1. alias is the command for creating command aliases
  2. name is the name we'll be calling our command alias
  3. command is the command to run when we type the alias command

So with all these, we can create a simple example as below:

alias clr='clear'

In the example above, we have shortened our clear (that clears our command line) command to clr using the command alias. So when you type clr now, it calls the clear command and clears our command.

You can even shorten it down to c, to make it a one letter command. Isn't that cool?

Now if you run the command above, you would only be able to use it in your current command line and remains in effect only during the current login session. Once you log out or reboot the system the alias will be gone. You can fix this by making it permanent.

So to do this, you will need to define the alias command in your shell/terminal settings/config/profile file. Most system-wide aliases are usually defined in the /etc/bashrc file, but in some cases, you will still need to redefine it in your shell config file.

Alias command is built into various shells including ksh, tcsh/csh, ash, bash, and others. Below are files you should look for if you are using any of below shells:

  1. Bash: .bashrc, .bash_aliases
  2. Zsh: .zshrc
  3. Fish: .config/fish/fish.config
  4. Csh: .cshrc
  5. Tcsh: .tcshrc
  6. Ksh: .kshrc
  7. Ash: .ashrc

So for instance, if you are using the default bash, you can add the alias to your ~/.bashrc file, enter:

vi ~/.bashrc

Then you can now add the following line to the file:

alias clr='clear'

Save and close the file.

Now, reload the .bashrc file using the following command:

$ source ~/.bashrc

With this, you can now run clr anytime forever (as long as your system is alive). To remove an alias, you can either run the unalias command as below:

unalias <alias_name>
unalias -a # to remove all

Or remove the command from the file and reload it using source.

Command Alias Guidelines

Before you start adding new command aliases to your terminal, there are a couple of things you need to know and keep to. Aliases, in many ways, makes it easier and efficient to execute many tasks. But it can make you end up not remembering the actual command and options anymore or maybe creates conflict between the existing crucial system commands.

To avoid this, here is a general set of rules to abide by when creating a command alias:

  1. There should be some substantial shortening in terms of typing. Converting a four-letter command to two letters is hardly worth it. eg. No need to create an alias for ls as l.
  2. You should not create a separate alias for every possible command-line option. Do not just rename commands just because you can. For instance, do not rename ls to l.
  3. These should be commands that you (semi) regularly use, not something that you use very rarely. You will forget that you created an alias exist anyway.
  4. Beware of one letter aliases, you can accidentally set it off. Using something like r for the shutdown is a bad idea.
  5. Preserve the name of the command if possible. you are just adding options that you might forget. eg. cp -iv can be aliased to cp (and not CPV or c).
  6. Having said that, change or modify the command name slightly, if you also want to execute the original command without the specified options occasionally.
  7. If it is not a one-liner, try a shell script rather than an alias.

Popular Command Aliases

There are tons of command aliases you can create and it really depends on your creativity and the commands you used most.

Below are popular command aliases:

alias pl='pwd; ls'
alias dirs="ls -al | grep '^d'"
alias lf="ls -aFG"
alias lm="ls -al|more"
alias h="history"
alias clr="clear" # Clear your terminal screen
alias ip="curl icanhazip.com" # Your public IP address
# MS-DOS
alias dir="ls"
alias copy="cp"
alias rename="mv"
alias md="mkdir"
alias rd="rmdir"
alias del="rm -i"
# GIT
alias ga='git add'
alias gaa='git add .'
alias gau='git add --update'
alias gb='git branch'
alias gbd='git branch --delete '
alias gc='git commit'
alias gcm='git commit --message'
alias gcf='git commit --fixup'
alias gco='git checkout'
alias gcob='git checkout -b'
alias gcom='git checkout master'
alias gcos='git checkout staging'
alias gcod='git checkout develop'
alias gd='git diff'
alias gda='git diff HEAD'
alias glg='git log --graph --oneline --decorate --all'
alias gld='git log --pretty=format:"%h %ad %s" --date=short --all'
alias gm='git merge --no-ff'
alias gma='git merge --abort'
alias gmc='git merge --continue'
alias gnit='git init'
alias gp='git push'
alias gpl='git pull'
alias gpom="git push origin master"
alias gpr='git pull --rebase'
alias gr='git rebase'
alias gs='git status'
alias gss='git status --short'
alias gst='git stash'
alias gsta='git stash apply'
alias gstd='git stash drop'
alias gstl='git stash list'
alias gstp='git stash pop'
alias gsts='git stash save'
# NPM
alias ni='npm install'
alias nis='npm install --save'
## get rid of command not found ##
alias cd..='cd ..'

## a quick way to get out of current directory ##
alias ..='cd ..'
alias ...='cd ../../../'
alias ....='cd ../../../../'
alias .....='cd ../../../../'
alias .4='cd ../../../../'
alias .5='cd ../../../../..'
alias path='echo -e ${PATH//:/\\n}'
alias now='date +"%T"'
alias curdate='date +"%d-%m-%Y"'
## Browsers ##
alias firefox='/opt/firefox13/firefox'
alias chrome='/opt/google/chrome/chrome'
alias opera='/opt/opera/opera'

#default ff
alias ff=firefox

#default browser
alias browser=chrome
## pass options to free ##
alias meminfo='free -m -l -t'

## get top process eating memory
alias psmem='ps auxf | sort -nr -k 4'
alias psmem10='ps auxf | sort -nr -k 4 | head -10'

## get top process eating cpu ##
alias pscpu='ps auxf | sort -nr -k 3'
alias pscpu10='ps auxf | sort -nr -k 3 | head -10'

## get GPU ram on desktop / laptop##
alias gpumeminfo='grep -i --color memory /var/log/Xorg.0.log'

Conclusion

Command Aliases is a great way to reduce the time spent on typing and remembering commands. It increases your productivity on the shell/terminal. However, with the goodness of aliases, you should not just create any command alias just because you can and override other commands.

If you enjoyed this, share.