macOS Command-Line Guide

A quick reference for the commands covered in your EEE120 videos. Let's master the terminal! 💻


Navigation & Information

Commands for moving around and getting information about your system.

Command Description
pwd Print Working Directory (shows your current location).
ls List files and directories in the current location.
cd [directory] Change Directory. cd .. goes up one level. cd alone goes to your home directory.
whoami Shows the username of the current account.
history Shows previously used commands. The Up Arrow Key scrolls through them one by one.
man [command] Shows the manual page for a command. Your go-to for help!
which [command] Shows the full path/location of a command's executable file (e.g., which ls).
clear Clears the terminal screen for a clean workspace.
ping [ip_address] Sends a packet to a network address to check for a connection.

File & Directory Management

Commands for creating, deleting, and organizing your files.

Command Description
mkdir [name] Make a new directory.
rmdir [name] Remove an empty directory.
touch [filename] Creates a new, empty file.
cp [source] [destination] Copy a file or directory.
mv [source] [destination] Move a file or directory. Also used to rename files (e.g., mv oldname.txt newname.txt).
rm [filename] Remove (delete) a file. Be careful, there's no undo!
ln [target] [link_name] Creates a hard link between two files, making them act as one.

Common Command Flags (Options)

Flags modify the behavior of commands. They usually start with a dash - and can often be combined (e.g., ls -al).

Command with Flag Description
ls -l Lists files in long format, showing permissions, owner, size, and modification date.
ls -a Lists all files, including hidden ones (those starting with a dot .).
cp -r Copies recursively, meaning it copies a directory and everything inside it.
rm -r Removes a directory and its contents recursively. Use with extreme caution!

Viewing & Editing Files

Commands to see what's inside your files or to edit them.

Command Description
more [filename] Displays file content one screen at a time. Press space to advance.
less [filename] Similar to more, but allows you to scroll both up and down.
head [filename] Shows the first 10 lines of a file.
cat [filename] Concatenates and displays the entire content of a file at once. Can also combine files.
nano [filename] A simple, beginner-friendly command-line text editor.

Power Tools: Search, Sort, & Redirection

Combine these to create powerful command sequences.

Tool Description
find . -iname [filename] Searches for files by name within the current directory (.).
grep "[pattern]" [file] Searches for a specific text pattern within a file.
sort [file] Sorts the lines of a text file alphabetically.
| (Pipe) Takes the output of one command and "pipes" it as the input to another command. Example: sort file.txt | grep "a"
> (Output Redirection) Redirects the output of a command into a file, overwriting the file. Example: ls > files.txt
< (Input Redirection) Uses the content of a file as the input for a command. Example: ./program < input.txt

Permissions

Control who can read, write, or execute your files.

Command Description
ls -l The first part of the output (e.g., -rwxr-xr--) shows permissions for Owner, Group, and Other users. (r=read, w=write, x=execute).
chmod [###] [file] Change mode (permissions). Each digit corresponds to Owner, Group, and Other. Use numbers to represent permissions: 4=Read, 2=Write, 1=Execute. Add them up (e.g., 7 = rwx, 6 = rw-, 4 = r--).
chown [user] [file] Change the owner of a file.
chgrp [group] [file] Change the group of a file.
sudo [command] Super user do. Runs a command with administrative privileges. Requires a password.

Bash Scripting & Aliases

Automate tasks and create shortcuts.

Example Script (`firstScript.sh`)

#!/bin/bash

# This line is called a "shebang" and tells the system to use bash to run this file.

i=1
echo "Enter a number: "
read a

# Check if the input is a positive integer
if ! [[ $a =~ ^[0-9]+$ ]] || (( a < 1 )); then
    echo "Error: please enter a positive integer." >&2
    exit 1
fi

# Loop from 1 to the number provided
while [[ $i -le $a ]]; do
    echo "$i"
    if (( i % 2 == 0 )); then
        echo "$i is divisible by 2"
    else
        echo "$i is odd"
    fi

    if (( i % 6 == 0 )); then
        echo "$i is divisible by 6"
    fi
    (( i += 1 ))
done

# You can run any other command, too!
ls

Running a Script

1. Make it executable: chmod 755 yourScript.sh
2. Run it from the current directory: ./yourScript.sh

Aliases (Shortcuts)

Command Description
alias fs='./firstScript.sh' Creates a temporary shortcut. Now, typing fs will run ./firstScript.sh.
unalias fs Removes the alias.