Saving time with Aliasing in Unix

This is a quick post about some wonderful time-saving functionality you can add to your Linux or macOS machine (sorry Windows) when you find yourself using the terminal often.

Especially if you ssh into various machines frequently, whether it be for accessing and working with data, maintenance, problem solving because your docker swarm isn't behaving properly, or any other of a multitude of reasons, you likely know how arduous it is to find or type out the appropriate ssh script every single time.

Aliasing commands on your machine is the simplest way to automate these tasks and speed up your workflow, taking commands like

ssh -L 1010:127.0.0.1:1010 username@100.1.100.100 -p 10

and replacing them with something as simple as typing work.

Set up Aliasing

First, open up your preferred shell. We'll be using bash for this guide.

Next, find the configuration file for your shell. For bash, this will be .bash_profile.

Open up with file with your preferred editor. We'll use nano for simplicity:

nano ~/.bash_profile

There may already be content in this file left behind by other installers (Anaconda, for example) but you don't need to worry about any of it.

Scroll to the bottom of the file and on a new line, place your alias. The syntax for this is:

alias <yourcommand>='<full command>'

In practice, this looks something like:

alias cb='cd ..'

Or, in the case of an ssh command:

alias work='ssh <yourUsername>@100.100.100.100 -p 10'

You can call your aliases whatever is most convenient for you, but ensure that your alias is one word and does not interfere with other bash commands.

Next, save the file by writing out with ^O, then exit nano with ^X. Lastly, restart your terminal or open a new terminal window. To test that you have succeeded in creating an alias, type alias into the command line and you should see your newly-created alias listed.

From here, just type the alias you created anytime you need that specific command and enjoy zooming around your terminal!