Monday, November 04, 2013

Terminal Alias commands on Mac

One of the little things I enjoyed on Linux was using alias commands on the terminal.  Alias commands allow for quick shortcuts to longer commands that you may want run in the terminal.

I'm including a few I use on the Mac.

In order to use a terminal alias, you need a .bash_profile.  The . makes it a hidden file.  A default Mac build likely doesn't have a .bash_profile.  If you've installed things like MacPorts then one may have been created already.  You can add to an existing file.

Open up terminal.  If you don't know how to open terminal, close the browser, go get some ice cream and don't come back.

In terminal type
cd ~
 - This takes you to your home directory

ls -al | grep .bash

 - This lists all the files starting with .bash.  Do you see .bash_profile?  If yes, then you can just edit that, if not it needs to be created.  That character between -al and grep is the pipe.  (Shift \).

To create a new one type
touch .bash_profile

In order to edit the file, you need to open a text editor of some sort.  I like vi but if you don't know what it is, don't use it.
Default TexEdit:
open -e .bash_profile

Have TextWrangler?
open -a /Applications/ .bash_profile

Add the following commands in to the file.

# reload your bash config

alias src="source ~/.bash_profile"

# CPU and Memory stats

alias cpu='top -o cpu'

alias mem='top -o rsize' # memory


alias flush="sudo killall -HUP mDNSResponder"

alias dns="cat /etc/resolv.conf"

# Get local IP info - removes loopback and static VMware fusion

alias ip='ifconfig | grep "inet " | grep -v | grep -v | grep -v'

# Get Local default gateway

alias gw="netstat -rn | grep default"

# Get VPN Routes when connected

alias vrt="netstat -rn | grep utun | grep UGSc"

# Get Public IP

alias pubip="curl"

Save the file.
The commands won't work right off the bat, you'll need to load/reload the profile.
"source ~/.bash_profile"
Once done, you can just use "src" in the future (the first alias) to reload the profile after you make additional changes.

The shortcut is the word after alias and before the =.  The real command is after the =.

One note, the Local IP info string strips out the loopback IP and my VMware fusion IP's.  You can find yours by just running a straight ifconfig to see what IP's you need to strip out.

Also, I found some of the commands on the Web, some I created.   Have fun.

No comments: