Back Original

Useful built-in macOS command-line utilities

Sometimes when I'm bored, I like to look at the list of macOS Bash commands. Here's some commands that I found interesting:

Access your Keychain programmatically

If you store your secrets in the Keychain (and you should!), you can access them programmatically using security.

security find-internet-password -s "https://example.com"

I found this useful for writing automated scripts that used locally-stored credentials.

Link: https://ss64.com/mac/security.html

Bonus tip: If you are using 1Password, there is a 1Password CLI that you can use to access your 1Password items from the command line.

Open files from the terminal

If you want to open a file from the terminal, you can use the open command.

open file.txt

This will open the file in the default application for that file type, as if you had double-clicked it in the Finder.

Link: https://ss64.com/mac/open.html

Copy and paste

pbcopy and pbpaste are command-line utilities that allow you to copy and paste text to the pasteboard (what other operating systems might call the "clipboard").

pbcopy takes whatever was given in the standard input, and places it in the pasteboard.

echo "Hello, world!" | pbcopy;

pbpaste takes whatever is in the pasteboard and prints it to the standard output.

pbpaste

>> Hello, world!

This is very useful for getting data from files into the browser, or other GUI applications.

Links:

UTC date

If you work with servers a lot, it can be useful to know the current time in UTC, when e.g. looking at server logs.

This is a one-liner in the terminal:

date -u

Alternatively, you can use

TZ=UTC date

Link: https://ss64.com/mac/date.html

Internet speedtest

If you want to run an Internet speedtest, you can run one directly from the terminal with

networkQuality

Link: https://ss64.com/mac/networkquality.html

Prevent your Mac from sleeping

If you are want to keep your Mac from sleeping, you can run caffeinate in the terminal.

caffeinate

caffeinate will keep your Mac awake until you stop it, e.g. by pressing Ctrl+C. caffeinate used to be a third-party tool, but it is now built-in to macOS.

I use this mostly to prevent my Mac from sleeping when I am running a server.

Link: https://ss64.com/mac/caffeinate.html

Generate UUIDs

If you need to generate a UUID, you can use the uuidgen command.

uuidgen

By default uuidgen outputs a UUID in uppercase. You can combine this with tr and pbcopy to copy the UUID to the clipboard in lowercase.

uuidgen | tr '[:upper:]' '[:lower:]' | pbcopy

I use this a lot when writing unit tests that require IDs.

Link: https://ss64.com/mac/uuidgen.html

Honourable mentions

  • mdfind: Spotlight search, but in the terminal. I generally use Spotlight itself (or rather the excellent Raycast). Link
  • say: This command makes your Mac speak the text you give it. Link
  • screencapture: This command allows you to take screenshots and save them to a file. I prefer using cmd-shift-5 for this. Link
  • networksetup: This command allows you to configure your network settings programmatically. I found its API very intimidating, and so I haven't really used it much. Link