EEE120 Terminal Cheat Sheet 🖥️

Your guide to Bash (macOS/Linux) and PowerShell (Windows) commands.

Navigation & Info

pwd Print Working Directory (shows where you are).
whoami Shows your account username.
cd [dir] Change Directory. Use cd .. to go up, cd to go home.
cd - Go to the previous directory you were in.
ls List files and directories.
ls -l List with long format (permissions, owner, size).
ls -a List all files (including hidden . files).
ls -alF Combine flags: all, long, F (adds / to dirs).
history Shows command history. history -75 shows last 75.
Up Arrow Scroll through last-used commands.
Tab Auto-completes filenames or commands.
clear Clears the terminal screen.
man [cmd] Shows the manual for a command (e.g., man ls).
which [cmd] Shows the full path to a command (e.g., which ls).

File & Directory Manipulation

mkdir [name] Make a new directory.
touch [file] Creates a new, empty file (or updates timestamp).
cp [src] [dest] Copy a file. (e.g., cp hi.txt bye.txt).
cp -r [src] [dest] Copy recursively (copies a whole directory).
mv [src] [dest] Move a file or directory. Also used to rename files.
rm [file] Remove a file. Be careful! This is permanent.
rmdir [dir] Remove an empty directory.
rm -r [dir] Remove a directory recursively (and all its contents).
ln [target] [link] Creates a hard link between two files.
ln -s [target] [link] Creates a symbolic link (a "shortcut"). More common.

Viewing & Reading Files

echo "text" Prints "text" to the terminal.
cat [file] Concatenate. Dumps the entire file content to the screen.
cat f1.txt f2.txt Combines and prints two files.
more [file] View a file one page at a time (scroll down only).
less [file] View a file one page at a time (scroll up and down). (more is less!)
head [file] Shows the first 10 lines of a file.
tail [file] Shows the last 10 lines of a file.
nano [file] Opens the nano text editor to create or edit a file.

Search, Sort & Redirects

find . -name "f.txt" Finds files with -name starting from . (current dir).
grep "term" [file] Searches for "term" inside a file.
grep -r "term" . Searches recursively for "term" in all files in current dir.
sort [file] Sorts the lines of a file alphabetically.
| (Pipe) Pipeline. Uses the output of one command as the input for another.
sort f.txt | grep "e" Example: sorts the file, then pipes the sorted list to grep.
> (Redirect) Redirects standard output to a file (overwrites).
ls > files.txt Example: Puts the output of ls into files.txt.
>> (Append) Redirects output to a file (appends to end).
< (Input) Redirects standard input from a file.
./app < input.txt Example: Runs app and feeds it input.txt as if you typed it.

Permissions & Ownership

ls -l shows permissions like -rwxr-xr--

  • First char: d (directory) or - (file).
  • Group 1: Owner (rwx = Read, Write, eXecute)
  • Group 2: Group (r-x = Read, no Write, eXecute)
  • Group 3: Other (r-- = Read, no Write, no eXecute)
chmod [###] [file] Change mode (permissions) using numbers.
4=Read, 2=Write, 1=X Permission numbers. Add them up for each group.
chmod 755 script.sh Owner: 7 (4+2+1), Group: 5 (4+0+1), Other: 5 (4+0+1).
chmod 660 file.txt Owner: 6 (4+2+0), Group: 6 (4+2+0), Other: 0.
sudo [command] Super User Do. Runs a command as administrator.
sudo chown [user] [file] Change owner of a file.
sudo chgrp [group] [file] Change group of a file.

Processes & Networking

ping [ip/host] Sends packets to a host (e.g., ping 8.8.8.8). (Ctrl+C to stop).
traceroute [host] Traces the network hops to a host.
ifconfig Shows network interface configuration (your IP address).
ps Shows your active processes.
ps aux Shows all processes from all users in detail.
top Shows a dynamic, real-time list of processes. (q to quit).
[cmd] & Runs a command in the background.
Ctrl+Z Suspends the current foreground job.
jobs Lists suspended or background jobs.
bg %1 Puts job %1 into the background.
fg %1 Brings job %1 to the foreground.
kill [PID] Kills a process by its Process ID (get from ps).
kill -9 [PID] Force-kills a stubborn process.

Scripting, Archive & Transfer

alias fs=./script.sh Creates a shortcut fs to run your script.
unalias fs Removes the fs alias.
tar -zcvf a.tar.gz [dir] Tape Archive: zip, create, verbose, file. Compresses [dir].
tar -xzvf a.tar.gz Extracts (unzips) the .tar.gz file.
ssh [user]@[host] Secure Shell. Logs into a remote server.
scp [file] [user]@[host]:~ Secure Copy. Sends [file] to the remote host's home (~) dir.
logout Logs out of an ssh session.

Disk & Package Mgmt (Added)

du -h Disk Usage for current dir, in human-readable format.
df -h Disk Free space for all volumes, in human-readable format.
brew install [name] (Homebrew) Installs software. (e.g., brew install htop).
brew update (Homebrew) Updates the list of available software.
brew upgrade (Homebrew) Upgrades all installed software.

Example: Bash Script

A file like firstScript.sh. Run with ./firstScript.sh after chmod 755.

#!/bin/bash
# ^ This is the "shebang", tells the OS to use bash

i=1
echo "Enter a number: "
read a # Read user input into variable 'a'

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

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 )) # Increment 'i'
done

ls # Runs 'ls' at the end of the script