Tuesday, December 11, 2012

Mac OS X - Terminal Tricks


This article covers some useful tricks in the Mac OS X terminal. Search for hidden files, Switch between the 32 bit and 64 bit kernel, fixing kernel extensions, enabling NTFS support and more.

  1. Finding absolutely anything on your Macintosh
  2. How to tell if you’re running the 32 bit or 64 bit kernel in Mac OS X Snow Leopard
    1. Method 1
    2. Method 2
  3. How to switch between 32-bit and 64-bit kernel mode in Mac OS 10.6
    1. Method 1 - Startup key combination (for current startup only)
    2. Method 2 - On-disk setting (persistent)
  4. Delete all files anywhere on your Mac which match a search term
  5. Working with Kernel Extensions
    1. Load kext
    2. Unload kext
    3. Troubleshooting kext (Mac OS 10.6.2 and above)
    4. Change Ownership of kext
  6. Enable NTFS drive support in OSX Snow Leopard or Lion

Finding absolutely anything on your Macintosh

The Spotlight feature doesn't search the Library or hidden locations.

Aside from Spotlight feature, OSX also has a Find feature. Unlike Spotlight you can configure OSX's Find feature (CMD+F) look for system files by performing the following:

  1. Press CMD+F to open the Find window.
  2. Towards the left of the window, just above where the search results would appear, you'll a pull-down menu which says (by default)Kind. Click this, and choose Other.
  3. In the window that opens, scroll down, find, and check off System files.
  4. Click OK to close the window.
  5. Now, you can perform searches which will include System files in the results. To do this, select the following criteria for your search:
    System files include.

However, even this will not search all files on your Mac (i.e. items in hidden folders). To search all locations for a file, perform the following:

  1. Open Macintosh HD > Applications > Utilities > Terminal
  2. In the window that opens, type the following, then press Enter:

    sudo find / -name "*Bicycles*"

    ...where in-place of Bicycles you enter your search term.
  3. You will be presented with a list of file-paths to any items whose file name contains your search term. The search can take a long time to complete. When the command prompt is displayed again (the same one which initially appeared at the top of the Terminal window when you first opened it), you'll know the search has completed.

How to tell if you’re running the 32 bit or 64 bit kernel in Mac OS X Snow Leopard

Summary
Snow Leopard 10.6 is the first Mac OS X version to ship with a 64 bit kernel. While our Mac has to have a 64 bit processor to utilize that kernel, some 64 bit Macintoshes do not default to using the 64 bit kernel.

Note: Not all applications support the 64 bit kernel. When using applications which only support 32-bit kernel mode, be sure you are running your computer using the 32-bit kernel.

Method 1

  1. In the upper-left of the screen, select the Apple menu, and choose About this Mac.
  2. In the window that opens, click the More Info button.
  3. In the Contents pane on the left-hand side of the window that opens, select Software.

    On the right, under System Software Overview, if 64-bit Kernel and Extensions is set to No, you are running a 64-bit kernel.

Method 2

  1. Open Macintosh HD > Applications > Utilities > Terminal
  2. In the window that opens, type the following, then press Enter:

    uname -a
  3. If you’re using a 32 bit Kernel in Mac OS X, you'll see:

    Darwin iMac.local 10.0.0 Darwin Kernel Version 10.0.0: Fri Jul 31 22:47:34 PDT 2009; root:xnu-1456.1.25~1/RELEASE_I386 i386

    If you’re using a 64 bit Kernel in Mac OS X, you'll see:

    Darwin iMac.local 10.0.0 Darwin Kernel Version 10.0.0: Fri Jul 31 22:47:34 PDT 2009; root:xnu-1456.1.25~1/RELEASE_X86_64 x86_64

How to switch between 32-bit and 64-bit kernel mode in Mac OS 10.6

Summary
Mac OS X v10.6 Snow Leopard includes a 64-bit kernel. On hardware that supports the 64-bit kernel, you can choose whether to start up (boot) your Mac using the new 64-bit kernel or the earlier 32-bit kernel.

Products Affected
Mac OS X Server 10.6, Mac OS X 10.6, Mac Pro (Mid 2010)

