Linux sed on macOS

Install gnu sed

brew install gnu sed

Create an alias that will replace macOS built-in sed

echo "alias sed=gsed" >> ~/.zshrc

Enable TouchID for sudo

Open the sudo utility

sudo vi /etc/pam.d/sudo

Add the following as the first line

auth sufficient pam_tid.so

Using latest bash

macOS stopped updating bash and switched to zsh. You can install bash with:

brew install bash

After that point bash -version should print version 5. In your scripts you should not reference /bin/bash anymore but env.

#!/usr/bin/env bash

Trust Downloaded Binaries

Check if binary is quarantined

xattr bin/cache/artifacts/libimobiledevice/idevice_id

Remove binary from quarantine

sudo xattr -d com.apple.quarantine bin/cache/artifacts/libimobiledevice/idevice_id 

Remove all attributes

sudo xattr -c bin/cache/artifacts/libimobiledevice/idevice_id 

Fix Spotlight Index

Disable indexing on all connected drives.

sudo mdutil -a -i off

Reboot

Enable indexing /

sudo mdutil -i on /

Erase and rebuild index

sudo mdutil -E /

Debug Spotlight

sudo mddiagnose

Remove Time Machine Snapshots

You can not disable local Time Machine snapshots, but you can remove them like this:

tmutil listlocalsnapshotdates / | grep 20 | while read f; do tmutil deletelocalsnapshots $f; done 

Exclude App Store apps from Time Machine backup

Run this once and it puts all the apps downloaded from the App Store into the TM exclude.

mdfind "kMDItemAppStoreHasReceipt=1" -onlyin /Applications/ | while read x; do sudo tmutil addexclusion -p "$x"; done

Installing SF Mono

If you want to use the SF Mono font you can find it inside the Terminal app.

open /Applications/Utilities/Terminal.app/Contents/Resources/Fonts/

Disable GameKit

launchctl unload -w /System/Library/LaunchAgents/com.apple.gamed.plist

and

defaults write com.apple.gamed Disabled -bool true

Extract Audio from Youtube Videos

brew install youtube-dl
youtube-dl --ignore-errors -f mp4 https://www.youtube.com/watch?v=dQw4w9WgXcQ
afconvert -d aac input.mp4 output.m4a
				

If you want 1080p mp4 to be your default add the following line to ~/.config/youtube-dl/config.

-i -f 'bestvideo[height<=1080][ext=mp4]+bestaudio[ext=m4a]/best[height<=1080][ext=mp4]/best'

Disabling System Integrity Protection

To disable System Integrity Protection boot into the Recovery partition by pressing cmd+R at startup. Open Utilities > Terminal and run:

csrutil disable

Enable Safari Backspace

For this to work you need to disable system integrity protection.

defaults write com.apple.Safari NSUserKeyEquivalents -dict-add Back "\U232b"

Show the Library folder

Right click in the home folder, select "Show View Options", at the bottom you can enable "Show Library Folder"

Alternative you can press: shift-cmd-. to show hidden files.

Clear DNS Cache

Screenshot file format and change extension to lowercase

sudo killall -HUP mDNSResponder

Disable auto-correct

defaults write NSGlobalDomain NSAutomaticSpellingCorrectionEnabled -bool false

Prevent Mac from falling asleep

stay awake

caffeinate -i

prevent display sleep

caffeinate -d

Prevent Mac from falling asleep for an hour

caffeinate -u -t 3600

Remove Duplicates from the "Open With" Menu

/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/LaunchServices.framework/Versions/A/Support/lsregister -kill -r -domain local -domain user;killall Finder

Screencapture

Screenshot file format and change extension to lowercase

defaults write com.apple.screencapture type png

Screenshot name

defaults write com.apple.screencapture name ScreenShot

Screenshot Location

defaults write com.apple.screencapture location [path]

Turn Off Expose

defaults write com.apple.dock mcx-expose-disabled -bool false;killall Dock

Turn Off Dashboard

defaults write com.apple.dashboard mcx-disabled -boolean YES;killall Dock

Disable Notification Center

log out and log back in after

sudo defaults write /System/Library/LaunchAgents/com.apple.notificationcenterui KeepAlive -bool false

Checking external USB stick

Format it for macOS and then use fsck_hfs with "-S" for scanning.

diskutil list
diskutil unmountDisk /dev/diskXXXX
sudo fsck_hfs -fy -S /dev/diskXXXX
(press ctrl-t for progress)

Restoring Modification Date of a File

f="test file.png"
dc="$(GetFileInfo -d "$f")" # store creation date
dm="$(GetFileInfo -m "$f")" # store modification date
# change the file here
SetFile -d "$dc" "$f" # restore creation date
SetFile -m "$dm" "$f" # restore modification date

more nice Terminal tricks

Wakeup Reason

log show |grep -i "Wake reason"

gzip

tar -czf folder_name.tar.gz folder_name/

Open screen sharing

open vnc://computername.local

Enable screen sharing

sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.screensharing.plist

Open current folder in Finder

open .

Restart Finder

killall Finder

Restart Dock

killall Dock

Network Monitor

lsof -ni -P

Display all locations of a binary

type -a git