Skip to content
GEEKnodes

macOS

Basic Mac Terminal Commands: A Beginner's Guide

Learn basic Mac Terminal commands: move around folders, copy and delete carefully, open, caffeinate, pmset, mdfind, diskutil and sudo.

The Mac Terminal is intimidating at first, but with a dozen commands you do in seconds what takes several clicks in the Finder, and things the Finder does not even allow. This guide teaches you to move around folders, handle files without deleting the wrong thing and use macOS-specific tools such as caffeinate, pmset or mdfind.

In short

  • With pwd, ls and cd you know where you are, what is there and how to move. Tab completes names for you.
  • rm deletes without going through the Trash: there is no undo. Use it calmly.
  • sudo runs as administrator; use it only when you know what the command does.

Open Terminal

Press Cmd + space bar, type Terminal and press Return. It is also under Applications → Utilities.

You will see a line like user@MacBook ~ %. The % sign shows the shell is zsh, the default since macOS Catalina. The ~ is your home folder (/Users/your_user).

On a US Mac keyboard, some symbols are typed like this:

SymbolKeys
~ (tilde)Shift + ` (key left of 1)
| (pipe)Shift + \
\ (backslash)\ key
#Shift + 3

Move around folders

pwd
ls
ls -la
ls -lh
cd Documents
cd ..
cd ~
cd -
CommandWhat it does
pwdShows the folder you are in
lsLists the contents
ls -laLists everything, including hidden files, with details
ls -lhDetails with readable sizes (K, M, G)
cd folderEnters a folder
cd ..Goes up one level
cd ~ or just cdReturns to your home folder
cd -Returns to the previous folder

A detail that confuses people: if your system is not in English, the Finder shows translated names for folders such as Documents, Downloads or Desktop, but in Terminal they are always Documents, Downloads and Desktop. The system translates the names only in the interface.

Absolute and relative paths

macOS folder tree from the root to Documents/facturas, with the absolute path and the relative path from the home folder
An absolute path starts at / or ~; a relative one at the folder you are in
  • An absolute path starts with / or ~ and works from anywhere: /Applications or ~/Downloads.
  • A relative path starts where you are: if you are in ~, Documents/facturas is the same as ~/Documents/facturas.

Paths with spaces

Terminal separates words by spaces. If a folder is called My photos, you have three options:

cd "My photos"
cd Mis\ fotos
cd ~/Pictures/"My photos"

The handiest trick: type cd (with a space), drag the folder from the Finder into the Terminal window and press Return. The whole path is pasted with spaces already escaped.

Tab and history: less typing

  • Tab: type the first letters of a file or folder and press Tab. If there are several matches, press Tab again to see them.
  • Up / down arrow: go through previous commands.
  • Control + R: search history. Type part of the command and press Return to run it.
  • history: shows the list of previous commands.
  • Control + C: cancels the running command.
  • Cmd + K: clears the screen (or type clear).

Create, copy, move and delete

mkdir proyectos
mkdir -p proyectos/2026/fotos
touch notas.txt
cp notas.txt copia.txt
cp -R proyectos proyectos-copia
mv copia.txt proyectos/
mv notas.txt apuntes.txt
CommandWhat it does
mkdir nameCreates a folder
mkdir -p a/b/cCreates the whole folder chain at once
touch fileCreates an empty file
cp source destinationCopies a file
cp -R folder destinationCopies a folder with all its contents
mv source destinationMoves, or renames if the destination is another name
rm fileDeletes a file forever
rm -r folderDeletes a folder and everything in it

Be careful with rm

This is the most important rule in the guide: rm sends nothing to the Trash. The file vanishes instantly and there is no Cmd + Z. cp and mv do not ask before overwriting a file with the same name either.

Comparison: deleting in the Finder sends to the Trash and can be recovered; rm in Terminal deletes the file forever
rm skips the Trash: there is no undo

Habits that avoid grief:

  1. Before deleting, run ls with the same pattern to see what will go.
  2. Use rm -i to confirm file by file.
  3. Use cp -i and mv -i so they warn before overwriting.
  4. When in doubt, move to the Trash instead of deleting.
ls *.log
rm -i *.log
mv archivo-dudoso.txt ~/.Trash/

Never copy a rm -rf command from the internet without understanding every part. A misplaced space in a path can delete far more than you intended.

open: the bridge to the Finder

open opens anything as if you double-clicked it.

open .
open informe.pdf
open -a "Preview" foto.jpg
open -R informe.pdf
open https://www.apple.com/
  • open . opens the current folder in the Finder.
  • open -a chooses which app opens it.
  • open -R reveals the selected file in the Finder.

caffeinate: keep the Mac awake

Handy for long downloads, network copies or presentations.

caffeinate
caffeinate -d
caffeinate -t 3600
caffeinate -i rsync -a ~/Documents /Volumes/Copia/
OptionEffect
No optionsPrevents sleep until you press Control + C
-dAlso keeps the display on
-t 3600Only for 3600 seconds (1 hour)
-i commandKeeps the Mac awake while that command runs

On a laptop, closing the lid without an external display still puts it to sleep.

pmset: power and sleep

pmset shows and changes power settings. Reading them does not require administrator rights:

pmset -g
pmset -g batt
pmset -g assertions
pmset sleepnow
  • pmset -g lists current settings.
  • pmset -g batt gives battery percentage and whether it is charging.
  • pmset -g assertions shows which apps are preventing sleep. Very useful if the Mac will not sleep on its own.
  • pmset sleepnow puts it to sleep immediately.

Changing settings requires sudo. For example, turn off the display after 10 minutes:

sudo pmset -a displaysleep 10

mdfind: Spotlight from Terminal

mdfind uses the Spotlight index, so it finds files in seconds.

mdfind presupuesto
mdfind -name factura
mdfind -onlyin ~/Documents contrato
  • Without options it searches names and contents.
  • -name searches names only.
  • -onlyin limits the search to a folder.

If a folder is excluded from Spotlight, mdfind will not see it either.

diskutil list: see drives and partitions

diskutil list
diskutil list external
diskutil info disk4

diskutil list shows all drives (disk0, disk1…), their partitions and volumes. It is the first step before formatting or repairing a drive, to identify the right one. diskutil erase commands do not ask for confirmation: double-check the identifier.

sudo: administrator rights

sudo runs a command as administrator. Terminal asks for your password and, as you type it, nothing appears on screen: no dots or asterisks. That is normal. Type it and press Return.

Three rules:

  1. Use sudo only when a command fails due to permissions and you know why it needs them.
  2. Never combine sudo with an rm you have not checked.
  3. Even with sudo you cannot modify the system volume: it is protected.

Built-in help

Almost every command has a manual:

man ls

Move with the arrows, search with / and quit with q.

Common problems

  • “command not found”: there is a typo in the name or the program is not installed. Check upper and lower case.
  • “No such file or directory”: the path is wrong or has unquoted spaces. Use Tab or drag from the Finder.
  • “Operation not permitted” in Desktop, Documents or Downloads: macOS protects those folders. Give Terminal Full Disk Access in System Settings → Privacy & Security.
  • The sudo password “does not type”: it does, it just is not shown.
  • You overwrote a file with cp or mv: there is no undo. If you have Time Machine, restore it from there; and use -i from now on.

Accessibility preferences

Quick profiles

Text and reading

Text size
Default
Line height
Default
Letter spacing
Default
Word spacing
Default

Colour

Motion and orientation

Cursor