Software

10 Terminal commands for power users

Apple's Terminal offers a lot of functionality compared to the macOS GUI. Power users and anyone else who wants to work smarter will benefit from learning these 10 Terminal commands.

Apple's Terminal is a direct link to the underpinnings of your Mac's operating system, and within it is access to all of the possibilities available to your computer. To interact with the Terminal, you only need to enter a command and retrieve the data you want—there's no multi-page windows, clicking all over the screen to set parameters, or waiting for the application to correlate the data it's retrieved into a specific file format.

Anyone can launch the Terminal and begin using it, though many users are content to use the GUI to manipulate data. Power users who want more efficient ways to perform tasks, as well as any user who wants more control over their productivity, should check out these 10 Terminal commands. This article is also available as a free PDF download.

SEE: Supporting Apple in the enterprise: Deploying and managing Apple technology (Tech Pro Research)

1. Cp/Scp

Description: The Cp command is used primarily to copy one or more files and folders from one location to another. The Scp command is used to copy files or folders securely between hosts on a network. The latter uses SSH to encrypt the transfer process and will prompt for authentication if it is required when establishing connectivity between the source and destination targets.

Usage:

cp [-R [-H | -L | -P]] [-fi | -n] [-pvX] Source_file(s) Target_Folder

scp [-12346BCpqrv] [-c cipher] [-F ssh_config] [-i identity_file] [-l limit][-o ssh_option] [-P port] [-S program] [[user@]host1:]file1 ... [[user@]host2:]file2

Examples:

Copy a file from one folder to another on the same computer
cp /path/to/source/file.ext /path/to/destination

Copy a folder and all the directories it contains (recursive) to another directory on the same computer
cp -R /path/to/source/folder /path/to/destination

Securely copy a file using SSH protocol 2 over IPv4 only to a remote computer
scp -2 -4 user1@server.locall:/path/to/source/file.ext user2@server.local:/path/to/destination

Securely copy a directory using 3DES encryption cipher to a remote computer
scp -c 3des -R user2@server.local:/path/to/source/folder user1@server.local:/path/to/destination

2. Caffeinate

Description: The Caffeinate command is a utility that prevents your Mac from sleeping, despite what is set in the energy settings. It is useful when working on a project or a task that requires a steady processing stream to not be interrupted by the Mac going to sleep. It is not a permanent setting, merely one that overrides the system settings for a short period of time, designated by either a timeout (in seconds) or once a process has completed, if one is specified in the syntax.

Usage: caffeinate [-disu] [-t timeout] [-w pid] [utility arguments...]

Examples:

Prevent the Mac from sleeping for two hours
caffeinate -u -t 7200

Prevent the Mac from sleeping until the PID value of a process has completed
caffeinate -w 33128

Prevents a Mac that is connected to AC power from sleeping
caffeinate -s

3. Find/Locate/Whereis

Description: The commands above are grouped together in this article because they all pertain to search for a variety of items within your Mac (although the commands are different in implementation). The Find command searches for files that match a specified criteria.

Usage: find [-H | -L | -P] [-EXdsx] [-f path] path ... [expression]

Examples:

List all files and folders that are located in your home directory
find $HOME

Search for every application (.APP) on the the computer
find / -iname *.app

Description: The Locate command searches the locate.updatedb (database), which stores pathnames information for all files. The search can be performed after the database is created, and will match specified criteria. The initial database creation will depend on the speed of the computer; however, after it is initialized manually the first time, the database will update automatically.

Usage: locate [-0Scims] [-l limit] [-d database] pattern ...

Examples:

To create the locate database
sudo /usr/libexec/locate.updatedb

Locate all files in the database that end with the .docx extension
locate '*.docx'

Description: The WhereIs command searches the system's directories for specified programs and applications, as well as prints out their paths onscreen.

Usage: whereis [program ...]

Examples:

Display the location of the Automator.app
whereis automator

Display the location of the Ping command
whereis ping

4. Opendiff

Description: The Opendiff command is a utility that provides a way to graphically compare files and folders by way of the FileMerge application. After comparing two files or folders, the user may optionally merge the files or folders into one, if desired. This requires Xcode to be installed on the host Mac.

Usage: opendiff file1 file2 [-ancestor ancestorFile] [-merge mergeFile] opendiff dir1 dir2 [-ancestor ancestorDirectory] [-merge mergeDirectory]

Examples:

To compare two files
opendiff /path/to/file1.ext /path/to/file2.ext

