Show description
EEE120 - Mac Terminal Cheat Sheet
EEE120 - Mac Terminal Cheat Sheet
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.…
EEE120 - Mac Terminal Cheat Sheet
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>EEE120 - Mac Terminal Cheat Sheet</title>
<style>
body {
background-color: #0d0d0d;
color: #00ff41;
font-family: 'Menlo', 'Monaco', 'Courier New', monospace;
padding: 2em;
line-height: 1.6;
}
h1, h2 {
border-bottom: 1px solid #00ff41;
padding-bottom: 10px;
}
h1 {
text-align: center;
font-size: 2.5em;
}
code {
background-color: #222;
padding: 2px 6px;
border-radius: 4px;
font-weight: bold;
}
pre code {
display: block;
padding: 1em;
background-color: #1a1a1a;
border: 1px solid #00ff41;
border-radius: 5px;
white-space: pre-wrap;
word-wrap: break-word;
}
.container {
max-width: 900px;
margin: auto;
}
table {
width: 100%;
border-collapse: collapse;
margin-top: 1.5em;
}
th, td {
border: 1px solid #333;
padding: 12px;
text-align: left;
}
th {
background-color: #1a1a1a;
}
hr {
border: 0;
height: 1px;
background: #333;
margin: 2em 0;
}
/* Style for the image */
img {
display: block;
margin: 20px auto;
max-width: 150px;
border-radius: 8px;
border: 2px solid #00ff41;
}
</style>
</head>
<body>
<div class="container">
<h1>macOS Command-Line Guide</h1>
<p>A quick reference for the commands covered in your EEE120 videos. Let's master the terminal! 💻</p>
<hr>
<h2>Navigation & Information</h2>
<p>Commands for moving around and getting information about your system.</p>
<table>
<tr>
<th>Command</th>
<th>Description</th>
</tr>
<tr>
<td><code>pwd</code></td>
<td><strong>P</strong>rint <strong>W</strong>orking <strong>D</strong>irectory (shows your current location).</td>
</tr>
<tr>
<td><code>ls</code></td>
<td><strong>L</strong>i<strong>s</strong>t files and directories in the current location.</td>
</tr>
<tr>
<td><code>cd [directory]</code></td>
<td><strong>C</strong>hange <strong>D</strong>irectory. <code>cd ..</code> goes up one level. <code>cd</code> alone goes to your home directory.</td>
</tr>
<tr>
<td><code>whoami</code></td>
<td>Shows the username of the current account.</td>
</tr>
<tr>
<td><code>history</code></td>
<td>Shows previously used commands. The <strong>Up Arrow Key</strong> scrolls through them one by one.</td>
</tr>
<tr>
<td><code>man [command]</code></td>
<td>Shows the <strong>man</strong>ual page for a command. Your go-to for help!</td>
</tr>
<tr>
<td><code>which [command]</code></td>
<td>Shows the full path/location of a command's executable file (e.g., <code>which ls</code>).</td>
</tr>
<tr>
<td><code>clear</code></td>
<td>Clears the terminal screen for a clean workspace.</td>
</tr>
<tr>
<td><code>ping [ip_address]</code></td>
<td>Sends a packet to a network address to check for a connection.</td>
</tr>
</table>
<hr>
<h2>File & Directory Management</h2>
<p>Commands for creating, deleting, and organizing your files.</p>
<table>
<tr>
<th>Command</th>
<th>Description</th>
</tr>
<tr>
<td><code>mkdir [name]</code></td>
<td><strong>M</strong>a<strong>k</strong>e a new <strong>dir</strong>ectory.</td>
</tr>
<tr>
<td><code>rmdir [name]</code></td>
<td><strong>R</strong>e<strong>m</strong>ove an empty <strong>dir</strong>ectory.</td>
</tr>
<tr>
<td><code>touch [filename]</code></td>
<td>Creates a new, empty file.</td>
</tr>
<tr>
<td><code>cp [source] [destination]</code></td>
<td><strong>C</strong>o<strong>p</strong>y a file or directory.</td>
</tr>
<tr>
<td><code>mv [source] [destination]</code></td>
<td><strong>M</strong>o<strong>v</strong>e a file or directory. Also used to rename files (e.g., <code>mv oldname.txt newname.txt</code>).</td>
</tr>
<tr>
<td><code>rm [filename]</code></td>
<td><strong>R</strong>e<strong>m</strong>ove (delete) a file. Be careful, there's no undo!</td>
</tr>
<tr>
<td><code>ln [target] [link_name]</code></td>
<td>Creates a hard <strong>l</strong>i<strong>n</strong>k between two files, making them act as one.</td>
</tr>
</table>
<hr>
<h2>Common Command Flags (Options)</h2>
<p>Flags modify the behavior of commands. They usually start with a dash <code>-</code> and can often be combined (e.g., <code>ls -al</code>).</p>
<table>
<tr>
<th>Command with Flag</th>
<th>Description</th>
</tr>
<tr>
<td><code>ls -l</code></td>
<td>Lists files in <strong>l</strong>ong format, showing permissions, owner, size, and modification date.</td>
</tr>
<tr>
<td><code>ls -a</code></td>
<td>Lists <strong>a</strong>ll files, including hidden ones (those starting with a dot <code>.</code>).</td>
</tr>
<tr>
<td><code>cp -r</code></td>
<td>Copies <strong>r</strong>ecursively, meaning it copies a directory and everything inside it.</td>
</tr>
<tr>
<td><code>rm -r</code></td>
<td>Removes a directory and its contents <strong>r</strong>ecursively. Use with extreme caution!</td>
</tr>
</table>
<hr>
<h2>Viewing & Editing Files</h2>
<p>Commands to see what's inside your files or to edit them.</p>
<table>
<tr>
<th>Command</th>
<th>Description</th>
</tr>
<tr>
<td><code>more [filename]</code></td>
<td>Displays file content one screen at a time. Press space to advance.</td>
</tr>
<tr>
<td><code>less [filename]</code></td>
<td>Similar to <code>more</code>, but allows you to scroll both up and down.</td>
</tr>
<tr>
<td><code>head [filename]</code></td>
<td>Shows the first 10 lines of a file.</td>
</tr>
<tr>
<td><code>cat [filename]</code></td>
<td>Con<strong>cat</strong>enates and displays the entire content of a file at once. Can also combine files.</td>
</tr>
<tr>
<td><code>nano [filename]</code></td>
<td>A simple, beginner-friendly command-line text editor.</td>
</tr>
</table>
<hr>
<h2>Power Tools: Search, Sort, & Redirection</h2>
<p>Combine these to create powerful command sequences.</p>
<table>
<tr>
<th>Tool</th>
<th>Description</th>
</tr>
<tr>
<td><code>find . -iname [filename]</code></td>
<td>Searches for files by name within the current directory (<code>.</code>).</td>
</tr>
<tr>
<td><code>grep "[pattern]" [file]</code></td>
<td>Searches for a specific text pattern within a file.</td>
</tr>
<tr>
<td><code>sort [file]</code></td>
<td>Sorts the lines of a text file alphabetically.</td>
</tr>
<tr>
<td><code>|</code> (Pipe)</td>
<td>Takes the output of one command and "pipes" it as the input to another command. Example: <code>sort file.txt | grep "a"</code></td>
</tr>
<tr>
<td><code>></code> (Output Redirection)</td>
<td>Redirects the output of a command into a file, overwriting the file. Example: <code>ls > files.txt</code></td>
</tr>
<tr>
<td><code><</code> (Input Redirection)</td>
<td>Uses the content of a file as the input for a command. Example: <code>./program < input.txt</code></td>
</tr>
</table>
<hr>
<h2>Permissions</h2>
<p>Control who can read, write, or execute your files.</p>
<table>
<tr>
<th>Command</th>
<th>Description</th>
</tr>
<tr>
<td><code>ls -l</code></td>
<td>The first part of the output (e.g., <code>-rwxr-xr--</code>) shows permissions for <strong>Owner</strong>, <strong>Group</strong>, and <strong>Other</strong> users. (r=read, w=write, x=execute).</td>
</tr>
<tr>
<td><code>chmod [###] [file]</code></td>
<td><strong>Ch</strong>ange <strong>mod</strong>e (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--).</td>
</tr>
<tr>
<td><code>chown [user] [file]</code></td>
<td><strong>Ch</strong>ange the <strong>own</strong>er of a file.</td>
</tr>
<tr>
<td><code>chgrp [group] [file]</code></td>
<td><strong>Ch</strong>ange the <strong>gr</strong>ou<strong>p</strong> of a file.</td>
</tr>
<tr>
<td><code>sudo [command]</code></td>
<td><strong>S</strong>uper <strong>u</strong>ser <strong>do</strong>. Runs a command with administrative privileges. Requires a password.</td>
</tr>
</table>
<hr>
<h2>Bash Scripting & Aliases</h2>
<p>Automate tasks and create shortcuts.</p>
<h3>Example Script (`firstScript.sh`)</h3>
<pre><code>#!/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
</code></pre>
<h3>Running a Script</h3>
<p>1. Make it executable: <code>chmod 755 yourScript.sh</code><br>
2. Run it from the current directory: <code>./yourScript.sh</code></p>
<h3>Aliases (Shortcuts)</h3>
<table>
<tr>
<th>Command</th>
<th>Description</th>
</tr>
<tr>
<td><code>alias fs='./firstScript.sh'</code></td>
<td>Creates a temporary shortcut. Now, typing <code>fs</code> will run <code>./firstScript.sh</code>.</td>
</tr>
<tr>
<td><code>unalias fs</code></td>
<td>Removes the alias.</td>
</tr>
</table>
</div>
</body>
</html>