Command Alias in Unix and Mac

Create shortcut for your command(s) and reduce the time you spend while typing long ones

Are you still using those long cd /path_to_directory command to go to a particular directory or log in to your server using the ssh command? You might have thought of creating a bash script to simulate your frequently required commands and then run it every time you need to perform those tasks.

You have a handy command or shall I call it a tool :) which will convert your lengthy commands into one single command with your custom name. You can make an alias of a command by adding it to your ~/.bash_profile file.

So what is this .bash_profile file? It is the file located in your home directory. cd ~ is your home directory. This file performs the task of loading shell (terminal) defaults, options, aliases, the prompt, and functions when the shell first starts. 

What is alias command? Alias command let you define your own commands or command shortcuts, so you can customize the command line, and make it work the way you want it to work. For example, every time you want to login into your remote server you will use the ssh command as below

ssh -i /Users/jdpawar/sshcommands/keys/ws/key root@

In order to avoid typing this command, you can add it to the end of your .bash_profile.

Adding Alias Command

alias server='ssh -i /Users/jdpawar/sshcommands/keys/ws/key root@'

What this will do is, it will load the command wscom each time you open your terminal. So now when you type server then your ssh... command will be executed and you'll be logged in.

Same can be applied to got to your favourite directory

alias favdir='cd /Users/jdpawar/my_fav_dir_path'

To get the list of all the alias defined, just type in alias in the terminal and hit enter.

List Alias Command

Now that you know how to create alias/shortcuts of your daily commands, enjoy the time saved!