Installing Homebrew for all your OSX command line goods

Getting your source on is a whole lot easier when using a package manager, for linux you might use Yum or APT, but on OSX Homebrew is my goto. It’s super easy to install, but to get full benefits and access to more software you will need to have an install disc or have the developer tools already installed. If you don’t you can download it from apple. Google it or comment if you need to know what I’m talking about.

Installing Homebrew on OSX in the Terminal

Installing Homebrew takes 5 seconds

Here’s how you install Homebrew:

ruby <(curl -fsSkL raw.github.com/mxcl/homebrew/go)

of course using ruby is awesome, because it's installed by default on everyone's machine and has read write. Also notice that it's not using wget, which you won't have on a clean install w/out the dev tools. Underneath brew is pretty much an alias to GIT with some cool ruby helpers.

Once you have brew installed you can do things like:

brew install mongodb

Command Z for developers

If your using GIT, your going to love this:

git reset HEAD~1

That will take you back to your last commit while leaving your working directory (files) alone. This is great if you make a commit and want to fix something, you would just do git reset HEAD~1, make your chages and then add and commit as regular.

Gitting Advanced

# Roll back one commit leaving the working directory intact
git reset HEAD~1

# Roll back your working directory to the last commit
git reset --hard HEAD~1

# Keeps the files AND index (what's been added for commit)
git reset --soft HEAD~1

One more thing…

Need to go back to a specific commit? Just use the commit’s SHA hash instead of HEAD~1, like so:
git reset --hard 9748bc01c6ef45ef7f824c6f2bd6052f0d8b9691

Protip: You can find your commit hash using git log.

My Favorite OSX Software

My first computer was the black pismo laptop, with the removable and interswappable batteries and CD-RW drives, it actually looks remarkably similar to my current MacBook. Soon I was using AOL to connect to the internet through our home phone and downloading scenarios and mods for my then favorite computer game, Command & Conquer. Over the years I have seen software evolve, many trends emerge, and ideas radically change the way software was not only used, but created as well. Anyways, what you’ve all been waiting for, my favorite OSX software!

Billings

The best looking invoicing solution I have ever witnessed. This app is full featured and works very well for a freelance contract worker. It supports multiple currencies, tax rates, gas milage, invoicing, statements, time tracking, and more. If you want to keep records of your billing with a breathtaking UI and workflow, it’s hard to beat Billings.

FileMaker Pro

My mom would be happy to know that I am including FileMaker Pro. She developed software using FileMaker for many years and introduced me into the world of programming and database design. FileMaker is a solid foundation for any type of business software you could imagine.

Sequel Pro

I used to use phpmyadmin, until I discovered Sequel Pro. Now I view my MySQL databases in a native cocoa app that is very thoughtfully designed and full featured. If your a web designer or developer I would not wait any longer, and just download it. You will thank me I am sure. Oh you really want to thank me? How about following me on twitter and finding something you really can’t resist retweeting.

TextMate

My favorite text editor for many years, even though I switched to Sublime Text 2, I recently reverted back to TextMate. The clean UI, lightning fast performance, and the huge array of helpers and tools called Bundles, it’s hard to beat. And ST2 has an ugly icon, and I can’t have an ugly icon in my dock.

Terminal

The only app included in my list that is a preinstalled app, the Terminal is the window into the system that powers your computer. It is based on unix and your terminal is by default a bash shell, so any commands that work on *nix bash should probably maybe work on your mac. It took me a long time to start using command line for common tasks, but now that I do it is a huge timesaver. Take for instance the common task of navigating to a specific file. Instead of 5 clicks and the time spent waiting for the UI to render, I type open /where/I/want/to/go and I’m there. It’s also the place where I can run…

GIT

If your not using version control or backing your stuff up, your not a professional. While using version control is not essential to building software, it definitely can save you and your teammates a lot of time and hassle and more importantly save you from a serious loss of data. Using GIT alone is not enough however, you need to backup your repo’s remotely, and I recommend BeanstalkApp for just that.

OSX Screenshot Utility Shortcuts & Hacks

Mac users have native screen capturing abilities through hotkeys or the Grab utility, underneath these are based off the screencapture software that ships with OSX.

If your like me you take multiple screenshots a day. Over the years I have collected a few helpful tricks that I would like to share with you.

Basic Commands:

Save full screen to a file:             ⌘ ⇧ 3                                         
Save selection to a file*               ⌘ ⇧ 4 + Click + Drag                      
Save selected window  to file           ⌘ ⇧ 4 + Spacebar + Click Camera               

Save full screen to clipboard           ⌘ ⇧ 3 + Ctrl:                                 
Save selection to clipboard*            ⌘ ⇧ 4 + Click + Drag + Ctrl:                  
Save window to clipboard                ⌘ ⇧ 4 + Spacebar + Ctrl + Click Camera        

