If you use the Terminal in Linux/Mac a lot, then you know how much trouble it can be entering the same exact command over and over again. For example, I use Python a lot and I have a directory (folder for Windows users) that I store my Python Scripts in, but its a few levels into my folders, so I had to type this big, long command
and it got tiresome. The fix? Create an alias.
While you can create temporary ones that only work until you close the terminal window, if you plan to use it over and over, you need a little more permanent solution and here’s how:
NOTICE: This has only been tested on Linux, Mac does not work by following this tutorial exactly, though there may be a work-around avaliable
Open up terminal and enter this command
sudo gedit ~/.bash_aliases
This will, after you enter your password, open up gedit and display the bash_aliases file. It should look something like this:
You can see where I have added my own aliases for my python scripts and programs I wrote and wish to quickly access (the lines that say #Quick alias for …)
`Ok already` you’re saying. Enough with all the jibber-jabber and get on with the tutorial. Okedoke, I will.
For this tutorial I will make a command that makes sure you want to remove a file instead of just deleting it willy-nilly.
How to is as follows:
- Run the
sudo gedit ~/.bash_aliasescommand to load the bash_aliases file into gedit so you can edit it
- Basically choose anywhere in the file to type, but towards the top makes more sense and enter this
#This is the beginning of the comment that will tell what this alias is for
- Next, type in
This alias is a command that makes sure I want to remove (rm) something. This is the comment about your alias
- Press enter and type this
alias. This is the beginning of the alias programming.
- Type in
rm. This text is the command (alias) we are creating. This command will replace the current
="rm -i"after the
rmtext. This text we just entered is what the command (alias) will do when we enter it. It will run the real rm command, but with the -i (interactive) option attached. This will make you confirm the deletion (interactively) instead of just deleting it, perhaps accidentally deleting a file you really needed.
- Save the file and then exit gedit and then restart the BASH Terminal. Once you start it again, our new command will work and you will have successfully made your first permanent alias!
As you can see with my custom aliases, I added a comment explaining what each one does by placing a # before the comment text. While you don’t have to do it, it’s a good idea to, as it explains to you (and anyone else who might be using your system), what it does. Once you have (or haven’t) added the comment you can get into the programming for the alias.
[alias-name] is the command you want to create (eg. scripts)
[command] is the BASH command that your alias will call
Once you do this, save the file and exit. Don’t try to use your new command yet, you have to restart the shell first, so close the Terminal window and open it again. Once the Terminal has been restarted you can now use your command