Wednesday, June 22, 2016

My Transition To UNIX From DOS: Working With Aliases

Background


It's been fun playing on a Windows OS machine as a Software Engineer. I always find it interesting and fun to explore other things. Funny enough though, I have always been scared of using commands because I always felt that I would mess up something on my machine and have to do a factory reset of some sort. It's always a drag when one has to do that on their work machine. Finally I landed at a place where the recommended OSes were the unix based ones, preferably, Linux Ubuntu or Macintosh OS. Ultimately I went for the Macintosh OS, just because... 

From my first day there I had to learn some basic and common unix commands from the likes of: tail -f /file/path/here up to, ssh username@server.ip.address.here. At some point I got a bit fed up with typing a long and same old command everyday and to a point, every hour even. Even though it was great practice, something had to be done and that's when I learned about Unix aliases. Aliases are pretty much abbreviations of FUCs "Frequently-Used Commands" 

For example one can shorten a command like: defaults write com.apple.finder AppleShowAllFiles NO, which toggles hidden files & folders off, in Mac OS's finder, to something like show-hidden-files which will essentially be the name "Alias" of that command. 

Now to the "Nuts & Bolts", we will look at the following:
  1. Creating / Adding a new alias
  2. Referencing an external file for aliases.
We will also need some "shell scripting skills", nothing major, very few lines of code so that's all fun and well

1. Creating / Adding a new alias

Perhaps this may be slightly different on other unix platforms, so this is more pertinent to Mac OS. Let's go...!
  • Open finder and head to your home directory. (Unless you are in your home directory by default) 
  • Look for a file named, ".bash_profile". Notice the dot before the file name. If you cannot see hidden files then use the command (in your terminal)  from the example above and just change that "NO" to "YES". Then hold down the "alt" button and then "alt-click (right-click)"  on the finder icon and then "left click"  the option "Relaunch".
  • Go back to looking for our file and then open it in any of your favourite text editor... (Sublime Text, Atom, even VI on your terminal etc ...)
  • Let's add a new line, this will be a new alias for any command you want to add, for example:  

  alias show_files_no="defaults write com.apple.finder AppleShowAllFiles NO"
  


  • To delineate things a bit more, "alias" is a reserved key word for shell, so you are letting shell know that you want to add a new alias, the next word "show_files_no" can be anything you want, this is the actual name or alias of your command. Then last part "defaults write com.apple.finder AppleShowAllFiles NO" is the actual shell command that you would normally type and execute on the terminal. We are almost done. 
  • If you were editing the file using the terminal with VI, VIM, Nano etc... The next thing is to reload the ".bash_profile"  by either closing and opening the terminal again or typing the command "bash -l". I believe there may be more commands out there that one can use. 
  • Finally open terminal if you closed it. If you reloaded the ".bach_profile"  using the command then just type the alias you added recently, in our case just type "show_files_no". This will execute the command associated with that alias. To see if this made an effect just follow the steps about relaunching finder above. That's it!

2. Referencing an external file for aliases.

I have recently been playing around with externalising some bash profile stuff because I have a lot of aliases and my bash profile artifact was just getting too congested. So main things to note here is that
... you don't have to create your new external file inside the same directory as your bash profile and the name of your file does not have to start with the word "bash"...
I just named it that way for the sake of naming it that way! So let's get to it:
  • Create a new file as follows, "~/bashes/.whatever_file_name", of which in my case, is inside a new folder that I created, "bashes", in my home directory and named it ".bash_aliases". Keep in mind that your folder can be named anything you want.



  • Now go back to your main file, ".bash_profile" and then replace your alias with the following shell scrip code.

  #Referencing path to file containing the aliases
  aliasesPath=~/bashes/.bash_aliases 
  if [ -f $aliasesPath ]; then 
     source $aliasesPath
  fi  
  


  • Something along those lines should help you out. It's basically a shell if statement that checks if that file path exists and if so we then reference it from the main ".bash_profile" so next thing you should try is to now reload like we did earlier if you did all this using terminal or just close and then open terminal.  
So now you have externalised the aliases and you can try with other stuff like your environment variable profiles can also be externalised and so forth. Like I said this is the first main thing I learned when moving to UNIX, I hope it helps someone out there. I would really love to learn from you on how I can improve this post and some feedback, Cheers! 

2 comments:

  1. That is a good explanation on how to create and also externalise your aliases. I would also encourage that you make another post on how you can do it on the windows power shell. That is a cool blog post.

    ReplyDelete
    Replies
    1. Thanks for your feedback are you referring to something like this : http://informingtechies.blogspot.com/2020/01/setting-up-custom-powershell-scripts-on.html

      Delete