Basic commands
Clipboard
Put some test in to the clipboard
echo "text to copy" | pbcopy
Copy output of the command to the clipboard
whoami | pbcopy
Copy file to the clipboard from the terminal
pbcopy < ~/path/to/file
Paste from clipboard
pbpaste
Put contents of the clipboard to a file
pbpaste > file-name.txt
Execute clipboard contents in a shell
pbpaste | zsh
Sort content of the clipboard, remove duplicate lines
pbpaste | sort -u | pbcopy
Application entitlements
Check application entitlements
codesign -d -vv --entitlements - /Applications/Your-Application.app
Open
Open application from terminal
open -a Notes
Open new instance of the application, even if the app is already running
open -n -a Notes
Open new “fresh” instance - without restoring the state
open -F -n -a Safari
Open file in TextEdit
open -e file.txt
Open finder in current working directory
open .
Reveal the file in Finder instead of opening
open -R test.txt
Open the application hidden
open -a TextEdit -j
Do not bring the application to foreground
open -a TextEdit -g
Open the URL with default app
open -u smb://127.0.0.1
Write output of the command to temp file and open it in default text editor
ls / | open -f
Open a web browser
Open web page in default browser
open https://example.com
Open web page in Safari from terminal
open -a Safari https://example.com
Open file in Safari
open -a Safari file:///Users/user/Downloads/test.html
Additional arguments
--args
- pass remaining arguments to the opened application--env VAR
- add the variable to the environment of the application--stdin PATH
- open app with stdin set to PATH--stdout PATH
- open app with stdout set to PATH--stderr PATH
- open app with stderr set to PATH
Users
List users
dscl . -ls Users
Check user password
dscl . -authonly USER [PASSWORD]
Networking
Check system proxy for “Wi-Fi” adapter
networksetup -getautoproxyurl "Wi-Fi"
Listening ports
Show listening ports
netstat -na | grep -i listen
with lsof
lsof -nP | grep LISTEN
Finder
Show all files in Finder
defaults write com.apple.finder AppleShowAllFiles TRUE
killall Finder
Hide desktop icons
defaults write com.apple.finder CreateDesktop -bool false
killall Finder
Extended file attributes
List attributes
List file attributes
xattr -l /path/to/file
List file attributes recursively in current working directory
xattr -lr .
Remove attributes
Remove quarantine from the file
xattr -d com.apple.quarantine /path/to/file
Remove the location where the file was downloaded from
xattr -d com.apple.metadata:kMDItemWhereFroms /path/to/file
Remove all attributes
xattr -c /path/to/file
Spotlight
Spotlight search
mdfind -name .pdf
Limit the scope of the search to specified directory
mdfind -onlyin ~/Documents -name .pdf
mdfind image -onlyin ~/Documents
Print total number of matches
mdfind .pdf -onlyin ~/Documents -count
Disable/enable spotlight
Disable the index
mdutil -i off /
Enable the index
mdutil -i on /
Erase the current Spotlight index
mdutil -E /
Get/set computer name
Get host name
scutil --get HostName
Get local host name
scutil --get LocalHostName
Get computer name
scutil --get ComputerName
Change comupter name
Set fully qualified hostname
sudo scutil --set HostName new-host.name.com
Local host name (Bonjour hostname)
sudo scutil --set LocalHostName new-host
Set computer name
sudo scutil --set ComputerName new-host
osascript
Execute AppleScript
echo 'do shell script "whoami"' | osascript
Execute JavaScript code
echo '...' | osascript -l JavaScript
Compile script to .app
file
osacompile -o test.app script.txt
Compile JavaScript code
osacompile -l JavaScript -o test.app -s test.js
Display notification
osascript -e 'display notification "Hello world" with title "Hello"'
Beep
osascript -e 'beep'
Multiple beeps
osascript -e 'beep 5'
Volume
Change volume
osascript -e "set volume output volume 50"
Mute
osascript -e 'set volume output muted true'
osascript -e "set volume with output muted"
Unmute
osascript -e 'set volume output muted false'
osascript -e "set volume without output muted"
Convert text to audible speech - say
Say with default voice
say 'hello world'
Specify voice
say -v Wobble 'hello'
Pass text to Say
echo 'hello' | say
Read text from input file
say -f input.txt
Run Say in interactive mode
say -i
Disks
File Vault
FileVault status
fdesetup status
List FileVault enabled accounts
fdesetup list
Disable user from FileVault enabled accounts
fdesetup remove -user USERNAME
Encrypted dmg volumes
Mount encrypted dmg:
hdiutil attach file_name.dmg
Create an encrypted volume:
hdiutil create -encryption AES-256 -fs HFS+ -size 1g encrypted.dmg
pkgbuild
Build .pkg
file with pre/post install scripts only
pkgbuild --identifier com.example --nopayload --scripts ./example-package/scripts ./simple.pkg
Options:
--auth
- values:root
ornone
--install-location
- installation location--info PackageInfo
- use PackageInfo file
Universal binary files
Get info about binary file
lipo -info fileName
Detailed information
lipo -detailed_info fileName
List binary architectures
lipo -archs fileName
Create “fat” binary for multiple architectures
lipo -create fileName-arm.bin fileName-amd64.bin -output fileName
Extract given architecture to a separate universal file
lipo -extract arm64 fileName -output fileName.arm64
Remove given architecture from universal file
lipo -remove arm64 fileName -output fileName2
Create a thin output file
lipo -thin arm64 fileName -output fileName.arm64
plutil - property list util
Read plist files
Read plist
file in human-readable form
plutil -p /path/to/file.plist
Read plist
file, convert it to xml and display to the console
plutil -convert xml1 -o - /path/to/file.plist
Examples
List what apps user has in the dock
plutil -p ~/Library/Preferences/com.apple.dock.plist
List recently accessed files
plutil -p ~/Library/Preferences/com.apple.finder.plist
Display macOS version information
plutil -p /System/Library/CoreServices/SystemVersion.plist
Create
Create an empty plist
file in XML format
plutil -create xml1 file.plist
Check plist
file syntax
plutil -lint file.plist
launch agents
Example plist for launch agent
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.test</string>
<key>ProgramArguments</key>
<array>
<string>touch</string>
<string>/tmp/test</string>
</array>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>
Copy .plist
file to ~/Library/LaunchAgents/com.test.plist
cp test.plist ~/Library/LaunchAgents/com.test.plist
load launch agent
launchctl load ~/Library/LaunchAgents/com.test.plist