To compare the contents of two folders and merge them into one
opendiff /path/to/folder1 /path/to/folder2 -merge /path/to/new/folder

5. Grep/Pgrep

Description: The Grep command provides a way to search input file(s) for specific strings of text that match to the given pattern specified in the command.

Usage: grep <options> "Search String" [filename]

Examples:

Search a drive for a file that contains the text string "power users"
grep -a 'power users' /dev/disk0

Search all the files stored in the home directory for the text string "power users"
grep -d recurse 'power users' $HOME

Description: Similar to what the Grep command does with files and folders, Pgrep provides a way to search the process table for matches to strings specified in the command.

Usage: pgrep [-Lafilnoqvx] [-F pidfile] [-G gid] [-P ppid] [-U uid] [-d delim] [-g pgrp] [-t tty] [-u euid] pattern ...

Examples:

To list all the processes that are owned by the user User1
pgrep -u User1

To kill all processes that belong to any users that are part of the group ID "20"
pkill 'pgrep -g 20'

SEE: Top 20 Apple keyboard shortcuts for business users (free PDF) (TechRepublic)

6. Qlmanage

Description: The Qlmanage command is primarily a Quick Look server debug and management tool; it also offers users the ability to perform a quicklook on a file or multiple files to visually confirm the contents of a document prior to running additional commands on them.

Usage: qlmanage -rmtph [-d debugLevel] [-x] [-i] [-s size] [-f factor] [-c contentTypeUTI [-g generator]] [file ...]

Examples:

To generate a thumbnail view of a file
qlmanage -t /path/to/file.ext

To generate thumbnail views for multiple files contained in a directory
qlmanage -t /path/to/directory/*.*

7. Pbcopy/Pbpaste

Description: The Pbcopy command is used to copy data from an input source, such as a file, to the clipboard, similar to the standard copy command. However, pbcopy can be used to grab specific strings of information or dump entire file contents, as needed.

Usage: pbcopy [-pboard {general | ruler | find | font}]

Examples:

Copy the entire contents of a file to the clipboard
pbcopy < /path/to/file.ext

Copy a list of files contained in a directory to the clipboard
ls /path/to/directory | pbcopy

Description: Similar to the paste command, Pbpaste will paste the contents of the clipboard after running the Pbcopy command to a file.

Usage: pbpaste [-pboard {general | ruler | find | font}] [-Prefer {ascii | rtf | ps}]

Examples:

Paste the contents of the clipboard to a .txt file
pbpaste > /path/to/file.ext

Set pbpaste to prefer clipboard content that is Encapsulated PostScript encoded first
pbpaste -prefer ps

8. Ls

Description: The Ls command is used to list the contents of a directory.

Usage: ls [-Option(s)] [file ...]

Examples:

Display the contents of a specific directory
ls /path/to/directory

Display the contents of a specific directory with characteristics such as permissions, time stamps, and size
ls -l /path/to/directory

Display the contents of a specific directory, including hidden files
ls -a /path/to/directory

9. Say

Description: The Say command converts text to speech, which is narrated through the speakers of your Mac computer.

Usage: say [-v voice] [-o out.aiff | -n name:port ] [-f file.in | string ...]

Examples:

To have the computer speak a specified message in its default voice
say "Hello, this is your Mac speaking"

To identify a list of available voices to choose from
say -v ?

To have the computer narrate the contents of a text file in a female's voice
say -v Victoria -f /path/to/file.ext

10. Screencapture

Description: The Screencapture command allows users to take a screenshot, capturing any part of the screen or the whole screen, as needed.

Usage: screencapture [options] [file]

Examples:

Capture the entire screen and save to JPG file
screencapture /path/to/file.jpg

Capture a specific window instead of the whole screen and save to PNG file (must click window to capture with mouse after executing command)
screencapture -W /path/to/file.png

Capture a screenshot of the whole screen to the clipboard, with a 10 second delay
screencapture -T 10 -c

What are your favorite Terminal commands?

Do you have a particular set of Terminal commands that you rely on daily to accomplish your work? If so, please let us know below in the comments section—we're always looking for feedback from our readers.

Also see

programmeristock-849858410nd3000.jpg
Image: nd3000, Getty Images/iStockphoto

About Jesus Vigo

Jesus Vigo is a Network Administrator by day and owner of Mac|Jesus, LLC, specializing in Mac and Windows integration and providing solutions to small- and medium-size businesses. He brings 19 years of experience and multiple certifications from seve...

Editor's Picks

Free Newsletters, In your Inbox