Introduction
Understanding macOS terminal commands can greatly enhance your efficiency and control over your system. From managing clipboard contents and opening applications to configuring system settings and handling file attributes, these commands offer powerful solutions to everyday tasks. By incorporating these techniques into your workflow, you can streamline operations, automate processes with osascript, and fully leverage the capabilities of your macOS environment. Keep experimenting and exploring to unlock even more potential from your terminal commands.
In this article we will focus on the macOS specific command line tools, for generic unix/linux commands you can check other articles:
- Navigating the Linux Filesystem A Short Guide to Listing Files and Directories
- Basic Linux System Commands
- Short Guide to Bash Scripting
TL;DR
You can find a shorter cheat sheet version of this article here.
Table of contents
Open Table of contents
Basic Commands
Clipboard Management
Managing the clipboard from the terminal can be incredibly useful for quickly copying and pasting text, command outputs, or even entire files. Here are some handy clipboard commands:
-
Copy Text to Clipboard:
echo "text to copy" | pbcopy
This command sends the specified text directly to the clipboard.
-
Copy Command Output to Clipboard:
whoami | pbcopy
The output of the
whoami
command is copied to the clipboard. -
Copy File Contents to Clipboard:
pbcopy < ~/path/to/file
This command copies the contents of a file to the clipboard.
-
Paste from Clipboard:
pbpaste
Displays the contents of the clipboard in the terminal.
-
Save Clipboard Contents to a File:
pbpaste > file-name.txt
This saves the clipboard’s contents to a specified file.
-
Execute Clipboard Contents:
pbpaste | zsh
Executes the clipboard contents in the
zsh
shell. -
Sort and Remove Duplicate Lines from Clipboard Content:
pbpaste | sort -u | pbcopy
This command sorts the clipboard content and removes duplicate lines before copying it back to the clipboard.
Checking Application Entitlements
To check the entitlements of an application, which are permissions granted to it by macOS, use the following command:
- Check Application Entitlements:
codesign -d -vv --entitlements - /Applications/Your-Application.app
Opening Applications and Files
The open
command is a tool to open applications, files, or URLs directly from the terminal. Here are various ways to use it:
-
Open an Application:
open -a Notes
This command opens the Notes application.
-
Open a New Instance of an Application:
open -n -a Notes
Opens a new instance of the Notes app, even if it’s already running.
-
Open a Fresh Instance Without Restoring State:
open -F -n -a Safari
Opens Safari in a fresh instance without restoring the previous state.
-
Open a File in TextEdit:
open -e file.txt
Opens
file.txt
in TextEdit. -
Open Finder in Current Directory:
open .
Opens Finder in the current working directory.
-
Reveal a File in Finder:
open -R test.txt
Reveals
test.txt
in Finder instead of opening it. -
Open Application Hidden:
open -a TextEdit -j
Opens TextEdit hidden.
-
Open Application Without Bringing to Foreground:
open -a TextEdit -g
Opens TextEdit without bringing it to the foreground.
-
Open URL with Default Application:
open -u smb://127.0.0.1
Opens the URL with the default application.
-
Write Command Output to Temp File and Open in Default Text Editor:
ls / | open -f
Opening Web Pages
You can also open web pages directly from the terminal:
-
Open Web Page in Default Browser:
open https://example.com
-
Open Web Page in Safari:
open -a Safari https://example.com
-
Open Local HTML File in Safari:
open -a Safari file:///Users/user/Downloads/test.html
Additional Arguments
The open
command supports additional arguments to customize its behavior:
--args
- Pass remaining arguments to the opened application.--env VAR
- Add the variable to the application’s environment.--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.
Finder
You can control Finder’s behavior using defaults
commands:
-
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
Extended attributes provide additional metadata about files. You can manage these using the xattr
command:
-
List File Attributes:
xattr -l /path/to/file
-
List File Attributes Recursively:
xattr -lr .
-
Remove Quarantine from File:
xattr -d com.apple.quarantine /path/to/file
-
Remove Download Location Metadata:
xattr -d com.apple.metadata:kMDItemWhereFroms /path/to/file
-
Remove All Attributes:
xattr -c /path/to/file
Spotlight
Spotlight search can be leveraged from the terminal using mdfind
:
-
Search for PDFs:
mdfind -name .pdf
-
Limit Search to Specific Directory:
mdfind -onlyin ~/Documents -name .pdf
-
Search for Images in Specific Directory:
mdfind image -onlyin ~/Documents
-
Print Total Number of Matches:
mdfind .pdf -onlyin ~/Documents -count
Disabling/Enabling Spotlight
You can control Spotlight indexing with mdutil
:
-
Disable Indexing:
mdutil -i off /
-
Enable Indexing:
mdutil -i on /
-
Erase Spotlight Index:
mdutil -E /
Computer Name Management
You can get and set various computer names using scutil
:
-
Get Host Name:
scutil --get HostName
-
Get Local Host Name:
scutil --get LocalHostName
-
Get Computer Name:
scutil --get ComputerName
Changing Computer Name
-
Set Host Name:
sudo scutil --set HostName new-host.name.com
-
Set Local Host Name:
sudo scutil --set LocalHostName new-host
-
Set Computer Name:
sudo scutil --set ComputerName new-host
AppleScript (osascript)
Use osascript
to execute AppleScript or JavaScript from the terminal:
-
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 Control
Control the system volume using osascript
:
-
Change Volume:
osascript -e "set volume output volume 50"
-
Mute:
osascript -e 'set volume output muted true'
-
Unmute:
osascript -e 'set volume output muted false'
Text-to-Speech with say
Convert text to speech using the say
command:
-
Say Text 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
Disk Management
FileVault
Manage FileVault encryption status and users:
-
FileVault Status:
fdesetup status
-
List FileVault Enabled Accounts:
fdesetup list
-
Remove User from FileVault:
fdesetup remove -user USERNAME
Encrypted DMG Volumes
-
Mount Encrypted DMG:
hdiutil attach file_name.dmg
-
Create Encrypted Volume:
hdiutil create -encryption AES-256 -fs HFS+ -size 1g encrypted.dmg
Building Packages with pkgbuild
Create installer packages with pkgbuild
:
- Build
.pkg
with Pre/Post Install Scripts: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
Manage universal (fat) binaries with lipo
:
-
Get Information 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 Architecture to Separate File:
lipo -extract arm64 fileName -output fileName.arm64
-
Remove Architecture from Universal File:
lipo -remove arm64 fileName -output fileName2
-
Create Thin Output File:
lipo -thin arm64 fileName -output fileName.arm64
Property List Utility (plutil
)
Manage property list files with plutil
:
Reading Plist Files
-
Read Plist in Human-Readable Form:
plutil -p /path/to/file.plist
-
Convert Plist to XML and Display:
plutil -convert xml1 -o - /path/to/file.plist
Common Plist Examples:
-
List Dock Apps:
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
Creating Plist Files
-
Create Empty Plist in XML Format:
plutil -create xml1 file.plist
-
Check Plist Syntax:
plutil -lint file.plist
Conclusion
Mastering these macOS terminal commands can significantly boost your productivity and streamline your workflow. Whether you’re manipulating clipboard contents, opening applications and files, managing Finder settings, or utilizing Spotlight for advanced searches, these commands offer powerful and efficient ways to interact with your system. By incorporating these techniques into your daily routine, you can enhance your ability to perform a variety of tasks more quickly and effectively. Explore these commands and discover how they can simplify your macOS experience.