What is an Alias?
An alias in Linux is a way to assign a ‘nickname’ to a command or set of commands. For example, instead of typing in ‘cd my_very_long_directory’, you could assign ‘golongdir’ to that command. Then every time you type, ‘golongdir’, you will run that cd command. If you put the alias command into .bashrc, it will load every time a terminal is started.
Windows can do something similar – see this link.
So What’s the Big Deal?
Suppose you work on several different projects at work – several different directories that are checked out from version control. You can immediately get from one of those projects to another with an alias. For example…
alias goproj1="cd /home/myuser/workspace/projects/proj1"
If you have several aliases like this one, you can swap to several different projects quickly.
Let’s take this a step further.
Some Other Bashrc Tricks
I have to interact with many different servers at work. In Windows, many use Filezilla for file transfers and putty for SSH. In Linux, I set up some variables and bash functions in bashrc to help shortcut SSH and SFTP. For example, rather than typing ssh username@ip address, I set the IP address to a variable and use sshpass. This prevents me from having to type in a password every time (I would use SSH keys if I were using SSH over the Internet – this is strictly internal). So rather than running an SSH command and typing a password, I type in one command and am immediately connected to the server.
In a similar manner, I have a function for scp and sftp. One command copies a file to a remote server, then SSHs into that server and runs a command to restart a web server. This would involve…
- running SCP
- entering a password
- running SSH
- entering a password
- running SSH once again on that server to another device
- entering a password again
- running a command on that second device.
The SFTP even reads the current directory and determines the proper directory to cd into automatically once the connection is made. This allows me to quickly connect to a server and be in the proper directory to transfer files.
Take the Extra Hour to Save Several Minutes in the Future
Setting up shortcuts for commands you often use can take a little bit of time. Perhaps it takes an hour or two. But consider the trade-offs. How many times do you use that command? These commands have saved me hours over the number of times I have used them. Work a little smarter and don’t repeat yourself over and over across weeks/months/years. Use shortcuts/bash functions to help make you a more efficient developer over time. Don’t waste time changing to the same long directory hundreds of times over the course of a year or any other long command that you use often.