The Ultimate Mac Terminal Commands Guide

The Ultimate Mac Terminal Commands Guide

D
dongAuthor
7 min read

Master Mac terminal basics and advanced features! Learn essential commands like pwd, ls, and cd, plus practical tips for developers. Perfect for beginners!

One of the first things you encounter when starting development is the terminal: a daunting black screen with blinking white text. It might feel overwhelming at first, but the terminal is not as difficult as it seems. In fact, it’s a powerful tool that can make your development workflow much more efficient.

The terminal is an interface for communication between you and your computer. Instead of using a mouse, you can perform various tasks using just the keyboard. Even if you’re not writing code, the terminal can still be incredibly useful. Mac’s terminal, based on a Unix-like operating system, uses commands very similar to Linux or other Unix systems.

Understanding the difference between CLI (Command Line Interface) and GUI (Graphic User Interface) will help you appreciate the advantages of using the terminal. While GUI relies on mouse clicks, CLI allows you to interact with your computer using text commands. CLI might feel unfamiliar at first, but once you get the hang of it, it’s often faster and more precise.

Mastering Basic Commands

Let’s start with essential commands you need to know as a terminal beginner. By learning these, you can easily manage files and perform basic tasks.

Checking Your Current Location

pwd (Print Working Directory)

pwd

This command shows your current directory. When you open the terminal, you usually start in your home directory (/Users/your-username). Use pwd to confirm your current location.

Viewing File and Folder Lists

ls (List)

ls              # View basic list
ls -a           # Include hidden files
ls -l           # Show detailed information
ls -al          # Combine both options

This command displays the content of your current directory. Adding options lets you see more detailed info or include hidden files.

cd (Change Directory)

cd Desktop      # Go to the Desktop folder
cd ..           # Move up one directory
cd ~            # Go back to the home directory
cd -            # Return to the previous directory

Switching between folders is one of the most common tasks during development, especially when working on project folders.

Creating Folders and Files

mkdir (Make Directory)

mkdir my_project        # Create a folder
mkdir -p project/src    # Create nested folders in one step

touch

touch index.html        # Create a file
touch style.css app.js  # Create multiple files at once

These commands are useful when setting up a new project structure and initializing files.

Editing and Viewing File Contents

vi (Vim Editor)

vi filename.txt

Vim allows you to edit files directly in the terminal. Here are some basic shortcuts to get started:

  • i: Enter editing mode
  • Esc: Switch back to command mode
  • :wq: Save and exit
  • :q!: Exit without saving

cat (Concatenate)

cat README.md           # View file contents
cat file1.txt file2.txt # View contents of multiple files at once

Deleting Files and Folders

rm (Remove)

rm filename.txt         # Delete a file
rm -r foldername        # Delete a folder and its contents
rm -i filename.txt      # Prompt for confirmation before deleting

⚠️ Caution: The rm -rf command is very powerful and can delete everything in a specified directory. Always double-check your path before using it!

Handy Utility Commands

clear

clear

Cleans up the terminal screen, making it easier to focus on your current task.

history

history

Displays a list of previously used commands, which is helpful for reusing or referencing past commands.

man (Manual)

man ls          # View the manual for the ls command
man mkdir       # View the manual for the mkdir command

Provides detailed usage information for a command. You can exit the manual by pressing q.

which

which node      # Find the installation path for Node.js
which python    # Find the installation path for Python

Use this command to locate where a program is installed on your system.

Level Up with Advanced Commands

Once you’re comfortable with the basics, you can start exploring more advanced features to optimize your workflow or adjust system settings.

Show Hidden Files

defaults write com.apple.finder AppleShowAllFiles -bool TRUE && killall Finder

Mac hides system and configuration files by default. This command lets you view hidden files like .gitignore or .env in Finder.

Copy Folder Contents

ditto -V ~/source/folder/ ~/new/folder/

This command is great for duplicating project templates or creating backups, as it copies both folder structure and contents.

Change Screenshot Format

defaults write com.apple.screencapture type jpg && killall SystemUIServer

Mac’s default screenshot format is PNG, but you can switch it to JPG with this command to save space.

Disable Sleep Mode

caffeinate

Prevents your Mac from sleeping during long tasks like downloads or server operations. Perfect for uninterrupted work sessions.

Enable Automatic Restart

sudo systemsetup -setrestartfreeze on

Sets your Mac to automatically restart if it freezes. This is a useful setting for ensuring stability on a development machine.

Tips for Becoming a Terminal Pro

Here are some practical tips to help you use the terminal more effectively:

Use Autocomplete

Press the Tab key to autocomplete commands or file names. It’s a lifesaver for long file names.

Reuse Previous Commands

Use the up arrow key to scroll through your command history. It’s a huge time-saver for repetitive tasks.

Run Multiple Commands at Once

mkdir new_project && cd new_project && touch index.html

The && operator lets you chain multiple commands together, executing them in sequence.

Set Up Aliases

Create shortcuts for frequently used commands:

alias ll='ls -al'
alias proj='cd ~/Documents/projects'

Stay Safe While Using the Terminal

The terminal is a powerful tool, but it requires caution. Here are some essential safety tips:

  1. Be Careful with Delete Commands: Commands like rm -rf are irreversible, so double-check before running them.
  2. Use Sudo Sparingly: Only use sudo for trusted commands, as it grants system-level permissions.
  3. Check Your Path: Always run pwd to confirm your location before executing commands.
  4. Backup Important Files: Ensure critical files are backed up regularly.

What’s Next?

Now that you’ve mastered the basics, it’s time to expand your knowledge. Learn Git commands for version control, install Homebrew to manage development tools, or dive into programming with Node.js or Python. Try starting a small project to practice these new skills.

The terminal might feel intimidating at first, but with regular use, it will become an indispensable part of your workflow. As you grow as a developer, your terminal skills will play a key role in your success. Take it step by step, and don’t hesitate to use the man command whenever you need help!

The Ultimate Mac Terminal Commands Guide | devdong