OS X Misc Stuff

root's picture

How to start finder from command line:

Open up Terminal and run the command below (dot means current directory).
user@osx$ open .

How to restart the graphical interface of OS X:

user@osx$ sudo killall -HUP WindowServer

How to retrieve CPU information via command line in OS X:

sysctl -n machdep.cpu.brand_string
system_profiler | grep Processor

How to retreive CPU and Memory information in OS X (works also on the iPhone's console):

patrues:~ root# hostinfo
Mach kernel version:
         Darwin Kernel Version 11.0.0: Tue Nov  1 20:34:16 PDT 2011; root:xnu-1878.4.46~1/RELEASE_ARM_S5L8940X
Kernel configured for up to 2 processors.
2 processors are physically available.
2 processors are logically available.
Processor type: armv7f (arm v7f)
Processors active: 0 1
Primary memory available: 504.95 megabytes
Default processor set: 35 tasks, 293 threads, 2 processors
Load average: 0.65, Mach factor: 1.36

How to restart Finder from command line in OS X:

osx:~ osx$ osascript -e 'tell app "Finder" to quit'

How to force the rebuild of Spotlight index in OS X:

osx:~ osx$ sudo mdutil -E /

How to mount NFS from command line in OS X:

osx:~ root# mount -t nfs /mnt
If you get the following error mount_nfs: /mnt: Operation not permitted, mount it with -o resvport:
osx:~ root# mount -t nfs -o resvport /mnt
osx:~ root# df -h /mnt
Filesystem                 Size   Used  Avail Capacity  Mounted on   50Gi   35Gi   15Gi    70%    /mnt

How to share NFS mount in OS X:

Here you have no choice but to use command line.
1. Populate (create or update) the file /etc/exports (type man exports if you do not know how):
eeny:~ osx$ cat /etc/exports 
/Volumes/vbox/ -network -mask

2. Restat nfsd as follows:
eeny:~ osx$ sudo nfsd disable
eeny:~ osx$ sudo nfsd enable

3. see the results:
eeny:~ osx$ showmount -e
Exports list on localhost:

PS: if you get "cannot chown" even if you have full access, you must add -mapall option:
eeny:incoming osx$ cat /etc/exports  
/Volumes/vbox/incoming -alldirs -mapall

How to execute dd over ssh in OS X (in our example, how to make a dump of your iphone's disk - use /dev/disk0s1s2 for /private/var partition):

from the iPhone: patrues:/ root# dd if=/dev/disk0 bs=1M | ssh username@mac ‘dd of=iphone-dump.img’
from Mac: mac: /# ssh user@patrues dd if=/dev/disk0 bs=1M | dd of=iphone-dump.img

How to (re)install rEfit in OS X:

florians-MacBook-Pro:~ florian$ cd /efi/refit/
florians-MacBook-Pro:refit florian$ ls
enable-always.sh	icons			refit.efi
enable.sh		refit.conf		refit.vollabel
florians-MacBook-Pro:refit florian$ ./enable.sh 
+ sudo bless --folder /efi/refit --file /efi/refit/refit.efi --labelfile /efi/refit/refit.vollabel
florians-MacBook-Pro:refit florian$ 

How to get the OS X Version from command line:

osx:~ osx$ sw_vers
ProductName:	Mac OS X Server
ProductVersion:	10.6
BuildVersion:	10A433

How to do a time delayed screen capture via terminal in OS X:

osx:~ osx$ screencapture -T 10 screenshot.jpg

How to lock/unlock a file from command line in OS X:

A. Mac OS X uses the immutable bit to designate that a file is locked. To lock a file:
To lock: osx:~ osx$ chflags uchg /path/to/file
To unlock: osx:~ osx$ chflags nouchg /path/to/file

How to reset the Spotlight data cache:

osx:~ osx$ sudo mdutil -E /

How to enable Spotlight data cache:

osx:~ osx$ sudo mdutil -i on /

How to disable Spotlight:

osx:~ osx$sudo launchctl unload -w /System/Library/LaunchDaemons/com.apple.metadata.mds.plist

How to re-enable Spotlight:

osx:~ osx$sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.metadata.mds.plist

How to “trace” disk I/O execution live. Each time a disk I/O completes, a line of output is printed to summarize it, including process name and filename details:

flmbp:~ root# iosnoop
    0  7971 R  6289576   8192   spindump ??/<unknown (NULL v_parent)>/<unknown (NULL v_name)>
    0  7971 R   801736   8192   spindump ??/<unknown (NULL v_parent)>/<unknown (NULL v_name)>

How to measure I/O before it is processed by the HFS+ file system. (iosnoop measures I/O after the file system, and only if it reaches disk)

flmbp:~ root# hfsslower.d

How to trace outbound TCP connections along with details:

flmbp:~ root# socconnect_mac.d

How to print a line for each new process that is executed (run this and do something):

flmbp:~ root# execsnoop
  501   8016    355 QuickLookUIHelpe
  501   8017      1 com.apple.audio.
    0   8017      1 xpcproxy
    0   8018      1 xpcproxy
  501   8018      1 com.apple.audio.
    0   8019      1 xpcproxy
  501   8019      1 VTDecoderXPCServ

How to trace file opens and print various details:

flmbp:~ root# opensnoop
  UID    PID COMM          FD PATH                 
    0     78 cfprefsd      -1 /Library/Managed Preferences/com.apple.AddressBookSourceSync.plist 
    0     78 cfprefsd      -1 /Library/Managed Preferences/com.apple.AddressBookSourceSync.plist 
    0     78 cfprefsd      -1 /Library/Preferences/com.apple.AddressBookSourceSync.plist 
    0     78 cfprefsd      -1 /Library/Preferences/com.apple.AddressBookSourceSync.plist 
  501   8108 AddressBookSour   4 /etc/localtime       
  501   8108 AddressBookSour   4 /.vol/16777220/92116233 

How to trace all types of system calls (like strace on linux):

flmbp:~ root# dtruss type
SYSCALL(args) 		 = return
thread_selfid(0x0, 0x1DC0, 0x7FFF6A359550)		 = 830326 0
csops(0x0, 0x0, 0x7FFF537D0444)		 = 0 0
issetugid(0x0, 0x0, 0x0)		 = 0 0
shared_region_check_np(0x7FFF537CE378, 0x10C42F000, 0x4)		 = 0 0
stat64("/usr/lib/dtrace/libdtrace_dyld.dylib\0", 0x7FFF537CF528, 0x7FFF537D0460)		 = 0 0
open("/usr/lib/dtrace/libdtrace_dyld.dylib\0", 0x0, 0x0)		 = 3 0
sigaltstack(0x0, 0x7FFF537D0AF0, 0x0)		 = 0 0
read(0xFF, "\0", 0xBE)		 = 0 0
read(0x3, "type\n\0", 0x80)		 = 5 0

How to provide a summary of which system calls were failing, showing the process name, error code, and short description of the error:

flmbp:~ root# errinfo -c
Tracing... Hit Ctrl-C to end.
            EXEC          SYSCALL  ERR  COUNT  DESC
   ServerPerfLog __semwait_signal   60      1  Operation timed out 
   ServerPerfLog        proc_info   22      1  Invalid argument 
        perl5.16        sigreturn   -2      1 
   ServerPerfLog        proc_info    3     17  No such process 

How to characterize the disk I/O workload, showing a distribution of the size of the I/O in bytes along with the application name:

florians-MacBook-Pro:~ root# bitesize.d
Tracing... Hit Ctrl-C to end.

     PID  CMD
      74  cfbackd\0

           value  ------------- Distribution ------------- count    
            2048 |                                         0        
            4096 |@@@@@@@@@@@@@@@@@@@@@@@                  17       
            8192 |@@@@@@@@@@@@@@@@@                        12       
           16384 |                                         0        

       1  launchd\0

           value  ------------- Distribution ------------- count    
            2048 |                                         0        
            4096 |@@@@@@@@@@@@@@@                          16       
            8192 |@@@@@@@@@@@@@@@@@@@@@@@@@                26       
           16384 |                                         0        

How to show the same data as iosnoop, but in a summarized way similar to top:

flmbp:~ root# iotop
Tracing... Please wait.

2014 Feb 14 00:34:57,  load: 0.53,  disk_r:      0 KB,  disk_w:    252 KB

  UID    PID   PPID CMD              DEVICE  MAJ MIN D            BYTES
    0      0      0                  ??        1   4             242688
    0      1      0 launchd          ??        1   4 W           258048

How to trace the creation and deletion of files:

flmbp:~ root# maclife.d

How to disable ipv6 in MAC OS X (the command id giving no output if successful):

flmbp:~ root# networksetup -setv6off Wi-Fi
flmbp:~ root# networksetup -setv6off Ethernet

How to clear (clean) "Open With" menu:

Go to ~/Library/Preferences and then delete “com.apple.LaunchServices.plist” file.

How to check the power management status:

osx:~ osx$ pmset -g
Active Profiles:
AC Power		-1*
Currently in use:
 hibernatemode        0
 sleep                10 (sleep prevented by 35, 65, 65, 65, 65, 155, 337)
 powerbutton          1
 ttyskeepawake        1
 hibernatefile        /var/vm/sleepimage
 disksleep            10
 displaysleep         10

How to disable hibernation:

sudo pmset -a hibernatemode 0
sudo pmset -a autopoweroff 0
sudo pmset -a standby 0
sudo rm /var/vm/sleepimage

How to create bootable OS X image with iesd (in our example we use Yosemite app store package):

florians-MacBook-Pro:~ root# gem install iesd
Fetching: iesd-1.2.1.gem (100%)
Successfully installed iesd-1.2.1
Parsing documentation for iesd-1.2.1
Installing ri documentation for iesd-1.2.1
1 gem installed
florians-MacBook-Pro:~ root# iesd -i /Applications/Install\ OS\ X\ Yosemite\ Beta.app -o Yosemite.dmg -t BaseSystem
==> Mounting /Applications/Install OS X Yosemite Beta.app/Contents/SharedSupport/InstallESD.dmg
Mounted: /var/folders/zz/zyxvpxvq6csfxvn_n0000000000000/T/d20140730-32110-1nc9m1
==> Mounting /var/folders/zz/zyxvpxvq6csfxvn_n0000000000000/T/d20140730-32110-1nc9m1/BaseSystem.dmg
Mounted: /var/folders/zz/zyxvpxvq6csfxvn_n0000000000000/T/d20140730-32110-1xohbi0/d20140730-32110-1v9dgx8
==> Copying /var/folders/zz/zyxvpxvq6csfxvn_n0000000000000/T/d20140730-32110-1nc9m1/Packages
Copied: /var/folders/zz/zyxvpxvq6csfxvn_n0000000000000/T/d20140730-32110-1xohbi0/d20140730-32110-1v9dgx8/System/Installation/Packages
==> Unmounting /var/folders/zz/zyxvpxvq6csfxvn_n0000000000000/T/d20140730-32110-1nc9m1/BaseSystem.dmg
Unmounted: /var/folders/zz/zyxvpxvq6csfxvn_n0000000000000/T/d20140730-32110-1xohbi0/d20140730-32110-1v9dgx8
==> Merging /var/folders/zz/zyxvpxvq6csfxvn_n0000000000000/T/d20140730-32110-1xohbi0/BaseSystem.dmg.shadow
Merged: /var/folders/zz/zyxvpxvq6csfxvn_n0000000000000/T/d20140730-32110-1qohe3d/BaseSystem.dmg
==> Unmounting /Applications/Install OS X Yosemite Beta.app/Contents/SharedSupport/InstallESD.dmg
Unmounted: /var/folders/zz/zyxvpxvq6csfxvn_n0000000000000/T/d20140730-32110-1nc9m1

How to check the custom kernel extentions (not the ones apple os is coming with):

note: 00000000-0000-0000-0000-0000000000000 is what I have edited to mask my output. In yours, you will see (instead of zeros) a combination of numbers and letters.
mini:~ osx$ kextstat | grep -v com.apple
Index Refs Address            Size       Wired      Name (Version) UUID <Linked Against>
  137    3 0xffffff7f82ef4000 0x60000    0x60000    org.virtualbox.kext.VBoxDrv (5.0.10) 00000000-0000-0000-0000-0000000000000 <7 5 4 3 1>
  141    0 0xffffff7f82f54000 0x8000     0x8000     org.virtualbox.kext.VBoxUSB (5.0.10) 00000000-0000-0000-0000-0000000000000 <140 137 40 7 5 4 3 1>
  142    0 0xffffff7f82f5c000 0x5000     0x5000     org.virtualbox.kext.VBoxNetFlt (5.0.10) 00000000-0000-0000-0000-0000000000000 <137 7 5 4 3 1>
  144    0 0xffffff7f82f61000 0x6000     0x6000     org.virtualbox.kext.VBoxNetAdp (5.0.10) 00000000-0000-0000-0000-0000000000000 <137 5 4 1>

How to enable/disable firewall via command line:

Enable: sudo defaults write /Library/Preferences/com.apple.alf globalstate -int 1
Disable:defaults write /Library/Preferences/com.apple.alf globalstate -int 0

How to stop/start the firewall via command line:

To stop:

launchctl unload /System/Library/LaunchAgents/com.apple.alf.useragent.plist
sudo launchctl unload /System/Library/LaunchDaemons/com.apple.alf.agent.plist

To start:

launchctl load /System/Library/LaunchDaemons/com.apple.alf.agent.plist
launchctl load /System/Library/LaunchAgents/com.apple.alf.useragent.plist

How to list the firewall's trusted applications via command line:

sudo /usr/libexec/ApplicationFirewall/socketfilterfw -l | grep TRUSTEDAPPS

How to free up inactive memory via command line:

sudo purge

How to force Aperture Library to be repaired next time is open:

In example below we want Current.aplibrary to be verified.
$ touch Current.aplibrary/repairOnLaunch
$ ls -la Current.aplibrary/repairOnLaunch 
-rw-r--r--@ 1 florian  polkitd  0 Jan 23 18:17 Current.aplibrary/repairOnLaunch

Thou shalt not steal!

If you want to use this information on your own website, please remember: by doing copy/paste entirely it is always stealing and you should be ashamed of yourself!
Have at least the decency to create your own text and comments and run the commands on your own servers and provide your output, not what I did!

Or at least link back to this website.