As a Mac user, you might not venture into the Terminal as often as Linux users, but mastering a few key commands can significantly enhance your productivity and give you greater control over your system. In this article, we’ll explore ten of the most useful Terminal commands tailored specifically for macOS. Whether you’re a developer, power user, or just curious about your Mac’s capabilities, these commands will help you navigate your system more efficiently.
1. pbcopy and pbpaste - Interact with the Clipboard
The pbcopy
and pbpaste
commands allow you to interact with the macOS clipboard directly from the Terminal. This can be incredibly useful for scripting or quick text manipulation tasks.
Sample usage:
echo "Hello, Clipboard!" | pbcopy
pbpaste
Output:
Hello, Clipboard!
Copying SSH public key:
cat ~/.ssh/id_rsa.pub | pbcopy
This command copies your SSH public key to the clipboard, ready to be pasted into services like GitHub or GitLab.
Why it’s useful:
- Easily copy command output to the clipboard
- Paste clipboard content into Terminal or files
- Integrate clipboard operations into shell scripts
2. open - Open Files and Applications
The open
command is a versatile tool for opening files, folders, and applications from the Terminal. It uses the default application associated with the file type.
Sample usage:
open /Applications/Safari.app
open https://www.apple.com
open -a "Visual Studio Code" myfile.txt
Why it’s useful:
- Quickly open applications or files from the Terminal
- Launch websites in your default browser
- Open files with specific applications
3. caffeinate - Prevent Your Mac from Sleeping
The caffeinate
command prevents your Mac from going to sleep or starting the screen saver. This is particularly useful when you’re running long processes or presentations.
Sample usage:
caffeinate -t 3600
This will keep your Mac awake for 3600 seconds (1 hour).
Why it’s useful:
- Keep your Mac awake during long downloads or processes
- Prevent sleep mode during presentations
- Can be used with other commands to keep the system active until a task completes
4. mdfind - Spotlight Search from the Terminal
mdfind
is the command-line interface to macOS’s Spotlight search. It allows you to perform powerful searches directly from the Terminal.
Sample usage:
mdfind -name "quarterly report"
Output:
/Users/username/Documents/Q2_2024_Quarterly_Report.pdf
/Users/username/Downloads/Q1_2024_Quarterly_Report.docx
Why it’s useful:
- Quickly find files across your entire system
- Perform more complex searches than the Finder interface allows
- Useful for scripting and automation tasks involving file searches
5. screencapture - Take Screenshots
While macOS has built-in keyboard shortcuts for screenshots, the screencapture
command offers more flexibility and options for capturing your screen.
Sample usage:
screencapture -T 10 -t jpg screenshot.jpg
This captures a screenshot after a 10-second delay and saves it as a JPG file.
Why it’s useful:
- Capture screenshots with various options (e.g., timed delay, specific window)
- Save screenshots in different formats
- Easily integrate screenshot capture into scripts or workflows
6. say - Text-to-Speech
The say
command utilizes macOS’s text-to-speech engine to speak text aloud. While it might seem gimmicky at first, it can be quite useful for accessibility purposes or for adding audio cues to scripts.
Sample usage:
say "Your long-running process has completed"
Why it’s useful:
- Create audio notifications for script completion
- Test or use text-to-speech functionality
- Fun tool for experimenting with different voices and languages
7. diskutil - Disk Utility Command Line
diskutil
is the command-line version of macOS’s Disk Utility application. It allows you to manage disks and volumes from the Terminal.
Sample usage:
diskutil list
diskutil eraseDisk JHFS+ "NewDisk" /dev/disk2
Why it’s useful:
- List, mount, unmount, and format disks
- Repair disk permissions and volumes
- Create and manage APFS containers and volumes
8. defaults - Access macOS User Defaults
The defaults
command allows you to read, write, and delete macOS user defaults. This can be used to configure hidden settings or automate preference changes.
Sample usage:
- Show hidden files in Finder:
defaults write com.apple.finder AppleShowAllFiles -bool true
killall Finder
- Change the default screenshot format to PNG:
defaults write com.apple.screencapture type png
killall SystemUIServer
- Disable the “Are you sure you want to open this application?” dialog:
defaults write com.apple.LaunchServices LSQuarantine -bool false
- Show the full POSIX path as Finder window title:
defaults write com.apple.finder _FXShowPosixPathInTitle -bool true
killall Finder
- Disable the warning when changing a file extension:
defaults write com.apple.finder FXEnableExtensionChangeWarning -bool false
killall Finder
- Enable subpixel font rendering on non-Apple LCDs:
defaults write NSGlobalDomain AppleFontSmoothing -int 2
- Show all file extensions in Finder:
defaults write NSGlobalDomain AppleShowAllExtensions -bool true
killall Finder
- Expand save panel by default:
defaults write NSGlobalDomain NSNavPanelExpandedStateForSaveMode -bool true
defaults write NSGlobalDomain NSNavPanelExpandedStateForSaveMode2 -bool true
- Disable auto-correct:
defaults write NSGlobalDomain NSAutomaticSpellingCorrectionEnabled -bool false
- Speed up Mission Control animations:
defaults write com.apple.dock expose-animation-duration -float 0.1
killall Dock
Why it’s useful:
- Modify system and application settings not accessible through the GUI
- Automate preference changes across multiple Macs
- Access and modify application-specific settings
- Customize your Mac’s behavior to suit your workflow
- Quickly apply power user tweaks without third-party software
9. networksetup - Configure Network Settings
networksetup
is a powerful command-line tool for configuring network settings on macOS.
Sample usage and output:
- List all network services:
networksetup -listallnetworkservices
Output:
An asterisk (*) denotes that a network service is disabled.
Wi-Fi
Ethernet
Bluetooth PAN
Thunderbolt Bridge
- Get Wi-Fi network name (SSID):
networksetup -getairportnetwork en0
Output:
Current Wi-Fi Network: MyHomeNetwork
- Turn Wi-Fi power on:
networksetup -setairportpower en0 on
Output:
(No output if successful)
- List preferred Wi-Fi networks:
networksetup -listpreferredwirelessnetworks en0
Output:
Preferred networks on en0:
MyHomeNetwork
WorkNetwork
CoffeeShopWiFi
- Get DNS servers for a specific network service:
networksetup -getdnsservers Wi-Fi
Output:
8.8.8.8
8.8.4.4
- Set a static IP address:
sudo networksetup -setmanual "Ethernet" 192.168.1.200 255.255.255.0 192.168.1.1
Output:
(No output if successful)
Why it’s useful:
- List and manage network interfaces
- Configure Wi-Fi, Ethernet, and other network services
- Retrieve current network settings for troubleshooting
- Automate network configuration changes
- Useful for scripting network changes or troubleshooting
10. brew - The Missing Package Manager for macOS
While not a built-in command, brew
(Homebrew) is an essential tool for many Mac power users and developers. It allows you to install, update, and manage various software packages and tools.
How to Install Homebrew:
To install Homebrew, open Terminal and run the following command:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Follow the prompts in the Terminal. You may need to enter your system password to complete the installation.
After installation, you might need to add Homebrew to your PATH. The install script will provide instructions if this is necessary.
Sample usage:
brew install python
brew update
brew upgrade
Why it’s useful:
- Install and manage software packages not available through the App Store
- Keep your command-line tools and utilities up-to-date
- Access a vast repository of open-source software
Useful Homebrew Links:
- Official Homebrew Website: The main site with installation instructions and documentation.
- Homebrew Documentation: Comprehensive guide to using Homebrew.
- Homebrew Formulae: Search for available packages (formulae) that can be installed via Homebrew.
Additional Tips:
- Use
brew search [package-name]
to find packages. brew info [package-name]
provides detailed information about a package.brew cleanup
removes old versions of installed formulae and frees up disk space.- Consider using
brew bundle
to manage your Homebrew installations across multiple machines.
Homebrew simplifies the process of installing and managing command-line software on macOS, making it an indispensable tool for developers and power users alike.
Bonus: launchctl - Control the macOS Launch Daemon
launchctl
interfaces with launchd
, the service management framework in macOS. It allows you to manage and control background services and scheduled tasks.
Sample usage:
launchctl list
launchctl load /Library/LaunchDaemons/com.example.daemon.plist
Why it’s useful:
- Manage system services and background processes
- Start, stop, and restart services
- Load and unload launch daemons and agents
Conclusion
These ten (plus one bonus) Terminal commands showcase the power and flexibility available to Mac users who are willing to venture beyond the graphical user interface. From managing your clipboard with pbcopy
and pbpaste
to controlling system services with launchctl
, these commands offer a level of control and efficiency that can significantly enhance your macOS experience.
By incorporating these commands into your daily workflow, you can:
- Increase your productivity by automating repetitive tasks
- Gain deeper insights into your system’s operation
- Customize your Mac’s behavior in ways not possible through standard System Preferences
- Troubleshoot issues more effectively
- Expand your understanding of macOS’s underlying Unix-based system
Remember, with great power comes great responsibility. Always be cautious when using Terminal commands, especially those that modify system settings or manage disks. It’s a good practice to backup your important data before making significant changes to your system.
As you become more comfortable with these commands, don’t hesitate to explore their man pages (man command_name
) to discover additional options and capabilities. The Terminal is a powerful tool, and mastering these commands is just the beginning of what you can accomplish with your Mac.
Whether you’re a developer, system administrator, or just a curious Mac user looking to get more out of your machine, these Terminal commands will serve as valuable additions to your toolkit. So fire up Terminal.app, start experimenting, and unlock the full potential of your Mac!