Handy Mac Commands - rajivkanaujia/alphaworks GitHub Wiki

Note: You will have to use "Terminal" program for executing some of the commands

Bluetooth

Remove auto-connecting to a blue tooth audio device

$ sudo defaults write /Library/Preferences/com.apple.Bluetooth.plist DontPageAudioDevices 1

 

Finder

See the hidden files

$ killall Finder
$ defaults write com.apple.Finder AppleShowAllFiles YES
$ killall Finder

 

Hide hidden files

$ killall Finder
$ defaults write com.apple.Finder AppleShowAllFiles NO
$ killall Finder

  

How To Change All Email Attachments To View As Icon

  • Open Terminal
  • Run the following: 
$ defaults write com.apple.mail DisableInlineAttachmentViewing -bool yes
  • Press Return.
  • Quit Mail then Relaunch it.

Open File in an editor

Opening file in TextEdit

$ open -a TextEdit filename

-t opens in the default editor (i.e. if you use BBEdit, TextMate, etc.) -e will open the file specifically in TextEdit

Example: Open in an app called Brackets

$ open -a Brackets filename

Remove duplicate lines from files preserving their order

I use this a lot for cleaning up my .bash_history

$ cp current_file current_file_backup
$ awk '!visited[$0]++' current_file > deduplicated_current_file
$ cp deduplicated_current_file current_file

Remove duplicate lines from files WITHOUT preserving their order

I rarely use this for cleaning up my .bash_history as I would like to keep the sort order.

$ cp current_file current_file_backup
$ nl current_file | sort -k 2  -k 1,1nr| uniq -f 1 | sort -n | cut -f 2 > deduplicated_sorted_current_file
$ cp deduplicated_sorted_current_file current_file

Reset Launchpad Apps Order

Reset the Launchpad apps order to their default order

$ defaults write com.apple.dock ResetLaunchPad -bool true; killall Dock

Rename files based on File Create Date (not same as date the photo was taken)#

Following commands can be used to rename file by adding a number in front of it. Remove 'echo' when you really want to execute this command

$ n=1; for f in *; do echo mv "$f" "$((n++))_$f"; done

Following commands can be used to rename file by using its create date. Remove 'echo' when you really want to execute this command

$ for f in *.*; do echo mv "$f" "$(stat -f '%Sm' -t '%Y-%m-%d %H.%M.%S' "$f").${f##*.}"; done

Adds a "counter" number. Remove 'echo' when you really want to execute this command. Run this twice so that counter becomes ordered.

n=1; for f in *.*; do echo mv "$f" "$(stat -f '%Sm' -t '%Y-%m-%d %H.%M.%S' "$f") ("$((n++))").${f##*.}"; done

Rename files using DateTimeOriginal meta-data via exiftool

A good use of this is for Images and Videos where you want to use the EXIF metadata. Install the tool -

brew install exiftool

Following commands can be used to rename file by adding a number in front of it. Remove 'echo' when you really want to execute this command

$ n=1; for f in *; do echo mv "$f" "$((n++))_$f"; done

Following commands can be used to rename file by using its create date. Remove 'echo' when you really want to execute this command

$ for f in *.*; do echo mv "$f" "$(exiftool -DateTimeOriginal -d "%Y-%m-%d %H.%M.%S"  "$f" | awk '{print $4, $5}').${f##*.}"; done

Used for image files as we take a lot pf photos via cellphone, rollowing commands can be used to rename file by using its create date and adds a "counter" number. Remove 'echo' when you really want to execute this command. Run this twice so that counter becomes ordered.

n=1; for f in *.*; do echo mv "$f" "$(exiftool -DateTimeOriginal -d "%Y-%m-%d %H.%M.%S"  "$f" | awk '{print $4, $5}') ("$((n++))").${f##*.}"; done

Note: If you like the instructions here, please refer it on your posts/documentation. Contact me if there are corrections needed.