Wednesday, January 28, 2015

Mac OSX Shell tips and tools

bash_profile tuneup


To let bash return "user@hostname:path/to/directory$" as your prompt, add the following line to your ~/.bash_profile:
export PS1='\u@\H:\w$'
or
export PS1='\u@\H:\w$ '
alias www='open -a "Google Chrome"'

if you like having a space between the $ and the command
to make the changes take effect immediately, run the following command in every open window (or restart Terminal):
source ~/.bash_profile

~/.bashrc

a little script that extracts archives
extract () {
   if [ -f $1 ] ; then
       case $1 in
           *.tar.bz2)   tar xvjf $1    ;;
           *.tar.gz)    tar xvzf $1    ;;
           *.bz2)       bunzip2 $1     ;;
           *.rar)       unrar x $1       ;;
           *.gz)        gunzip $1      ;;
           *.tar)       tar xvf $1     ;;
           *.tbz2)      tar xvjf $1    ;;
           *.tgz)       tar xvzf $1    ;;
           *.zip)       unzip $1       ;;
           *.Z)         uncompress $1  ;;
           *.7z)        7z x $1        ;;
           *)           echo "don't know how to extract '$1'..." ;;
       esac
   else
       echo "'$1' is not a valid file!"
   fi
 }

No more cd ../../../.. but up 4

up(){
  local d=""
  limit=$1
  for ((i=1 ; i <= limit ; i++))
    do
      d=$d/..
    done
  d=$(echo $d | sed 's/^\///')
  if [ -z "$d" ]; then
    d=..
  fi
  cd $d
}



Since I use so many different machines, my .bashrc always sets the command prompt to include, among other things, the name of the server I am currently logged into. This way, when I am three levels deep in telnet/ssh, I don't type the wrong thing in the wrong window. It really sucks to rm -rf . in the wrong window! (Note: At home, telnet is disabled on all machines. At work, ssh is not always enabled and I don't have root access to very many machines.)
I have a script ~/bin/setprompt that is executed by my .bashrc, which contains:
RESET="\[\017\]"
NORMAL="\[\033[0m\]"
RED="\[\033[31;1m\]"
YELLOW="\[\033[33;1m\]"
WHITE="\[\033[37;1m\]"
SMILEY="${WHITE}:)${NORMAL}"
FROWNY="${RED}:(${NORMAL}"
SELECT="if [ \$? = 0 ]; then echo \"${SMILEY}\"; else echo \"${FROWNY}\"; fi"

# Throw it all together 
PS1="${RESET}${YELLOW}\h${NORMAL} \`${SELECT}\` ${YELLOW}>${NORMAL} "
This script sets the prompt to the host name followed by :) if the last command was successful and :( if the last command failed.
do sudo, or sudo the last command if no argument given
s() { # do sudo, or sudo the last command if no argument given
    if [[ $# == 0 ]]; then
     sudo $(history -p '!!')
    else
     sudo "$@"
    fi
}

# Show me the size (sorted) of only the folders in this directory
alias folders="find . -maxdepth 1 -type d -print | xargs du -sk | sort -rn"

# some more ls aliases
alias ll='ls -alh'
alias la='ls -A'
alias l='ls -CFlh'
alias woo='fortune'
alias lsd="ls -alF | grep /$"

# This is GOLD for finding out what is taking so much space on your drives!
alias diskspace="du -S | sort -n -r |more"
 

To format your PATH variable for easy viewing, add following code to your bash startup file (such as ~/.bashrc or ~/.bash_profile) :

function path()

old=$IFS 
IFS=: printf "%s\n" $PATH 
IFS=$old 
}

Now just run path:
$ path
Output sample:

/usr/local/sbin 
/usr/local/bin 
/usr/sbin /usr/bin 
/sbin 
/bin 
/usr/bin/X11 
/usr/games

 

Home shortcut in Finder


⇧⌘C: Computer
⇧⌘H: Home  --> Then drag and drop Home Icon to Favorites
⇧⌘D: Desktop
⇧⌘K: Network
⇧⌘I: My iDisk
⇧⌘A: Applications
⇧⌘O: Documents
⇧⌘U: Utilities

Screen capture

  1. C. Specific application window:
    1. To capture a specific application window, press and hold Command-Shift-4 then tap on the Spacebar. The cursor will change to a camera, and you can move it around the screen. ...
    2. To copy a specific application window, press and hold Command-Control-Shift-4 then tap on the Spacebar.


4 Alternatives to the os x finder

Xfolders

If you’ve ever used a Mac FTP client like Transmit or Captain FTP, you’ll feel right at home with Xfolders. The dual window interface allows you to easily move and copy files from one place to another. There are also keyboard shortcuts and dedicated buttons for functions like copy, move, new folder, delete, and rename along with a menu for quickly accessing system utilities. Xfolders contains a built-in image browser and terminal port as well.
XFolders
XFolders

Feature Rundown from Developer Site:

  • Full integration of the Finder, thus support of all file operations from and to the Finder.
  • Drag & Drop between both filelists and the Finder.
  • Support for all important file operations.
  • Info dialoge for simply changing the file and folder attributes.
  • Intelligent path navigators for both file lists.
  • Bookmarks & manager for folders.
  • Direct access to importend system utilities.
  • Navigation with the keyboard ala Norton Commander.
  • Integrated, detailed Spotlight search.
  • Integrated image browser.
  • Integrated terminal.
  • Versatile search and compare possibilities.
  • Zip archive support.

Verdict

The dual window system is definitely a handy feature, though tabs would be nice. Most of the rest of the features (like keyboard shorcuts and image viewing) are already present in the Finder where, in my opinion, they are much more user friendly and “Mac like”.
As a graphic designer, I was particularly annoyed at how the image browser is a separate function contained in a different window. Leopard’s integration of images through large icon previews, quicklook, and coverflow is simply a much better system for browsing lots of images. Since it’s a free app, it’s definitely worth downloading to see if it functions for your particular workflow.
Price: Free Developer: Kai-Heitkamp Requires: Mac OS 10.4 or later

muCommander

muCommander’s interface is very similar to that of Xfolders. Strips of buttons run along the top and bottom of a dual panel window. However, muCommander takes this idea a bit farther by adding buttons/keyboard shortcuts for features like setting bookmarks, emailing files, showing a particular file in the Finder, etc.
muCommander
muCommander

Feature Rundown from Developer Site:

  • Virtual filesystem with local volumes, FTP, SFTP, SMB, NFS, HTTP and Bonjour support
  • Quickly copy, move, rename files, create directories, email files…
  • Browse, create and uncompress ZIP, RAR, TAR, GZip, BZip2, ISO/NRG, AR/Deb and LST archives
  • ZIP files can be modified on-the-fly, without having to recompress the whole archive
  • Universal bookmarks and credentials manager
  • Multiple windows support
  • Full keyboard access
  • Highly configurable

Verdict

Though very similar in concept to Xfolders, I found muCommander to be a bit more enjoyable to use. The feature set seems a bit fuller and the interface is a little brighter. However, most of the shortcomings I listed for Xfolders still apply. Since this app is also a free download I encourage you to give it a shot and tell us what you think.
Price: Free Developer: Maxence Bernard Requires: Mac OS 10.3 or later

Disk Order

Disk Order is a dual window file browser/FTP client with strips of buttons for basic features running along the top and bottom (sound familiar?). Just like the two previous apps there is support for single image viewing, file archiving, file compression/decompression, Terminal commands, and just about every basic Finder feature (copy, move, etc).
Disk Order does bring some innovation to the table though with an integrated iPod browser (although it doesn’t seem to support my iPod Touch) and a plugin system that leaves room for useful third party add-ons. My favorite feature is the ability to have multiple tabs. Dual windows is nice, but I always find myself opening ten different finder windows and shuffling them around my screen. Like in Safari, the use of tabs takes all this clutter and contains it in a compact and easily accessible area.
DiskOrder
DiskOrder

Feature Rundown from Developer Site:

  • Tabbed interface
  • Copy/Move/Delete/Link operations
  • Built-in Viewer (viewing html, rtf, mov, mp3, jpg, gif, tiff etc.)
  • Built-in Editor
  • Built-in FTP-client (create, upload, download, CHMOD, transfer mode, encodings, viewing files and so on…)
  • Multi-Rename Tool
  • Archives support (tar, gz, tgz, bz, bz2, tbz, zip)
  • Sophisticated Drag’n-Drop
  • Color Marking support
  • System Index Utilizing Search
  • Command Line
  • Plug-in architecture (Terminal window, Burn CDs, Zip, Unzip, Untar etc.)
  • Very usable interface (Eject buttons by volume names and FTP sessions, customizable toolbar, Drives panel)
  • Customizable main menu shortcuts
  • Two file selection modes (Mac native and Norton-Commander-like)
  • Compare Directories, wildcard selection

Verdict

Disk Order provides a decent all-in-one solution for file browsing. Though not drastically different from the previous apps, it has just enough unique features to set it apart from the others. However, at just shy of $30, you might be better off with one of the free alternatives if you’re on a budget.
Price: $29.95 Developer: LikeMac Group Requires: Mac OS 10.3 or later

Path Finder

Path Finder borrows its interface directly from the Finder. In fact, it feels more like a serious Finder upgrade or plugin than a separate application. Path Finder seamlessly integrates tabs and a dual window interface (only if you choose) into the normal Finder GUI. One of Path Finder’s most innovative features is “Drop Stack”, which allows you to grab files and throw them in a holding area while you navigate to the folder you want to put them into.
PathFinder
PathFinder

Feature Rundown from Developer Site:

  • Dual Pane File Browser
  • Drop Stack
  • Tabs & Bookmarks
  • Command Line tools
  • “QuickLook” support
  • Use Path Finder as your “Default File viewer”
  • Subversion plugin
  • Application Launcher
  • Size browser
  • Selection tools
  • File list filters
  • Integrated Stuffit Engine
  • Create and Convert Disk Images
  • Customize menu keyboard shortcuts
  • Smart Sorting

Verdict

This is where you’ll have to excuse my blatant favoring of one app over the others because in my opinion Path Finder puts all the rest to shame (including the Finder!). I see Path Finder as an open letter to Apple containing everything that the Finder should be. The folks over at Cocoatech have really done an amazing job creating an unmatched Mac file browsing experience that actually adds functionality as opposed to rearranging existing features.
Granted, $40 is a lot to pay, but if these features alone were integrated into a new version of OS X I dare say many of us would fork out the $40 without a second thought. There are a ton more features than those I’ve mentioned here. Do yourself a favor and watch this screencast so you can marvel at the goodness of a file browser that truly gets the job done.
Price: $39.95 Developer: Cocoatech Requires: Mac OS 10.4 or later