8 Lesser-Known macOS Terminal Commands That Can Save You Time

MacOS users, especially developers and power users, often find themselves using the Terminal to perform a variety of tasks more efficiently than they could through the graphical user interface (GUI). Knowing the right commands can save significant time and effort. While many are familiar with common commands like ls, cd, and mv, fewer are aware of some lesser-known utilities that can be incredibly powerful in the right hands. In this guide, we'll explore eight macOS Terminal commands that can enhance your workflow by performing complex tasks with ease.

The Power of the Terminal

The macOS Terminal is not just an interface for running scripts; it's a gateway to the Unix-like underpinnings of macOS. This offers immense control and flexibility if you know how to harness it. However, mastering it can seem daunting with the multitude of available commands. This guide aims to unveil some of these hidden features.

1. pbcopy and pbpaste

What Are They?

Have you ever wanted to directly copy terminal output to your clipboard for easy pasting elsewhere or utilize clipboard contents in your scripts? That's where pbcopy and pbpaste come in.

How They Work

  • pbcopy: Takes the output from a command and places it in the clipboard.

    bash
    1echo "Copy this text to clipboard" | pbcopy
    2
  • pbpaste: Fetches the clipboard content and dumps it to the terminal or a file.

    bash
    1pbpaste > output.txt
    2

Practical Example

Suppose you're running a script whose output you need to email. Rather than manually copying it, you can automate:

bash
1cat report.txt | pbcopy && echo "Report is ready and copied to clipboard!"
2

This saves time, particularly when dealing with long logs or reports.

2. lsof

What Is It?

lsof, short for 'list open files,' provides a listing of all open files and the processes using them. It's particularly useful for troubleshooting, especially when you can't unmount a disk or kill a stuck process.

Usage

  • View all open files:

    bash
    1lsof | more
    2
  • Find processes using a specific file:

    bash
    1lsof /path/to/file
    2

Practical Example

If a device won’t eject, it might be because a process is still using a file on it. Use lsof to identify the process and take appropriate action:

bash
1lsof /Volumes/YourDrive
2

This will list all processes preventing the device YourDrive from unmounting, allowing you to quit or force quit these applications accordingly.

3. du

Discover Disk Usage

Knowing how your disk space is being used is crucial, especially when you're running out of it. The du (disk usage) command helps you identify how much space files or directories are consuming.

Common Usage

bash
1du -sh /path/to/directory
2
  • -s: Summarizes the total size.
  • -h: Produces a human-readable output (e.g., KB, MB).

Example in Action

To find out which of your folders are the biggest under /Users/YourName/Documents, run:

bash
1du -sh /Users/YourName/Documents/*
2

This command reveals a summary of each item's size within your documents, providing a quick overview to manage your disk space smartly.

4. df

Display Free Disk Space

While du gives insights into file or directory space usage, df displays free space on the entire filesystem, showing you how much is available on each disk or partition.

Quick Command

bash
1df -h
2
  • -h: The human-readable flag, enabling output in standard units such as GB, making it easier to interpret at a glance.

Practical Usage

Running df -h will provide a snapshot of all mounted volumes, crucial for system admins or daily users who need to ensure their disk doesn't unexpectedly fill up.

5. top

System Process Monitoring

To keep track of processes and system resource usage, top is invaluable. It displays all running processes and their CPU usage, memory consumption, and more.

Command Features

  • Simply type top and press enter.

  • To exit, just press q.

Enhancing Understanding

Use top for real-time system monitoring, to see what's consuming your system's resources, from runaway processes to memory leaks. It's an excellent tool for proactive system management, especially if your computer seems slower than usual.

6. kill

Manage Unresponsive Processes

When applications hang or aren’t responding, kill can be used to terminate them effectively. It's particularly useful if your process has become unmanageable through conventional means.

Basic Syntax

bash
1kill [signal] PID
2
  • signal: Specifies the type of kill you want to execute; the most common is -9 for a forced kill.
  • PID: This is the Process ID you can find using ps or top.

Application Example

To forcefully terminate an unresponsive application (found via top), kill it by its PID:

bash
1kill -9 12345
2

This sends a cease command to process 12345. But use caution: a ‘kill -9’ won’t allow the process any cleanup time, potentially causing data loss.

7. say

Make Your Mac Talk

While not necessarily a time-saving command, say can break monotony and surprise your peers. It converts text to speech using your Mac’s text-to-speech engines.

How to Use

bash
1say "Hello, welcome to the macOS Terminal world!"
2

Fun Facts

You can use it to check notifications without looking at the screen, especially if you're engrossed in another task. Additionally, with some scripting ninja skills, say can be integrated into cron jobs to vocalize reminders at set intervals, providing audible alerts for task management.

8. Integrating Terminal Commands into Scripts

Once you’ve mastered these capabilities, the next step is to create scripts that further automate your workflow. Here’s a brief example of how you might combine pbcopy, lsof, and kill to free up resources:

bash
1#!/bin/bash
2
3# Copy essential output to clipboard
4echo "Creating report..." | pbcopy
5
6# Locate and kill process preventing disk ejection
7VOLUME="/Volumes/YourDrive"
8PID=$(lsof | grep "${VOLUME}" | awk '{print $2}')
9if [ ! -z "$PID" ]; then
10 echo "Killing process $PID to eject ${VOLUME}..."
11 kill -9 $PID
12fi
13

Conclusion

MacOS Terminal commands offer a wealth of functionality. From effectively managing processes and drives with lsof and kill, to providing insights into disk usage with du and df, you can optimize your setup significantly. By integrating these commands into scripts and day-to-day operations, you'll not only save time but also unlock potential for efficiency not possible through the GUI alone.

Exploring diverse commands enhances your Terminal experience, leading to more agile, responsive, and effective use of your macOS system. Dive deeper into these tools and transform the way you interact with your Mac.

For further reading to bolster your Terminal expertise, explore the macOS Terminal User Guide directly from Apple Support.

Suggested Articles