CLI Foundations

Use a terminal safely to identify your location, move between folders, and inspect files before running commands.

Objectives

  • Identify the folder where a command will run
  • Move safely between folders and inspect their contents
  • Use Unix-style commands for a consistent workflow across Windows, macOS, and Linux
    • Windows users run them in Git Bash
  • Open a file and safely save or exit with nano or vi
  • Create, copy, move, and remove a known file
  • Recognize when to pause before a file-changing command

The Terminal Has A Current Location

  • Every command runs in the current working directory
  • A relative path starts from that directory
  • .. means the parent directory
  • Confirm the location before creating, moving, or deleting files
  • The terminal does not know which folder you intended to use

Files, Directories, And Paths

  • A file stores content, such as text, code, or an image
  • A directory contains files and other directories
  • Folder and directory are used interchangeably
  • A path tells the terminal where a file or directory is located
  • Relative paths are usually shorter and easier to reuse in commands
  • See the slides below for path terminology and platform examples

Absolute And Relative Paths

  • An absolute path starts from the drive or filesystem root and identifies the full location
  • A relative path starts from the terminal's current working directory
  • The same relative command can work for different students when each student starts in their own repository
absolute-and-relative-pa-01.txt
Loading code...

Absolute Paths By Platform

PlatformExample
WindowsC:\Users\mt85\project\notes.txt
macOS/Users/mt85/project/notes.txt
Linux/home/mt85/project/notes.txt
  • Windows paths commonly begin with a drive letter
  • macOS and Linux paths begin with /
  • Absolute paths often differ between computers and user accounts

Prefer Relative Paths

prefer-relative-paths-02.sh
Loading code...
  • project starts inside the current directory
  • notes starts inside project after cd project
  • . means the current directory
  • .. means the parent directory
  • Wrap a path in quotes when a name contains spaces: cd "project files"

Orient Yourself First

orient-yourself-first-03.sh
Loading code...
  • A new terminal usually opens in your home directory, also represented by ~
  • pwd prints the current directory
  • ls lists the visible files and folders in that directory
  • cd folder-name enters a child folder
  • cd .. moves up one folder

Inspect A Directory With ls

  • Run ls before changing files so you can confirm names and capitalization
  • Pass a path to inspect another directory without entering it: ls project-folder
  • Command options change the information that ls displays
  • Options may be combined after you understand what each one does
  • See the slide below for common ls options

Common ls Options

CommandResult
lsVisible names
ls -lLong format with permissions, owner, size, and date
ls -aAll entries, including hidden names that begin with .
ls -laLong format plus hidden entries
  • .git is a hidden directory, so it appears with ls -a or ls -la
  • Start with plain ls; add an option when the extra information is useful

Terminal Text Editors

  • nano is a straightforward editor with keyboard shortcuts shown on screen
  • vi and vim are mode-based editors commonly available on Linux systems
  • Use the editor required by an example or the one you can operate safely
  • See the slides below for the minimum commands needed to open, save, and exit

Edit A File With nano

edit-a-file-with-nano-04.sh
Loading code...
  • Type normally to edit the file
  • Ctrl+O, then Enter, saves the file
  • Ctrl+X exits the editor
  • The ^ shown beside a shortcut means the Ctrl key

Recognize vi Or vim

recognize-vi-or-vim-05.sh
Loading code...
  • Press lowercase 'i' to enter insert mode and type text
  • Press Esc to leave insert mode
  • Type :wq, then press Enter, to save and exit
  • Type :q!, then press Enter, to exit without saving
  • If typing does not change the file, check which mode is active

Common File Operations

  • mkdir creates a directory
  • touch creates an empty file when the file does not already exist
  • cp copies a source to a destination
  • mv moves or renames an item
  • rm permanently removes an item
  • See the slides below for each command's structure, result, and main safety check

Create A Directory With mkdir

create-a-directory-with--06.sh
Loading code...
  • practice is created inside the current directory
  • The command fails when a directory with that name already exists
  • Run pwd first when you are unsure where the new directory will be created

Create An Empty File With touch

create-an-empty-file-wit-07.sh
Loading code...
  • The parent directory must already exist
  • Creates an empty file when a file with that name does not already exist
  • Updates an existing file's modified timestamp without changing its contents

Copy A File With cp

copy-a-file-with-cp-08.sh
Loading code...
  • The first path is the source
  • The second path is the destination
  • The original file remains after a successful copy
  • Confirm the destination name before replacing an existing file

Move Or Rename With mv

move-or-rename-with-mv-09.sh
Loading code...
  • The first path is the existing item
  • The second path is its new location or name
  • The item no longer appears under its old path after a successful move
  • Moving a file onto an existing destination may replace that destination

Remove A File With rm

remove-a-file-with-rm-10.sh
Loading code...
  • rm permanently deletes the named file
  • The file does not move to the Recycle Bin, Trash, or another recovery folder
  • Read the complete path before pressing Enter
  • * is a wildcard that can match multiple names, such as every non-hidden .txt file in a directory
  • -r recursively removes a directory and everything inside it
  • -f forces removal without prompts and ignores missing-file errors
Warning:

Never run rm -rf /. It directs rm to forcefully remove the filesystem root and its contents.

Elevate One Command With sudo

  • sudo is commonly read as "superuser do"; more precisely, it runs a command as another user, root by default
  • Use it only when a setup lesson explicitly requires it
  • Do not add sudo just to make a permission error disappear
  • Linux and macOS commonly use it for system setup tasks
  • Windows users should not need sudo in Git Bash for these lessons; open Git Bash as Administrator only when instructed

Use A Safe Command Habit

  1. Run pwd
  2. Run ls when the folder contents matter
  3. Read the command before pressing Enter
  4. Check the result before running the next command

This habit catches most wrong-folder mistakes before Git or other tools add more moving parts

When Something Looks Wrong

ErrorSolution
No such file or directoryRun pwd and ls, then check spelling and capitalization
Permission deniedConfirm the location and expected ownership; do not add sudo just to dismiss the error
command not foundCheck the command's spelling and that the required program is installed
Unexpected filesStop and run pwd before trying another cd command
Unexpected file operationInspect the directory and use an exact name instead of *

Knowledge Check 1/2

Questions

  1. What does pwd print?
  2. What does ls show?
  3. What is a relative path based on?
  4. What does an absolute path begin from?

Answers

  1. The current directory
  2. The entries in the current directory
  3. The current directory
  4. The drive root on Windows or the filesystem root on Linux and macOS

Use Next to reveal each answer

Knowledge Check 2/2

Questions

  1. How should you write a path that contains spaces?
  2. Which ls option shows hidden entries such as .git?
  3. After cp source.txt backup.txt succeeds, which files remain?
  4. Why is * dangerous with rm?

Answers

  1. Put the complete path in quotes, such as cd "My Project"
  2. ls -a
  3. Both source.txt and the copied backup.txt
  4. It can match and permanently delete unintended files

Use Next to reveal each answer

Key Terms And Further Learning

  • Current directory: The location relative commands start from; inspect it with pwd.
  • Relative path: A file or directory location measured from the current directory.
  • Absolute path: A location that begins at a drive or filesystem root.

Further Learning:

Summary

  • Start with pwd and inspect directory contents with ls
  • Prefer clear relative paths while recognizing platform-specific absolute paths
  • Use nano or the basic vi workflow when editing from a terminal
  • Name exact source and destination paths for file operations
  • Treat rm as permanent and stop when the current location is unexpected

Next presentation: GitHub Setup And SSH