*The following keys can be held down while selecting an area:

Spacebar, to lock the size of the selected region and move it with the mouse
Shift, lock either the width or height of the selected region
Option, to resize the selected region with its center as the anchor point

Going under the hood

We can change some defaults used by screencapture easily through the terminal by entering these commands.

# Save screenshots to a new location (My personal choice included)
defaults write com.apple.screencapture location ~/Pictures/Screenshots/

# Save screenshots in new format (other options: BMP, GIF, JPG, PDF, TIFF)
defaults write com.apple.screencapture type -string "png"

# Disable shadow in screenshots
defaults write com.apple.screencapture disable-shadow -bool true

# To enable any changes you must enter this
killall SystemUIServer

More command line geekery

You can take advantage of screencapture directly from the terminal as well, here are some commands to get you started.

# Take a screenshot and store it the current terminal directory
screencapture example.jpg

# You can change the place to store the file like so
screencapture ~/Desktop/screenshot.jpg

# The -x removes the shutter sound while the screenshot is taken
screencapture ~/Desktop/screenshot.jpg -x

# -h stands for help and will show you all the available options
screencapture -h

Have a lot of screenshots piling up?

This shell script will take your files, back them up, and then organize them into folders by day. Don’t know how to use a shell script? Your in luck, I just wrote a nice tutorial on creating and running your first shell script. Know a lot about shell scripting? Help me make this code better.

tar -czf backup.tar.gz *

for f in *; do
    dir=$(stat -f%SB -t%Y-%m-%d "$f")
    echo $f '->' $dir
    [ -d "$dir" ] || mkdir "$dir"
    mv "$f" "$dir"/
done

Once installed and named cleanup you can run the shell script via terminal like so:

cd ~/Pictures/Screenshots/
cleanup

Creating Your First Shell (Bash) Script

If you have used a mac for a long time and never opened up the terminal, you are really missing out on a productivity workhorse. The power comes from the simplicity. At it’s core the command line is all about typed input. However, if you find yourself using the same commands over and over, you could really crank up the geek by making your own shell scripts. Here is how to do just that.

Make a spot for your scripts

Finding a place for your scripts is wise so you can keep them separate from system scripts. I keep mine in my home folder in a folder called .bin (Yes, it’s hidden).

Create the folder like so:

mkdir ~/.bin

Next your going to need to set up the path so your shell can find your scripts, you can do that by adding the path to your .bash_profile.

Here is an example using the nano editor in OSX, but feel free to use any text editor you want. Nano is a great command line editor because it is almost everywhere and has a low learning curve unlike vim.

nano ~/.bash_profile

And place this line at the top:

export PATH=$PATH:~/.bin

Close and save the file by pressing ctrl-x (Looks like ^X) and choosing Yes to save as the same name. (Press y and then enter, for the type first read later people)

Create the script and set permissions

Now we need to create a script to live in his/her new home. To keep things simple and make sure the above steps worked we will just do the standard “Hello World” test. Anything following the ‘#’ is a comment and is not ran.

cd ~/.bin # Make sure we are in the right spot
touch hello # Creates a script named hello
chmod 700 hello # Makes executable "Off With His Head"
nano hello # Opens in nano editor

In nano copy any paste the following:

!#/bin/bash
echo Hello $1 

The first line is saying this is a bash script, which can be found at /bin/bash.

The second line is are actual script, the only special thing about it is the $1 sign which is a variable for any input following the script. You could have multiple variables in chronological order by using $2, $3 and so on.

Run the script like a boss

Now we should be able to run the script to desired effect by typing the following:

hello World

If you get any errors make sure your script is in the right place by typing:

cd ~/.bin # go to the spot
ls # show files, it should output back hello

If it is in the right place set the permissions with:

sudo chmod 700 hello # sudo will prompt you for an admin password

Finally check to make sure your path is set correctly:

nano ~/.bash_profile

In the spirit of don’t repeat yourself, lets make a script to make making scripts easier. We will call it ‘makescript’.

cd ~/.bin
touch makescript
chmod 700 makescript
nano makescript

Enter the following and save the file:

#!/bin/bash
cd ~/.bin
touch $1
chmod 700 $1
echo $1 > \#!/bin/bash

Now you can use the following to create any scripts you like:

makescript nameofscript

10 OS X Terminal Commands

Aside

I was trying to flush my DNS cache on my apple since my host rerouted the IP address and found this page in the process: ten-os-x-command-line-utilities-you-might-not-know.

My favorite ones on the list are definately TOP & screencapture, however I never knew about lipo or say.  Screencapture is definately useful for taking screenshots of my websites to email to clients.

Source:

Go to OSX Daily Website

Go to OSX Daily Website