Method 1 - Startup key combination (for current startup only)

  • If your Mac uses the 32-bit kernel by default, but supports the 64-bit kernel, you can start up using the 64-bit kernel by holding the 6 and 4 keys during startup.
  • If your Mac uses the 64-bit kernel by default, you can start up with the 32-bit kernel by holding the 3 and 2 keys during startup.

    Note:Your Mac will revert to the default kernel the next time you reboot it.

Method 2 - On-disk setting (persistent)

  • To select the 64-bit kernel for the current startup disk, perform the following:
    1. Open Macintosh HD > Applications > Utilities > Terminal
    2. In the window that opens, type the following, then press Enter

      sudo systemsetup -setkernelbootarchitecture x86_64
    3. Restart your computer.
  • To select the 32-bit kernel for the current startup disk, perform the following:
    1. Open Macintosh HD > Applications > Utilities > Terminal
    2. In the window that opens, type the following, then press Enter

      sudo systemsetup -setkernelbootarchitecture i386
    3. Restart your computer.

      Note: This setting is stored in the /Library/Preferences/SystemConfiguration/com.apple.Boot.plist file and will take effect every time you start up from this disk. If you start up from a different disk, the setting on that disk, or the hardware default, will take effect.

Delete all files anywhere on your Mac which match a search term

Be very careful with this one. There's almost no reason you'd need to do this. If you're trying to delete something, you should try any and all of the following first (listed in order):

  1. If you're deleting an application, just find it, drag it to the trash, and empty the trash.
  2. Finding the file using Spotlight, the OSX Find Feature, or the method described above. Then delete it yourself.
  3. AppZapper - Free utility which aids in completely uninstalling programs.
  4. AppCleaner - Free utility which aids in completely uninstalling programs and can catch files that Appzapper may miss.
  5. DesInstaller - Simple Macintosh utility for uninstalling device drivers. Be careful not to delete the wrong file(s).
  6. Leopard Cache Cleaner - Powerful Utility Useful for Installation Problems

If these don't accomplish what you needed, and your problem is serious enough, consider reinstalling your Mac OS, or using Time Machineto revert to a previously saved working state (You are using Time Machine right?).

The main point here is that the procedure below should be used at-your-own-risk, with extreme caution, and as an absolute last resort. Anything deleted using this method is not undo-able. If you delete the wrong file, worst-case-scenario, you could remove a critical system file, preventing your Mac from booting.

  1. Open Macintosh HD > Applications > Utilities > Terminal
  2. In the window that opens, type the following, then press Enter:

    sudo find / -name "*Bicycles*" –delete

    ...where in-place of Bicycles you enter your search term.
  3. Any items whose file name contains your search term will be permanently deleted from your computer. The operation can take a long time to complete. When the command prompt is displayed again (the same one which initially appeared at the top of the Terminal window when you first opened it), you'll know the operation has completed.

Working with Kernel Extensions

Load kext

  1. Open Macintosh HD > Applications > Utilities > Terminal
  2. In the window that opens, type the following, then press Enter:

    sudo kextload /System/Library/Extensions/Bicycles.kext/

    ...where in-place of Bicycles you enter the name of the Kernel Extension you'd like to load.

Unload kext

  1. Open Macintosh HD > Applications > Utilities > Terminal
  2. In the window that opens, type the following, then press Enter:

    sudo kextunload /System/Library/Extensions/Bicycles.kext/

    ...where in-place of Bicycles you enter the name of the Kernel Extension you'd like to unload.

Troubleshooting kext (Mac OS 10.6.2 and above)

  1. Open Macintosh HD > Applications > Utilities > Terminal
  2. In the window that opens, type the following, then press Enter:

    sudo kextutil /System/Library/Extensions/Bicycles.kext

    ...where in-place of Bicycles you enter the name of the Kernel Extension you'd like to look at.

    You'll be presented with information on the status of the extension.

Change Ownership of kext

Often, using the troubleshooting step above, you'll find that you have ownership issues with a kext.
  1. Open Macintosh HD > Applications > Utilities > Terminal
  2. In the window that opens, type the following, then press Enter:

    sudo chown -R root:wheel /System/Library/Extensions/Bicycles.kext

    ...where in-place of Bicycles you enter the name of the Kernel Extension you'd like to change.

  1. Open Macintosh HD > Applications > Utilities > Terminal
  2. In the window that opens, type the following, then press Enter:

    sudo chmod -R 755 /System/Library/Extensions/Bicycles.kext

    ...where in-place of Bicycles you enter the name of the Kernel Extension you'd like to change.

