The graphical user interface (GUI) of macOS is a masterpiece of design, offering an intuitive and visually appealing experience that makes Apple computers a joy to use. But beneath this polished veneer lies a powerful engine, a “black box” accessible only through the command line interface (CLI) – the Terminal. For the uninitiated, the Terminal can seem daunting, a relic from a bygone era of computing. For power users, however, it’s a gateway to unparalleled control, customization, and efficiency.
While many common Terminal commands are widely known, there exists a clandestine collection of commands, often less documented or tucked away in obscure corners, that truly empower you to manipulate your macOS environment with surgical precision. These aren’t just tricks; they are tools that can dramatically enhance your workflow, streamline system management, and unlock hidden features that the GUI simply doesn’t expose.
This comprehensive guide is your key to unlocking these secret Mac Terminal commands. Designed for the macOS power user, it delves into advanced functionalities, providing you with the knowledge to optimize your system, automate tasks, troubleshoot complex issues, and truly make your Mac your own.
Why Venture into the Terminal’s Depths?
Before we dive into the commands, let’s briefly touch upon why mastering the Terminal is a worthwhile endeavor for power users:
- Granular Control: The GUI often abstracts complex operations, giving you limited choices. The Terminal, however, offers direct access to system components, allowing for fine-grained control over settings and processes.
- Automation & Scripting: Repetitive tasks can be easily automated using shell scripts, saving you countless hours over time.
- Efficiency: For many tasks, typing a quick command is significantly faster than navigating through multiple menus and windows.
- Troubleshooting & Diagnostics: When things go wrong, the Terminal often provides deeper insights into system behavior, logs, and network activity, which are crucial for effective debugging.
- Unlocking Hidden Features: macOS has many undocumented or hidden features that can only be activated via Terminal commands, allowing for a truly personalized experience.
A Crucial Disclaimer Before You Begin
The Terminal gives you immense power, and with great power comes great responsibility. Many of the commands discussed here can modify core system settings or delete critical files if used incorrectly. Always exercise caution. Before executing any command you’re unsure about, research it thoroughly. It’s also highly recommended to have a recent Time Machine backup or another form of backup before making significant system changes. When in doubt, don’t execute.
Now, let’s unlock some secrets.
1. Mastering System Customization with defaults write
The defaults write command is arguably one of the most powerful and versatile tools for macOS power users. It allows you to read, write, and delete application defaults, which are essentially preference settings stored in .plist files. Many hidden macOS features and behaviors can only be toggled using defaults write.
a. Show Hidden Files in Finder
By default, macOS hides system files and dot files (files starting with a .). While this keeps the Finder tidy, power users often need access to these.
defaults write com.apple.finder AppleShowAllFiles -bool TRUE; killall Finder
To hide them again:
defaults write com.apple.finder AppleShowAllFiles -bool FALSE; killall Finder
killall Finder restarts the Finder process, applying the changes immediately.
b. Change Default Screenshot Format and Location
Tired of PNG screenshots taking up too much space, or want them saved directly to a specific folder?
defaults write com.apple.screencapture type JPG; killall SystemUIServer
Replace JPG with PNG, GIF, TIFF, or PDF.
To change the save location to, for example, a “Screenshots” folder in your Documents:
defaults write com.apple.screencapture location ~/Documents/Screenshots; killall SystemUIServer
c. Remove the Downloads Stack Auto-Arrangement
The Downloads folder in the Dock automatically arranges its contents. If you prefer manual control:
defaults write com.apple.dock allow-downloads-auto-arranging -bool no; killall Dock
d. Add Flexible Spacers to the Dock
Organize your Dock visually by adding transparent spacers between app icons:
defaults write com.apple.dock persistent-apps -array-add '{tile-data={}; tile-type="spacer-tile";}' && killall Dock
Repeat the command to add more spacers. To remove them, simply drag them off the Dock as you would an app icon.
2. Advanced File and Image Manipulation
a. sips: Scriptable Image Processing System
Need to batch resize, crop, rotate, or convert images without opening a heavy image editor? sips is your answer.
Convert all JPEGs in a folder to PNG:
sips -s format png *.jpg --out .
Resize all images in a folder to a width of 1000 pixels, maintaining aspect ratio:
sips -Z 1000 *.jpg
This command is incredibly powerful for web developers, photographers, or anyone dealing with large collections of images.
b. mdfind: Spotlight Search from the Terminal
mdfind taps into Spotlight’s metadata index, allowing for incredibly fast and precise file searches from the command line. It’s much more powerful than find for general content.
Find all .pdf files containing the word “invoice” modified in the last 7 days:
mdfind -onlyin ~/Documents "invoice" -name "*.pdf" -lastused -7d
Find applications by their bundle identifier:
mdfind 'kMDItemCFBundleIdentifier =="com.apple.Safari"'
3. System Health and Power Management
a. pmset: Power Management Settings
pmset allows you to inspect and modify power management settings, going beyond what’s available in System Settings.
List current power management settings:
pmset -g
Prevent your Mac from sleeping for a specific duration (e.g., 2 hours) while running a task:
pmset noidle -t 7200
Schedule your Mac to wake up at a specific time:
sudo pmset schedule wake "MM/DD/YY HH:MM:SS"
Replace the date and time with your desired wake time.
b. caffeinate: Prevent Sleep Temporarily
A simpler alternative to pmset noidle for quick, temporary sleep prevention.
Keep your Mac awake for a specified duration (e.g., 2 hours):
caffeinate -t 7200
Keep your Mac awake as long as a specific process is running (useful for long backups or downloads):
caffeinate -w PID
Replace PID with the Process ID of the application you want to keep active.
Keep your Mac awake as long as the Terminal session is open (or a command is running within it):
caffeinate
Press Ctrl+C to stop it.
c. log stream: Real-time System Logs
When troubleshooting, viewing system logs in real-time can be invaluable. The log stream command provides a live feed of system activity.
log stream --predicate 'process =="Finder"' --info --debug
This command streams only logs related to the Finder process, including informational and debug messages. You can filter by process, subsystem, message type, and more. This is immensely helpful for diagnosing crashes or unexpected behavior.
4. Network and Connectivity Secrets
a. scutil: Service Configuration Utilities
scutil is a low-level command for interacting with the System Configuration framework, allowing you to get and set network configurations that aren’t exposed in System Settings.
Get your current public IP address:
scutil --dns | grep "nameserver[0]" | awk '{print $2}' && curl -s ifconfig.me
(Note: ifconfig.me is an external service; scutil alone won’t get your public IP, but it will show DNS servers.)
Get current DNS server settings:
scutil --dns
Flush DNS cache (similar to dscacheutil but for a specific configuration):
sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder
(While not strictly scutil, this is a common network troubleshooting command)
b. lsof: List Open Files and Network Connections
lsof can reveal which processes are using which files or network ports. It’s a critical tool for security audits and troubleshooting network issues.
List all open files by a specific process (e.g., Safari):
lsof -c Safari
Find which process is listening on a specific port (e.g., port 8080):
sudo lsof -i tcp:8080
This helps identify rogue processes or resolve port conflicts.
5. Security and Privacy Control
a. xattr: Extended Attributes
macOS uses extended attributes to store additional metadata about files, such as where they were downloaded from (com.apple.quarantine), if they’ve been put in iCloud, or custom tags. These can sometimes cause issues.
View extended attributes of a file:
xattr -l filename.txt
Remove all extended attributes from a file (use with caution):
xattr -c filename.txt
Remove the quarantine attribute from an application (useful for unsigned apps, but a security risk):
xattr -d com.apple.quarantine /Applications/MyApp.app
b. csrutil: System Integrity Protection Status
System Integrity Protection (SIP) is a crucial security feature in macOS that prevents even root users from modifying certain system files and processes. It’s good practice to know its status.
Check SIP status:
csrutil status
Output will be “enabled” or “disabled.” Disabling SIP (which requires booting into Recovery Mode) is generally only recommended for developers or advanced troubleshooting and should be re-enabled promptly.
c. tccutil: Reset Privacy Permissions
Transparency, Consent, and Control (TCC) manages permissions for applications accessing sensitive data like contacts, calendars, photos, camera, microphone, etc. Sometimes, an app might be denied access, and you can’t easily reset it in System Settings.
Reset permissions for a specific service (e.g., Camera):
tccutil reset Camera
This will prompt any app that subsequently tries to access the camera to ask for permission again. Useful when an app’s permissions get stuck or you want a fresh start.
6. Advanced System Management
a. launchctl: Managing Daemons and Agents
launchctl is the command-line interface for launchd, the system that manages processes, daemons (system-wide background processes), and agents (user-specific background processes). It’s crucial for system administrators and developers.
List all currently loaded services (can be very long):
launchctl list
Unload a user agent (e.g., a background task from an app):
launchctl unload ~/Library/LaunchAgents/com.example.agent.plist
Load a user agent:
launchctl load ~/Library/LaunchAgents/com.example.agent.plist
Manipulating launchd can solve persistent background process issues or enable custom scripts to run automatically.
b. diskutil: Advanced Disk Management
While many diskutil commands are common, some options offer deeper control than Disk Utility.
Secure erase free space on a volume (can take a long time, use with caution):
diskutil secureErase freespace 1 /Volumes/YourVolumeName
The 1 specifies a single-pass zero-fill erase. Higher numbers (up to 4 or 35) offer more secure but much slower erasure.
Rename a volume without reformatting:
diskutil rename /Volumes/OldName NewName
Verify and repair a disk’s file system (useful in Recovery Mode):
diskutil verifyVolume /
diskutil repairVolume /
c. duti: Default Application Handler
duti allows you to set or query the default application for specific file types or Uniform Type Identifiers (UTIs) from the command line.
Get the default application for .txt files:
duti -l txt
Set Visual Studio Code as the default editor for .md files:
duti -s com.microsoft.VSCode .md all
Find the bundle identifier of an app by using osascript -e 'id of app"AppName"'.
7. Productivity Enhancers
a. pbcopy and pbpaste: Clipboard Interaction
These simple commands allow you to pipe output directly to your macOS clipboard or paste from it.
Copy the contents of a file to the clipboard:
pbcopy < filename.txt
Paste the contents of the clipboard into a file:
pbpaste > newfile.txt
Copy the output of a command directly to the clipboard:
ls -l | pbcopy
Incredibly useful for quickly copying long command outputs, file paths, or text snippets.
b. say: Text to Speech
say converts text into spoken audio using your Mac’s text-to-speech engine.
Make your Mac speak a phrase:
say "Hello, power user! Welcome to the hidden world of macOS."
Read the content of a file:
say -f document.txt
You can even specify a voice using say -v "Alex" "Hello". Useful for quick audio feedback or accessibility.
c. screencapture: Command-line Screenshots
While Cmd+Shift+3/4/5 are great, screencapture offers more options, especially for scripting.
Capture a specific window by clicking it, with a delay:
screencapture -W -T 5 ~/Desktop/window_screenshot.png
The -T 5 adds a 5-second delay.
Capture a specific region (you’ll drag a selection rectangle):
screencapture -s ~/Desktop/region_screenshot.png
Capture a full screen and open it in Preview:
screencapture -C -o ~/Desktop/full_screen_preview.png
The -o flag opens the captured image immediately.
Conclusion: Unleash Your Mac’s True Potential
The macOS Terminal is far more than just a historical artifact; it’s a living, breathing interface that grants you unparalleled authority over your machine. By venturing beyond the familiar GUI and embracing these “secret” or less-known commands, you transform from a user into a master, capable of fine-tuning, troubleshooting, and personalizing your Mac experience to an extraordinary degree.
This guide merely scratches the surface of what’s possible. The true power lies in your curiosity and willingness to experiment. Always remember the warning about caution, practice safe computing, and enjoy the journey of discovering the endless possibilities that lie within your Mac’s black box. Happy commanding!