Skip to content

Latest commit

 

History

History
505 lines (336 loc) · 12.4 KB

mac.md

File metadata and controls

505 lines (336 loc) · 12.4 KB

Mac

The best laptop money can buy:

https://www.apple.com/uk/macbook-pro/

If you don't have an M3 Pro / Max - you're missing out on an excellent (but overpriced) machine.

On balance of price vs performance the Macbook Air M3 is excellent, and even thinner and lighter than the Macbook Pro - a real joy to use. Even when you're using more RAM than the physical 16GB max and swapping, the performance is still so good you don't notice significant performance degradation for most usage, even when using heavyweight IDEs like IntelliJ.

Terminal

This is where you should be spending your life if you're a techie, with a browser to supplement referencing documentation.

  • $COLUMNS
    • 179 column width on my MacBook Pro 13" terminal
    • 204 on my MacBook Pro Retina 15" terminal
    • 215 on my Macbook Pro M3 2023 terminal - resolution gets better over time

Homebrew - Package Management

The best most widely used package manager for Mac.

See brew.md for how to use it and great package lists I've spent years discovering and building up.

Commands

A list of commands is provided at https://ss64.com/mac/.

Search for or open anything

Cmd-Space - opens Spotlight search to auto-complete and open anything quickly.

Open

Open a file / folder / URL / Application. Use -a to select which app to use to open it. Use -e to edit the file in TextEdit.

Open URL in the default web browser:

open https://google.com

Open current directory in Finder:

open .

Open image in the default app, usually Preview:

open file.jpg

You can also drag to the Terminal to paste a file or directory's path, or right-click copy and paste into the terminal to get its path on your command line.

Clipboard

Copy from stdin to clipboard:

pbcopy < input.txt

Paste from clipboard to stdout:

pbpaste > output.txt

Screenshots

Screenshot the Whole Screen

Cmd + Shift + 3

Screenshot a Selection - Drag a Rectangle

Switches to a cross-hair to drag to what you want to screenshot.

Cmd + Shift + 4

The Screencapture CLI

You may be prompted to allow Terminal to record the screen under Privacy & Security -> Screen Recording the first time - it'll tell you that you have to restart the Terminal, but it worked for me without a restart.

Switches to select window to capture:

screencapture -W /tmp/screenshot.png

Interactive mode with full toolbar, same as the Screenshot.app:

screencapture -i -U /tmp/screenshot.png

Video recording mode for 10 seconds (without -V it will record until you Ctrl-c it):

screencapture -v -V 10 /tmp/videocapture.mp4

So many great options from delayed screenshots, copy to clipboard, open in Preview, capture a coordinate rectangle (great for automation!), see:

screencapture --help

The Screenshot.app

in the Utilities folder is easy to use:

open /System/Applications/Utilities/Screenshot.app

Flush the DNS Cache

dscacheutil -flushcache
sudo killall -HUP mDNSResponder

Set DNS Servers

Sometimes you want to use public DNS servers for better performance than the local DHCP given ones:

sudo networksetup -setdnsservers en0 4.2.2.1 4.2.2.2

To set back to DHCP DNS servers, just clear the list:

sudo networksetup -setdnsservers en0 "Empty"

In DevOps-Bash-tools the shell has a function dhcpdns to do this for all network interfaces and remove the DNS search domains since you should inherit them from DHCP and not leak those FQDN searches to the internet if on public networks.

Set DNS Search Domain

sudo networksetup -setsearchdomains en0 mydomain.com

In DevOps-Bash-tools the shell has a function set_dns to do this for all network interfaces to make this easier.

You should see these search domains appear in the class unix file /etc/resolv.conf managed by apple networking.

To remove DNS search domains:

sudo networksetup -setsearchdomains en0 "Empty"

or clear_dns_search function in DevOps-Bash-tools to do this for all interfaces.

Stats Bar

Stats gives many nice toolbar stats on your Mac's performance for CPU, GPU, RAM, Disk, Network and Battery.

Battery even tells you:

  • time to discharge
  • time to charge
  • number of cycles (although this one I'm sure is only since installation of Stats)
  • battery health
  • which apps which are consuming a lot of energy for you to kill them if you're not needing them, helping your battery life when on the move

Reducing Desktop Workspaces

If like me, you hate the time lost in graphics sliding effect from one desktop workspace to another, especially caused by apps like Windows Virtual Desktop in Remote Desktop opening to full screen, and just prefer good old fashioned Cmd-Tab, then you can remove the extra workspaces as described here by pressing F3 and then hovering on and clicking the X to delete the extra workspaces.

Unfortunately, Remote Desktop still seems to slide and send to fullscreen in another temporary desktop workspace.

Say - text-to-speech

Make your Mac speak. I use this to impress the kids, or even send a message from my study to my living room computer by running say over an SSH login to the computer downstairs for them to hear!

say "Daddy is the greatest"

You can also pipe in text:

echo "Daddy is the greatest" | say

Try different voices, list them like this

say -v ?

Speak from a text file and save it to an audio file:

say -f words.txt -o myaudioclip.aiff

Finding Files - Spotlight Search and Index Management

The equivalent of locate on Linux, uses the Spotlight index:

mdfind [-onlyin /path/to/directory] [-name "$filename"] "$term"

Erase and rebuild the Spotlight index:

mdutil -E

Enable / disable Spotlight indexing for a given volume or entirely:

mdutil -i

Launchctl

This accesses launchd, Mac's init script equivalent.

See which launchd scripts are loaded:

launchctl list

Stop and unload script, add -w to remove it from the boot sequence:

sudo launchctl unload [path/to/script]

Launch scripts are found in the following locations:

~/Library/LaunchAgents
/Library/LaunchAgents
/Library/LaunchDaemons
/System/Library/LaunchAgents
/System/Library/LaunchDaemons

Launchd documentation

Blog post on cool things launchd can do, like watching and executing on files that are dropped into directories.

Disk Management

Using graphical Disk Utility is easiest:

open /System/Applications/Utilities/Disk\ Utility.app

CLI Disk Management

Great tutorial:

Part 1 - List, Verify, Repair, Rename, Erase volumes

Part 2 - Partition, Format, Split / Merge Partitions

Part 3 - Create .dmg disk images from a Volume / Folder, Encrypted Disk Image, Resize Image, Restore Image

List disks

diskutil list

diskutil mount and diskutil mountDisk are the same

Mount a partition

diskutil mount /dev/disk4s2
diskutil unmount /dev/disk4s2

or by volume location:

diskutil unmount "/Volumes/$NAME"

Mount a partition read-only if having trouble and trying to recover data:

diskutil mount readOnly /dev/disk4s2

Mount at a different location to the default /Volumes/<partition_metadata_name>:

diskutil mount /dev/disk4s2 -mountPoint /path/to/dir

Mount / unmount partitions on a whole disk by reading its partition table:

diskutil mountDisk disk10
diskutil unmountDisk disk10

Verify a volume:

diskutil verifyVolume "/Volumes/$NAME"

Repair volume:

diskutil repairVolume "/Volumes/$NAME"
diskutil verifyPermissions "/Volumes/$NAME"
diskutil repairPermissions "/Volumes/$NAME"

Format a partition This is risky because there is no confirmation, better to do this from Disk Utility:

diskutil eraseDisk "$filesystem" "$name" "/dev/$diskN"

See which filesystems are available for formatting:

diskutil listFilesystems

Rename a disk:

diskutil rename "$volume_name" "$new_volume_name"

Partition and Format a disk

APFS requires GPT partition table

disk="disk4"
partition_table="GPT"
name="MyVolume"
filesystem="APFSX"  # AppleFS case-sensitive, found from 'diskutil listFilesystems' above
size="0b"           # integer + units suffix (b, m or g for bytes, megabytes or gigabytes) - '0b' uses all space
diskutil partitionDisk "/dev/$disk" "$partition_table" "$filesystem" "$name" "$size"
Multiple Partition and Format
diskutil partitionDisk /dev/"$disk" "$partition_table" "$filesystem" "First"  "$size" \
                                                       "$filesystem" "Second" "$size" \
                                                       "$filesystem" "Third"  "$size" \
                                                       "$filesystem" "Fourth" "$size" \
                                                       "$filesystem" "Fifth"   0b  # '0b' to use up all remaining space

Partition splitting doesn't seem to work with APFS, only macOS Extended, as APFS tells you to diskutil apfs deleteContainer disk10 instead which leaves you with free space to create a new partition.

Erase a disk before decommissioning it

Either use Disk Utility above, a command like diskutil eraseDisk ... or the more portable unix command dd with a custom command like this to do a moderate 3 pass overwrite (tune number of passes variable to suit your level of data recovery paranoia, eg. DoD standard 7 passes):

WARNING: disk numbers may shunt up in numbers as you insert more removal drives, especially for 'synthesized' virtual disks that display for volume containers
passes=3
time \
for number in $(seq $passes); do
    echo pass $number
    echo
    time sudo dd if=/dev/urandom of=/dev/disk4 bs=1M
    echo
done

Note: multiple passes are only for old inaccurate HDDs rotating mechanical metal platter disk. For SSDs, you only need a single pass.

Service Management

Load and start a service from a plist file:

sudo launchctl load -F "/System/Library/LaunchDaemons/$name.plist"
sudo launchctl start "com.apple.$name"

Stop and unload a service:

sudo launchctl stop "com.apple.$name"
sudo launchctl unload "/System/Library/LaunchDaemons/$name.plist"

See dhcp.md for a practical example of using this for the built-in tftp server for PXE boot installing Debian off your Mac.

Creating Bootable CDs & USBs from ISOs

Macs and many computers don't come with CD/DVD anymore to save space, so you can either buy an external USB dvd-writer or create bootable USBs.

To create a bootable USBs from ISO image files intended for CD/DVDs in order to use them to install Linux or use a disk wiping distro like DBAN or ShredOS, you can download Etcher or use mac_iso_to_usb.sh from DevOps-Bash-tools:

mac_iso_to_usb.sh "$iso"  # /dev/disk4

Other Options

  1. UNetbootin app from github
  2. DiskUtility
  3. CLI

CLI

Set your filename in a variable so the following commands can be executed as is without editing:

export ISO="memtest86+-5.01.iso"

Convert it to make it bootable - will automatically add .dmg suffix => .img.dmg:

hdiutil convert -format UDRW -o "$ISO.img" "$ISO"

Figure out which is the USB disk - be careful or you'll destroy your system!

diskutil list
diskutil unmountDisk /dev/diskN

Tip: prefixing disk with 'r' uses raw disk which is faster:

sudo dd if="$ISO.img" of=/dev/rdiskN bs=1m # or 1M
diskutil eject /dev/diskN

Burn CD - insert blank CD then:

hdiutil burn "$ISO"
Ported from various private Knowledge Base pages 2010+