Enable NTFS drive support in OSX Snow Leopard or Lion

  1. Open Macintosh HD > Applications > Utilities > Terminal
  2. In the window that opens, type the following, then press Enter:

    diskutil info /Volumes/volname

    ...where volname is the name of the NTFS volume.
  3. From the output, copy the Volume UUID value to the clipboard.
  4. Type the following, then press Enter:

    sudo nano /etc/fstab
  5. Then Type the following:

    UUID=111-111-111 none ntfs rw

    ...where 111-111-111 is the UUID you copied.
  6. Save and quit the nano function by typing Ctrl+XY, and then Enter.
  7. Restart your system.

Installing the INTL extension on MAMP


I just started developing in Symfony2, and the installation told me I should install the Intl extension (intl.so) for internationalization. Although Symfony2 works without the extension, I suspect I will run into problems sooner or later, e.g. when using the translation service. So after trying some different ways to compile and install the extension, I finally found that it is fairly easy using Macports.

First, you need to install Macports (http://www.macports.org/install.php), which allows you to compile and install many Linux packages without any hassle.
Please note, if compiling for 32-bit version of MAMP, please read last section of this blog post (Wrong architecture) before continuing !
After installation, go to your terminal and run following command :
sudo port install php5-intl
This will take a while, and will install a whole lot of packages (even apache, php5, …). When this is done, copy the file /opt/local/lib/php/extensions/no-debug-non-zts-20090626/intl.so to the extensions dir of your MAMP application (for PHP5.3, this is/Applications/MAMP/bin/php5.3/lib/php/extensions/no-debug-non-zts-20090626) :
cp /opt/local/lib/php/extensions/no-debug-non-zts-20090626/intl.so /Applications/MAMP/bin/php5.3/lib/php/extensions/no-debug-non-zts-20090626/
Now, add the extension to your php.ini file :
# /Applications/MAMP/conf/php5.3/php.ini
... some stuff here

extension=intl.so
Then (re)start your server, and the extension should be available (check phpinfo()). Have fun :)
Wrong architecture
If you get an error about wrong architecture, you have probably compiled everything in 64-bit while MAMP is compiled in 32-bit (and thus the extensions need to be in 32-bit as well). If so, edit the/opt/local/etc/macports/macports.conf file, and change (or uncomment) following line :
# /opt/local/etc/macports/macports.conf
build_arch                      i386
From now on, macports will compile everything in 32-bit.


PS: related info

how to find is it 32bit or 64 bit

switch(PHP_INT_SIZE) {
    case 4:
        echo '32-bit version of PHP';
        break;
    case 8:
        echo '64-bit version of PHP';
        break;
    default:
        echo 'PHP_INT_SIZE is ' . PHP_INT_SIZE;
        break;
}

how to uninstall homebrew
https://gist.github.com/mxcl/1173223

how to uninstall macports
http://guide.macports.org/chunked/installing.macports.uninstalling.html

Mac OS X - Terminal Tricks

Mac OS X - Terminal Tricks


Finding absolutely anything on your Macintosh

The Spotlight feature doesn't search the Library or hidden locations.

Aside from Spotlight feature, OSX also has a Find feature. Unlike Spotlight you can configure OSX's Find feature (CMD+F) look for system files by performing the following:

  1. Press CMD+F to open the Find window.
  2. Towards the left of the window, just above where the search results would appear, you'll a pull-down menu which says (by default)Kind. Click this, and choose Other.
  3. In the window that opens, scroll down, find, and check off System files.
  4. Click OK to close the window.
  5. Now, you can perform searches which will include System files in the results. To do this, select the following criteria for your search:
    System files include.

However, even this will not search all files on your Mac (i.e. items in hidden folders). To search all locations for a file, perform the following:

  1. Open Macintosh HD > Applications > Utilities > Terminal
  2. In the window that opens, type the following, then press Enter:

    sudo find / -name "*Bicycles*"

    ...where in-place of Bicycles you enter your search term.
  3. You will be presented with a list of file-paths to any items whose file name contains your search term. The search can take a long time to complete. When the command prompt is displayed again (the same one which initially appeared at the top of the Terminal window when you first opened it), you'll know the search has completed.