"In the beginning was the command line" is not the just name of a great book by Neil Stephensen (read the text here), but a way of life for many Linux users. The command line is the tool for just about any level user. However, the power user takes the command line to newer and many different heights and does so in many different ways.
One of those ways is with the help of bash aliases. A bash alias is a way to create shortcuts to commands that would normally take a lot of typing or are a challenge to remember. So instead of ssh -v -l jlwallen 192.168.1.10 I could enter just desktop or whatever I want that shortcut to be. In that vein, I am going to offer up some very handy shortcuts for you to add to make your command line usage more efficient.
Where they go
If you open up a terminal and issue the command ls -a |less you should come across a file called ~/.bashrc. This file is very powerful and handles a LOT of tasks. One such task is that of aliases. Within that file you will find the section:
# Alias definintions
This is where you put your user-created aliases...and where we will place the aliases listed below. Once you create the aliases, you will notice they don't work within the same terminal you used to add them with your text editor. You have to fire up a new terminal to make sure they work. Because of this, always leave your original terminal open to make sure the new terminal will still work. I have witnessed a user fubar their bash such that the terminal would no longer open. So use caution when randomly closing your terminals. Now, on the aliases.
Ask before you remove
Admit it, you've accidentally deleted a file that could have been prevented if you had used the -i switch with the rm command. To avoid this common mistake, let's add an alias such that any time the rm command is issued, it is done with the -i switch. This alias would look like:
alias rm="rm -i"
As I mentioned earlier, secure shell'ing to a machine can be a real pain when you do it over and over. Create an alias like so:
alias server1="ssh -v -l USER ADDRESS"
Where USER is the user name you would log in with and ADDRESS is the address of the machine you are remoting into.
Here's a fun one. You can open up your browser to a specific bookmark, from the command line with an alias like so:
alias ghacks="chromium-browser https://www.ghacks.net"
Of course you would replace chromium-browser with your default browser and the URL with the address you want the browser to open up to.
RPM batch installation
Say you do a lot of batch installations of RPMS. And say you always save your RPM files to ~/RPMS. You can add an alias to quickly install those RPMS like so:
alias brpm="rpm -ivh ~/RPMS/*rpm"
Update/upgrade with apt
Instead of having to issue both command for updating apt and upgrading your installation, combine them into one easy to use alias like so:
alias update="sudo apt-get update ; sudo apt-get upgrade"
You will have to enter your sudo password once and the update/upgrade will take place.
Bash aliases are only limited to your imagination. After you spend enough time with the command line you will see that these aliases can really make your day to day Linux life much easier.Advertisement
Advertising revenue is falling fast across the Internet, and independently-run sites like Ghacks are hit hardest by it. The advertising model in its current form is coming to an end, and we have to find other ways to continue operating this site.
We are committed to keeping our content free and independent, which means no paywalls, no sponsored posts, no annoying ad formats or subscription fees.
If you like our content, and would like to help, please consider making a contribution:
Ghacks is a technology news blog that was founded in 2005 by Martin Brinkmann. It has since then become one of the most popular tech news sites on the Internet with five authors and regular contributions from freelance writers.