Macoscommand

16 min read3,071 words

macOS Commands

macOS provides a Unix-style command-line environment through Terminal. Modern macOS commonly uses zsh as its default shell.

macOS ↓ Terminal ↓ zsh ↓ Unix commands ↓ macOS system

Most basic commands are very similar to Linux because macOS has a Unix foundation.

1. Terminal

Open Terminal from:

Applications ↓ Utilities ↓ Terminal

You can also search for Terminal using Spotlight.

2. Basic Command Syntax

command [options] [arguments]

Example:

ls -la /Users

Here:

ls → command -la → options /Users → argument

3. Getting Help

man

Displays the manual page.

man ls

Example:

man chmod

Exit:

q

--help

Some commands support:

ls --help

However, macOS's BSD utilities do not all support GNU-style --help, so man is often more reliable.

4. Current Directory — pwd

pwd

Example:

/Users/uday

pwd means:

Print Working Directory

5. List Files — ls

ls

Detailed:

ls -l

Show hidden files:

ls -a

Detailed + hidden:

ls -la

Human-readable sizes:

ls -lh

6. Change Directory — cd

cd Documents

Go to parent:

cd ..

Go to home:

cd ~

Go to root:

cd /

Return to previous directory:

cd -

7. macOS Important Paths

/ → root /Users → users /Applications → applications /System → core system /Library → system libraries/configuration /usr → Unix utilities /tmp → temporary files /dev → device interfaces /var → variable data

Current user's home:

~

Usually:

/Users/username

8. Create Directory — mkdir

mkdir project

Create nested directories:

mkdir -p project/src/components

Result:

project/ └── src/ └── components/

9. Create File — touch

touch file.txt

Multiple files:

touch index.html style.css script.js

10. Display File — cat

cat file.txt

Multiple files:

cat file1.txt file2.txt

11. Read Large Files — less

less largefile.txt

Useful keys:

Space → next page b → previous page q → quit

12. First/Last Lines

First 10 lines:

head file.txt

Last 10 lines:

tail file.txt

Follow a changing log:

tail -f application.log

13. Copy — cp

Copy file:

cp file.txt backup.txt

Copy into directory:

cp file.txt Documents/

Copy directory:

cp -R project backup/

14. Move — mv

mv file.txt Documents/

Rename:

mv old.txt new.txt

Move directory:

mv project ~/Documents/

15. Delete — rm

Delete file:

rm file.txt

Delete multiple:

rm file1.txt file2.txt

Delete directory recursively:

rm -r project

⚠️ Be careful with rm -r. Unlike moving something to Trash through Finder, command-line deletion can be difficult or impossible to recover from normally.

Avoid blindly using:

rm -rf /

or destructive variants of it.

16. Clear Terminal

clear

Keyboard shortcut:

Ctrl + L

17. Find Files — find

Find a file:

find . -name "file.txt"

Find all Python files:

find . -name "*.py"

Search from home:

find ~ -name "*.pdf"

18. Search Text — grep

grep "hello" file.txt

Case-insensitive:

grep -i "hello" file.txt

Recursive search:

grep -R "password" .

For safer code/search work, remember that searching recursively can produce many results.

19. which

Find the executable being used:

which python3

Example:

/usr/bin/python3

For shell commands/functions/aliases, type is also useful:

type python3

20. file

Identify a file type:

file document.pdf

Example:

document.pdf: PDF document

21. stat

Display file metadata:

stat file.txt

Information can include:

  • size
  • permissions
  • owner
  • timestamps
  • inode

22. Disk Space — df

Show filesystem disk usage:

df -h

-h means human-readable.

Example:

Filesystem Size Used Avail /dev/disk3 500G 200G 300G

23. Directory Size — du

du -sh Documents

Current directory:

du -sh .

Find sizes of items:

du -sh *

24. Processes — ps

Show your processes:

ps

Common macOS form:

ps aux

Find a process:

ps aux | grep Safari

25. top

Real-time process monitoring:

top

Shows information such as:

  • CPU usage
  • memory
  • processes
  • process IDs
  • system load

Exit:

q

macOS also provides Activity Monitor as the GUI equivalent.

26. Kill a Process — kill

First find PID:

ps aux | grep application

Then:

kill PID

Example:

kill 1234

If a process does not respond, stronger signals exist, but they should be used carefully because forced termination can cause data loss.

27. open

One very useful macOS-specific command is:

open .

This opens the current directory in Finder.

Open a file:

open document.pdf

Open an application:

open -a Safari

Open a URL:

open https://example.com

28. say

macOS includes a built-in text-to-speech command:

say "Hello Uday"

This makes the Mac speak the text.

Save speech to an audio file:

say -o output.aiff "Hello from macOS"

29. echo

echo "Hello macOS"

Environment variable:

echo $PATH

Username:

echo $USER

Home directory:

echo $HOME

30. Environment Variables

View PATH:

echo $PATH

Set a temporary variable:

export NAME="Uday"

Check:

echo $NAME

Important:

$PATH $HOME $USER $SHELL

31. Shell Information

echo $SHELL

Modern macOS commonly returns something like:

/bin/zsh

Check shell version:

zsh --version

32. System Information — uname

uname

More information:

uname -a

Example information can include:

  • kernel name
  • hostname
  • kernel version
  • architecture

33. macOS Version

sw_vers

Example:

ProductName: macOS ProductVersion: ... BuildVersion: ...

This is one of the most useful macOS-specific commands.

34. Hostname

hostname

Set/change hostname involves administrative configuration and is different from merely displaying it.

35. Current User

whoami

More information:

id

id can show:

  • UID
  • GID
  • groups

36. Logged-in Users

who

Another command:

w

These show information about logged-in users and sessions.

37. Permissions — chmod

Example:

chmod 755 script.sh

Meaning:

Owner → rwx Group → r-x Others → r-x

Numeric values:

r = 4 w = 2 x = 1

38. Change Owner — chown

sudo chown user file.txt

Change owner and group:

sudo chown user:group file.txt

⚠️ Ownership changes affect system access. Use sudo only when necessary.

39. sudo

sudo runs a command with elevated privileges when your account is authorized.

Example:

sudo command

You may be asked for your login password.

Conceptually:

Normal user ↓ sudo ↓ Elevated privileges ↓ System operation

Don't use sudo automatically; use it only when the operation actually requires administrative privileges.

40. Networking — ifconfig

View network interfaces:

ifconfig

You may see interfaces such as:

en0 lo0

Typically:

en0 → network interface such as Wi-Fi/Ethernet lo0 → loopback

Exact interface names vary by Mac.

41. ping

Test connectivity:

ping example.com

Stop:

Ctrl + C

Test a specific IP:

ping 8.8.8.8

42. DNS — nslookup

nslookup example.com

Another useful macOS command:

dig example.com

For DNS configuration:

scutil --dns

43. curl

curl is commonly available on macOS.

Request a webpage:

curl https://example.com

Headers only:

curl -I https://example.com

Download to a file:

curl -o file.zip https://example.com/file.zip

44. Network Connections

Depending on macOS version, commands such as:

netstat

can display network statistics and connections.

For listening TCP sockets, macOS users may also use:

lsof -iTCP -sTCP:LISTEN

45. lsof

lsof means:

List Open Files

In Unix systems, many resources are represented through file-like interfaces.

Find which process is using a port:

sudo lsof -i :8080

Example use:

Port 8080 ↓ Which process? ↓ lsof ↓ PID + program

This is very useful when a development server says:

Address already in use

46. ssh

Connect to a remote SSH server:

ssh username@server

Example:

ssh user@192.168.1.10

SSH is commonly used for:

  • remote administration
  • servers
  • development machines
  • Linux systems

47. scp

Copy a file over SSH:

scp file.txt user@server:/home/user/

Copy from remote system:

scp user@server:/home/user/file.txt .

48. Archive — tar

Create archive:

tar -cf backup.tar project/

Extract:

tar -xf backup.tar

Create gzip-compressed archive:

tar -czf backup.tar.gz project/

Extract:

tar -xzf backup.tar.gz

49. zip and unzip

Create ZIP:

zip -r project.zip project/

Extract:

unzip project.zip

50. Package Management with Homebrew

macOS does not use apt by default like Debian/Kali Linux.

A very common third-party package manager is Homebrew.

After Homebrew is installed:

brew update

Search:

brew search python

Install:

brew install python

Upgrade:

brew upgrade

List installed packages:

brew list

Check:

brew --version

51. macOS Services

macOS uses launchd for system/service and background-process management.

Useful commands include:

launchctl

For example:

launchctl list

This is different from Linux's systemctl.

Linux → systemd → systemctl macOS → launchd → launchctl

52. Disk Information

List disks:

diskutil list

This is an important macOS command.

Example conceptual output:

/dev/disk0 /dev/disk1

Show information about a disk:

diskutil info /dev/disk0

53. Disk Utility

GUI disk management application:

Disk Utility

Terminal equivalent for many operations:

diskutil

You can use it to inspect:

  • disks
  • partitions
  • APFS containers
  • volumes
  • filesystems

⚠️ Disk operations can destroy data. Verify the disk identifier carefully before performing modifications.

54. Mount Information

Show mounted filesystems:

df -h

You can also inspect mounts with:

mount

55. diskutil APFS Information

List APFS containers/volumes:

diskutil apfs list

This is useful for understanding modern Mac storage.

Conceptually:

Physical Disk ↓ APFS Container ↓ APFS Volumes

56. Hardware Information

System information:

system_profiler

For example:

system_profiler SPHardwareDataType

Network hardware:

system_profiler SPNetworkDataType

USB:

system_profiler SPUSBDataType

57. Battery Information

On Mac laptops:

pmset -g batt

This can show battery/charging status.

58. Power Management

pmset -g

Displays power-management settings/status.

pmset is an advanced system utility; changing power settings can affect sleep, wake, and battery behavior.

59. Process Tree

macOS provides process information through commands such as:

ps

For a broader view:

ps aux

You can combine it with grep:

ps aux | grep python

60. Pipes

macOS Terminal supports Unix pipes:

|

Example:

ps aux | grep python

Flow:

ps aux ↓ pipe ↓ grep python ↓ Python processes

61. Output Redirection

Save output:

ls > files.txt

Append:

ls >> files.txt

Error redirection:

command 2> error.txt

Both output and errors:

command > output.txt 2>&1

62. Command Chaining

Run sequentially:

command1 ; command2

Run second only if first succeeds:

command1 && command2

Run second if first fails:

command1 || command2

Example:

mkdir project && cd project

63. Background Processes

Run a command in the background:

command &

Example:

python3 server.py &

View jobs:

jobs

Bring a job to foreground:

fg

64. history

View previous commands:

history

Search shell history interactively:

Ctrl + R

65. Aliases

Create an alias:

alias ll='ls -la'

Then:

ll

Remove:

unalias ll

To make aliases persistent with zsh, they are commonly placed in:

~/.zshrc

66. .zshrc

View:

cat ~/.zshrc

Edit with a command-line editor:

nano ~/.zshrc

After changes:

source ~/.zshrc

This reloads the configuration into the current shell.

67. Text Processing

sort

sort names.txt

uniq

sort names.txt | uniq

wc

wc -l file.txt

Counts lines.

cut

cut -d',' -f1 data.csv

tr

echo "hello" | tr 'a-z' 'A-Z'

Output:

HELLO

68. sed

Stream editor:

sed 's/old/new/g' file.txt

It is commonly used for text transformation.

69. awk

Useful for structured text processing:

awk '{print $1}' file.txt

For CSV-like data:

awk -F',' '{print $1}' data.csv

70. Important macOS-Specific Commands

CommandPurpose
sw_versmacOS version
openOpen file/app/URL
sayText-to-speech
diskutilDisk management
system_profilerHardware/system information
pmsetPower management
launchctlManage/query launchd services
scutilSystem/network configuration
defaultsManage many macOS application preferences
mdfindSpotlight metadata search
networksetupNetwork configuration
hdiutilDisk-image operations

71. Spotlight Search — mdfind

macOS has Spotlight indexing.

Search:

mdfind "report.pdf"

Search a particular location:

mdfind -onlyin ~/Documents "report"

This can be faster than recursively scanning every file with find when Spotlight has indexed the content.

72. defaults

The defaults command interacts with many macOS application preferences.

Read preferences:

defaults read

Read a specific application's domain:

defaults read com.apple.finder

⚠️ defaults can modify application/system preferences. Avoid copying random commands from the internet without understanding what they change.

73. networksetup

View network services:

networksetup -listallnetworkservices

List hardware ports:

networksetup -listallhardwareports

This is useful for macOS network configuration.

74. macOS Command Comparison with Linux

Because both have Unix-like command environments:

PurposemacOSLinux
Listlsls
Directorycdcd
Create directorymkdirmkdir
Copycpcp
Movemvmv
Deletermrm
Readcatcat
Searchgrepgrep
Findfindfind
Permissionschmodchmod
Ownershipchownchown
Processespsps
Networkifconfigip/ifconfig depending on distro
Versionsw_versvaries
Diskdiskutillsblk, fdisk, etc.
Serviceslaunchctlsystemctl on systemd systems
Package managerHomebrew commonlyapt/dnf/pacman etc.

75. macOS vs Windows Commands

TaskmacOSWindows CMD
List fileslsdir
Current directorypwdcd
Change directorycdcd
Create directorymkdirmkdir
Copycpcopy
Movemvmove
Deletermdel
Renamemvren
Read filecattype
Process listpstasklist
Kill processkilltaskkill
Network configifconfigipconfig
Route tracingtraceroutetracert
DNSdig/nslookupnslookup
Diskdiskutildiskpart
System versionsw_versver

76. Practical macOS Troubleshooting

Suppose a local web server is not starting because port 3000 is already occupied.

Step 1 — Find the process

lsof -i :3000

You might get:

COMMAND PID node 12345

Step 2 — Inspect the process

ps -p 12345

Step 3 — If appropriate, stop it

kill 12345

Then try your server again.

This is a very useful workflow for developers.

77. Practical File Workflow

mkdir myproject cd myproject touch index.html echo "<h1>Hello macOS</h1>" > index.html cat index.html

Result:

myproject/ └── index.html

Open it with the default application:

open index.html

78. Important Command Categories

macOS COMMANDS │ ┌────────────────┼────────────────┐ ↓ ↓ ↓ FILES PROCESSES NETWORK ls ps ifconfig cd top ping cp kill curl mv lsof dig rm scutil │ │ │ ├────────────────┼────────────────┤ ↓ ↓ ↓ SYSTEM DISK SERVICES sw_vers diskutil launchctl uname df ... whoami du system_profiler

🧠 Exam Revision

Basic commands

pwd → current directory ls → list files cd → change directory mkdir → create directory touch → create file cp → copy mv → move/rename rm → delete cat → display file find → find files grep → search text

Process commands

ps → process list top → process monitoring kill → terminate process lsof → list open files/resources

Network commands

ifconfig → network interfaces ping → connectivity curl → HTTP/network requests dig → DNS lookup nslookup → DNS lookup scutil → system/network configuration networksetup → network configuration

System commands

sw_vers → macOS version uname -a → kernel/system information whoami → current user id → user/group information system_profiler → hardware information

macOS-specific commands

open → open file/app/URL say → text-to-speech diskutil → disk management launchctl → launchd/service management mdfind → Spotlight search defaults → application preferences pmset → power management

Memory trick

macOS Terminal = Files + Processes + Network + System + Disk + Services

And remember the major differences:

Linux → systemctl + apt + lsblk macOS → launchctl + brew + diskutil Windows → services/sc + winget + diskpart

Keep learning. Keep building. Keep growing.

© 2026 Ugenz Academy