Essential Terminal Commands for Mac Users

The macOS Terminal is a powerful tool that gives you direct access to the Unix-based core of your Mac. Whether you’re a developer, power user, or just someone looking to streamline tasks, mastering Terminal commands can significantly boost your productivity.

Below is a curated list of useful Terminal commands for macOS, categorized for easy reference.

Networking

Copy table

CommandDescription
ifconfigShow network interfaces and IP addresses
ping [website]Test network connectivity (e.g., ping google.com)
curl [URL]Fetch content from a URL (e.g., curl https://example.com)
nslookup [domain]Look up DNS information
netstat -rnDisplay routing table
lsof -i :[port]Check which process is using a port (e.g., lsof -i :8080)

Ping

ping -c (how many pings) ip address

sample

ping -c 4 123.456.789.123

1. Basic Navigation & File Management

Navigating Directories

CommandDescription
pwdPrint the current working directory
lsList files and folders in the current directory
ls -laList all files (including hidden ones) in long format
cd [directory]Change directory (e.g., cd Documents)
cd ..Move up one directory level
cd ~ or cdGo to the home directory
open .Open the current directory in Finder

File & Folder Operations

CommandDescription
mkdir [folder]Create a new folder (e.g., mkdir Projects)
touch [file]Create a new empty file (e.g., touch notes.txt)
cp [file] [destination]Copy a file (e.g., cp file.txt ~/Documents/)
mv [file] [destination]Move or rename a file (e.g., mv old.txt new.txt)
rm [file]Delete a file (e.g., rm temp.txt)
rm -rf [folder]Force delete a folder and its contents (⚠️ Dangerous!)
cat [file]Display file contents (e.g., cat notes.txt)
less [file]View file contents page by page (press q to exit)
nano [file]Edit a file in the Nano text editor
pbcopy < [file]Copy file contents to clipboard
pbpaste > [file]Paste clipboard contents into a file

2. System Information & Monitoring

Hardware & OS Info

CommandDescription
uname -aDisplay system information (kernel version, etc.)
sw_versShow macOS version details
system_profiler SPHardwareDataTypeDetailed hardware info (CPU, RAM, etc.)
df -hCheck disk space usage (human-readable format)
du -sh [folder]Check folder size (e.g., du -sh ~/Downloads)
topView running processes (press q to exit)
htopInteractive process viewer (install via brew install htop)
uptimeShow how long the system has been running
whoamiDisplay your current username

3. File Permissions & Ownership

CommandDescription
chmod [permissions] [file]Change file permissions (e.g., chmod 755 script.sh)
chown [user]:[group] [file]Change file ownership (e.g., chown user:staff file.txt)
sudo [command]Run a command with superuser privileges
sudo !!Run the last command with sudo

Common Permission Codes:

  • 755 – Owner: read/write/execute, Group/Others: read/execute
  • 644 – Owner: read/write, Group/Others: read-only
  • 777 – Full access (⚠️ Avoid unless necessary!)

4. Package Management (Homebrew)

Homebrew is the best package manager for macOS. Install it first:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

Homebrew Commands

CommandDescription
brew install [package]Install a package (e.g., brew install wget)
brew uninstall [package]Remove a package
brew updateUpdate Homebrew itself
brew upgradeUpgrade all installed packages
brew listList installed packages
brew search [package]Search for a package
brew doctorCheck for issues with Homebrew

5. Process Management

CommandDescription
ps auxList all running processes
kill [PID]Terminate a process by ID (e.g., kill 1234)
killall [process]Kill all instances of a process (e.g., killall Safari)
pkill [process]Kill a process by name
ctrl + cStop the current running command
ctrl + zPause a process (use fg to resume)
jobsList background jobs
bgResume a paused job in the background
fgBring a background job to the foreground

6. Disk & File System Operations

CommandDescription
diskutil listList all disks and partitions
diskutil info [disk]Get detailed info about a disk (e.g., diskutil info disk0)
diskutil eraseDisk [format] [name] [disk]Format a disk (e.g., diskutil eraseDisk APFS "NewDisk" disk2)
mountList mounted drives
umount [disk]Unmount a disk (e.g., umount /dev/disk2s1)
dd if=[input] of=[output]Clone a disk (⚠️ Advanced, use with caution!)

7. Text & File Manipulation

CommandDescription
grep "[pattern]" [file]Search for text in a file (e.g., grep "error" log.txt)
grep -r "[pattern]" [directory]Recursively search in a directory
sed -i '' 's/old/new/g' [file]Replace text in a file (e.g., sed -i '' 's/foo/bar/g' file.txt)
awk '{print $1}' [file]Extract specific columns from a file
sort [file]Sort file contents
uniq [file]Remove duplicate lines (use with sort first)
wc -l [file]Count lines in a file
diff [file1] [file2]Compare two files
tar -xvf [file.tar]Extract a .tar file
tar -czvf [archive.tar.gz] [folder]Compress a folder into .tar.gz
zip -r [archive.zip] [folder]Create a ZIP archive
unzip [file.zip]Extract a ZIP file

8. System Maintenance & Optimization

CommandDescription
sudo softwareupdate -i -aInstall all available macOS updates
sudo purgeClear inactive memory (macOS 10.13 and earlier)
sudo mdutil -E /Rebuild Spotlight index
sudo rm -rf /private/var/log/*Clear system logs (⚠️ Be careful!)
sudo rm -rf ~/Library/Caches/*Clear user cache
defaults write com.apple.finder AppleShowAllFiles YESShow hidden files in Finder
defaults write com.apple.finder AppleShowAllFiles NOHide hidden files
sudo shutdown -r nowReboot immediately
sudo shutdown -h nowShut down immediately

9. Fun & Useful One-Liners

CommandDescription
say "Hello"Make your Mac speak
caffeinate -t 3600Prevent Mac from sleeping for 1 hour
open -a "App Name"Open an app (e.g., open -a "Safari")
screencapture -i screenshot.pngTake an interactive screenshot
networksetup -setairportpower en0 offTurn off Wi-Fi
networksetup -setairportpower en0 onTurn on Wi-Fi
curl -s https://icanhazdadjoke.comGet a random dad joke
fortuneDisplay a random quote (install via brew install fortune)

10. Customizing the Terminal

Change Terminal Appearance

CommandDescription
export PS1="\u@\h \W $ "Customize the prompt (e.g., user@MacBook ~ $)
alias ll='ls -la'Create a shortcut for ls -la
nano ~/.zshrcEdit Zsh configuration (macOS default shell)
source ~/.zshrcReload Zsh config after changes

Install Oh My Zsh (Better Terminal Experience)

sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

Final Tips
✅ Use Tab for auto-completion – Saves time when typing file paths.
✅ Use ↑ and ↓ arrows – Navigate command history.
✅ Use ctrl + r – Search through command history.
✅ Use man [command] – View the manual for any command (e.g., man ls).
✅ Be careful with sudo and rm -rf – These can cause irreversible damage if misused.

Conclusion

The macOS Terminal is a powerful tool that can help you automate tasks, manage files, monitor system performance, and much more. While it may seem intimidating at first, mastering these commands will make you a more efficient Mac user.

Start with the basics, experiment in a safe environment, and gradually explore more advanced commands. Happy terminal-ing! 🚀

Mac OS Ping info

You can use the ping command to verify the connectivity between two network devices that are IP (Internet Protocol) based.

To ping a network device using a system that is running OSX, complete the following:

  1. Click Applications > Utilities > Terminal.
  2. Type ping -c <number of times to ping> <IP address>. The IP address is xxx.xxx.xxx.xxx, where xxx is a number between 0 and 255. For example, to ping 192.168.1.1 five times, you would type ping -c 5 192.168.1.1.

Note: If you do not enter the number of times that you want to ping the IP address, your system will continuously ping the address until you manually stop it. To stop pinging the IP address, press Control + C.

If the ping is successful, you should receive replies from the address that you are trying to ping. If the ping is unsuccessful, you need to diagnose your network setup further.

To verify if your local network adapter is working, you can ping 127.0.0.1, which is a loopback address. The loopback address is a virtual network port for most operating systems.