Aliasing Commands in Terminal

Automation comes in all shapes and sizes and sometimes it is the really little things that can make a big difference.

Do you find yourself using the same long commands in terminal over and over again? Do you find that you have to press UP through your bash history to find that command you used yesterday because you can't remember it exactly or want to save time not typing it out?

It sounds like you need to set up an alias command.

This neat little feature assign a single word to an entire command. It doesn't take much to set up and can save so much time in the long run.

There are a few ways of doing this, I will start with on Ubuntu.

In your home directory you should find .bashrc file.

Open it and check if the following lines are there:

if [ -f ~/.bash_aliases ]; then . ~/.bash_aliases fi

If they are not, add them.

The .bashrc is loaded up when the terminal starts and what this line does is load up a file called .bash_aliases if it exists. You could add your aliases directly in .bashrc but it is considered better practice to keep them separately.

vi .bash_aliases or whatever you named the file in the lines above and add your alias. In my example, I find myself needing to export a database using MySQL dump and you can not auto complete it.

alias exportDatabase="mysqldump --opt -u user -ppassword database > database.sql" Save the file and then restart the terminal.

You can now run exportDatabase and the above command will take place. Simple! It is quicker to type (and with auto-complete, even faster) and easier to remember.

On a Mac, you should be able to go straight to making a file called .bash_profile in the home directory and adding your aliases there. Again, you need to restart the terminal after you save. If it doesn't work, you may need to check .bashrc that the lines above are present and if not, add them.

You can add as many aliases as you like, just need to add new lines.

Advanced Stuff

Now that you are compressing entire commands into a single line you may be asking yourself, "Can I carry out several commands using a single command?" The answer is Yes.

Going back to my database example. Let's say that after doing the export, I want to copy it from where I run the command to a specified folder and unlock all privileges (I know that I can specify a path in the original command, but I need multiple commands for the example).

All you have to do, is separate your commands with semi-colons like so:

alias exportDatabase="mysqldump --opt -u user -ppassword database > database.sql; chmod 777 database.sql; cp database.sql ~/Desktop/exports/database.sql"

Now you are really saving time. 3 commands compressed into 1. 129 keystrokes compressed into 15 (including enter).

Now lets make things even more interesting...

Say that you had a second database with a different name. OK, you add another line in your alias file.

alias exportDatabase="mysqldump --opt -u user -ppassword database > database.sql; chmod 777 database.sql; cp database.sql ~/Desktop/exports/database.sql" alias exportDatabase2="mysqldump --opt -u user -ppassword database2 > database2.sql; chmod 777 database2.sql; cp database2.sql ~/Desktop/exports/database2.sql"

Are you going to have to remember to add another line for every new database that you build? We can turn these command into a function like so:

exportDatabase() { mysqldump --opt -u user -ppassword $1 > $1.sql chmod 777 $1.sql cp $1.sql ~/Desktop/exports/$1.sql }

$1 represents the first argument that you pass the function. So now you can run:

exportDatabase database and the function will run with $1 being database. If you wanted, you could change the username and password to be $2 and $3 to pass those in as arguments and make it truly flexible (and a bit more secure). You could also add some error handling so that it doesn't attempt to perform actions if previous commands fail.

So there you have it. A small part of your life now automated and made a tiny bit easier.


© 2012-2017