keepcalculator

06 Numbers and Powers

Mac Terminal Commands: The Essential Guide

Card No. 06·76101 · Updated · Standard formula, shown below

Mac Terminal commands let you control your Mac through text, giving you direct access to the Unix system underneath macOS. You open Terminal from Applications/Utilities, type a command, and press Return to execute it. This guide covers the most useful commands for everyday tasks, file management, system information, and troubleshooting.

Why Learn Terminal Commands?

Terminal is a powerful tool that can save time and give you control that the graphical interface doesn’t offer. For example, you can batch rename files, find large files, or check network details in seconds. It’s also essential for developers and system administrators who need to run scripts or manage servers. While the learning curve is real, starting with a few safe commands builds confidence quickly.

Getting Started: Basic Navigation

When you open Terminal, you see a prompt where you type commands. The first thing to learn is moving around the file system:

  • pwd – prints the current directory (folder) you’re in.
  • ls – lists files and folders in the current directory. Add -la to see hidden files and details.
  • cd – changes directory. For example, cd Documents moves into the Documents folder. cd .. goes up one level, and cd ~ goes to your home folder.
  • mkdir – creates a new folder, e.g., mkdir Projects.
  • rmdir – removes an empty folder.

These five commands cover most basic navigation. Try them in a test folder to get comfortable.

File Management Commands

Managing files from Terminal is faster than dragging and dropping for repetitive tasks. Here are the core commands:

  • cp – copies files or folders. Example: cp file.txt backup.txt.
  • mv – moves or renames. mv file.txt newname.txt renames; mv file.txt ~/Documents/ moves.
  • rm – deletes files. Use rm -r for folders. Caution: this bypasses the Trash and cannot be undone.
  • cat – displays a file’s contents in the terminal.
  • less – lets you view a file page by page (press q to quit).
  • find – searches for files. Example: find . -name "*.pdf" finds all PDFs in the current folder and subfolders.

These commands are powerful, especially when combined with wildcards like * (any characters) and ? (one character).

System Information and Monitoring

Terminal can quickly show you details about your Mac that are buried in menus:

  • top – displays real-time CPU, memory, and process usage. Press q to quit.
  • df -h – shows disk space usage in a human-readable format.
  • du -sh * – shows the size of each item in the current folder.
  • system_profiler – gives detailed hardware and software info. For a quick overview, try system_profiler SPHardwareDataType.
  • uname -a – prints the kernel version and architecture.
  • netstat -an – lists network connections and listening ports.

These are read-only commands, so they’re safe to experiment with.

Working with Text and Permissions

Many commands output text, and you’ll often want to search or manipulate it. The most useful text tools:

  • grep – searches for text. Example: grep "error" log.txt shows lines containing “error”.
  • wc – counts lines, words, and characters. wc -l file.txt gives the line count.
  • echo – prints text to the screen, useful in scripts.
  • chmod – changes file permissions. For example, chmod +x script.sh makes a script executable.
  • sudo – runs a command with administrator privileges. Use sparingly and only when needed, as it can modify system files.

Permissions can be tricky; a common mistake is using chmod 777, which gives everyone full access. Prefer specific permissions like 755 for scripts or 644 for regular files.

Networking Commands

Terminal is handy for network diagnostics:

  • ping – tests connectivity to a host. Example: ping google.com (press Control+C to stop).
  • curl – transfers data from or to a server. curl -I https://example.com fetches headers.
  • ssh – securely connects to remote servers. ssh user@host.
  • ifconfig – shows network interfaces and IP addresses.
  • nslookup – queries DNS records. Example: nslookup example.com.

These are standard tools that work on any Unix-like system, so skills transfer to Linux servers.

Common Problems and How to Fix Them

New users often hit a few snags:

  • Command not found – You may have mistyped, or the command isn’t installed. Check spelling, then consider installing via Homebrew (a package manager) if it’s a common tool.
  • Permission denied – You don’t have write access. Use sudo only if you understand the risk, or change permissions with chmod.
  • Stuck in a process – Press Control+C to stop the current command. Control+Z suspends it.
  • Terminal freezes – Try Control+C first, then Control+D to exit the shell if needed.

Always double-check before using rm or sudo – a typo can delete the wrong file or change system settings.

Frequently Asked Questions

How do I open Terminal on a Mac?

Go to Applications > Utilities > Terminal. Or press Command+Space to open Spotlight, type “Terminal,” and press Return.

Is Terminal safe to use?

Most commands are safe, but some (like rm and sudo) can cause irreversible damage if misused. Stick to read-only commands at first, and always understand what a command does before running it.

What does sudo do?

sudo runs a command with root (administrator) privileges. It’s necessary for some system tasks, but it bypasses normal protections, so use it only for specific commands you trust.

How do I quit Terminal?

Type exit and press Return, or close the window. If a process is running, you may need to stop it first with Control+C.

Can I undo a command?

No, Terminal doesn’t have an undo. For destructive commands like rm, consider using trash (a third-party tool) or moving files to a temporary folder instead of deleting.

Where can I learn more?

Type man followed by any command (e.g., man ls) to read its manual page. Apple’s official documentation and Unix tutorials are also good resources.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →