I have been doing quite a bit of development work recently on CentOS and Debian systems, I am sure like me you will find yourself endlessly typing the same commands over and over again in your SSH terminal. Sure you can just go up through your BASH history or copy and paste the command, however it’s a bit messy.
Say hello to the great command Alias, this allows you to define a word to use in your bash prompt that will run a command, a kind of macro/shortcut for the world of bash.
Example
Here is a simple example; while developing PHP I like to get a quick view of my error.log on the live webserver so I can proactively go through and fix any problems, the below command accomplishes this and also filters to show just logs form my IP.
tail -f /home/beta/log/error.log | grep 1.2.2.3 2012/05/31 21:32:06 [error] 9777#0: *1055599 connect() failed (111: Connection refused) while connecting to upstream, client: 1.2.2.3, server: beta.route.im, request: "GET /robots.txt HTTP/1.1", upstream: "http://127.0.0.1:8000/robots.txt", host: "beta.route.im" 2012/06/01 05:53:37 [error] 9777#0: *1059307 connect() failed (111: Connection refused) while connecting to upstream, client: 1.2.2.3, server: beta.route.im, request: "GET /robots.txt HTTP/1.1", upstream: "http://127.0.0.1:8000/robots.txt", host: "beta.route.im"
However I would much prefer if I could just type a single word to see the same output, lets use betalog, here is the alias command:
alias betalog="tail -f /home/beta/log/error.log | grep 1.2.2.3"
Great, so now look at my output 🙂
betalog 2012/05/31 21:32:06 [error] 9777#0: *1055599 connect() failed (111: Connection refused) while connecting to upstream, client: 1.2.2.3, server: beta.route.im, request: "GET /robots.txt HTTP/1.1", upstream: "http://127.0.0.1:8000/robots.txt", host: "beta.route.im" 2012/06/01 05:53:37 [error] 9777#0: *1059307 connect() failed (111: Connection refused) while connecting to upstream, client: 1.2.2.3, server: beta.route.im, request: "GET /robots.txt HTTP/1.1", upstream: "http://127.0.0.1:8000/robots.txt", host: "beta.route.im"
One final thing to remember
Alias commands only work in the current bash session, therefore when you close your SSH window they will be lost, fortunately it’s fairly easy to save them for use at a later date you just need to save it in a user profile.
Normally this will be in your home directory called ‘.profile’, on my debian system that actually referances another file in my home directory ‘.bashrc’.
cd ~ nano .bashrc
Finally these alias commands are stored in the user profile, which if you don’t already know means they will be only available for the user with that profile in their home directory.
Hope that helped, any questions please leave in the comments below :), I’m going back to coding some more!