Top 10 Most Useful Terminal Commands for Mac Users

Posted on:July 23, 2024 at 02:22

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:

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:

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:

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:

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:

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:

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:

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:

  1. Show hidden files in Finder:
defaults write com.apple.finder AppleShowAllFiles -bool true
killall Finder
  1. Change the default screenshot format to PNG:
defaults write com.apple.screencapture type png
killall SystemUIServer
  1. Disable the “Are you sure you want to open this application?” dialog:
defaults write com.apple.LaunchServices LSQuarantine -bool false
  1. Show the full POSIX path as Finder window title:
defaults write com.apple.finder _FXShowPosixPathInTitle -bool true
killall Finder
  1. Disable the warning when changing a file extension:
defaults write com.apple.finder FXEnableExtensionChangeWarning -bool false
killall Finder
  1. Enable subpixel font rendering on non-Apple LCDs:
defaults write NSGlobalDomain AppleFontSmoothing -int 2
  1. Show all file extensions in Finder:
defaults write NSGlobalDomain AppleShowAllExtensions -bool true
killall Finder
  1. Expand save panel by default:
defaults write NSGlobalDomain NSNavPanelExpandedStateForSaveMode -bool true
defaults write NSGlobalDomain NSNavPanelExpandedStateForSaveMode2 -bool true
  1. Disable auto-correct:
defaults write NSGlobalDomain NSAutomaticSpellingCorrectionEnabled -bool false
  1. Speed up Mission Control animations:
defaults write com.apple.dock expose-animation-duration -float 0.1
killall Dock

Why it’s useful:

Note: Be cautious when using `defaults` commands, especially those that modify system behavior. It's a good idea to make note of the original settings before making changes, so you can revert if needed. Some changes may require logging out and back in or restarting your Mac to take effect.

9. networksetup - Configure Network Settings

networksetup is a powerful command-line tool for configuring network settings on macOS.

Sample usage and output:

  1. List all network services:
networksetup -listallnetworkservices

Output:

An asterisk (*) denotes that a network service is disabled.
Wi-Fi
Ethernet
Bluetooth PAN
Thunderbolt Bridge
  1. Get Wi-Fi network name (SSID):
networksetup -getairportnetwork en0

Output:

Current Wi-Fi Network: MyHomeNetwork
  1. Turn Wi-Fi power on:
networksetup -setairportpower en0 on

Output:

(No output if successful)
  1. List preferred Wi-Fi networks:
networksetup -listpreferredwirelessnetworks en0

Output:

Preferred networks on en0:
	MyHomeNetwork
	WorkNetwork
	CoffeeShopWiFi
  1. Get DNS servers for a specific network service:
networksetup -getdnsservers Wi-Fi

Output:

8.8.8.8
8.8.4.4
  1. 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:

Note: Some `networksetup` commands may require administrator privileges (use `sudo`). The `en0` in the examples typically refers to the Wi-Fi interface on most Macs, but this can vary. Use `networksetup -listallhardwareports` to find the correct device name for your system.

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:

Useful Homebrew Links:

  1. Official Homebrew Website: The main site with installation instructions and documentation.
  2. Homebrew Documentation: Comprehensive guide to using Homebrew.
  3. Homebrew Formulae: Search for available packages (formulae) that can be installed via Homebrew.

Additional Tips:

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:

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:

  1. Increase your productivity by automating repetitive tasks
  2. Gain deeper insights into your system’s operation
  3. Customize your Mac’s behavior in ways not possible through standard System Preferences
  4. Troubleshoot issues more effectively
  5. 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!