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
nanoorvi - 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
| Platform | Example |
|---|---|
| Windows | C:\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...
projectstarts inside the current directorynotesstarts insideprojectaftercd 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
~ pwdprints the current directorylslists the visible files and folders in that directorycd folder-nameenters a child foldercd ..moves up one folder
Inspect A Directory With ls
- Run
lsbefore 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
lsdisplays - Options may be combined after you understand what each one does
- See the slide below for common
lsoptions
Common ls Options
| Command | Result |
|---|---|
ls | Visible names |
ls -l | Long format with permissions, owner, size, and date |
ls -a | All entries, including hidden names that begin with . |
ls -la | Long format plus hidden entries |
.gitis a hidden directory, so it appears withls -aorls -la- Start with plain
ls; add an option when the extra information is useful
Terminal Text Editors
nanois a straightforward editor with keyboard shortcuts shown on screenviandvimare 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, thenEnter, saves the fileCtrl+Xexits the editor- The
^shown beside a shortcut means theCtrlkey
Recognize vi Or vim
recognize-vi-or-vim-05.sh
Loading code...
- Press lowercase 'i' to enter insert mode and type text
- Press
Escto leave insert mode - Type
:wq, then pressEnter, to save and exit - Type
:q!, then pressEnter, to exit without saving - If typing does not change the file, check which mode is active
Common File Operations
mkdircreates a directorytouchcreates an empty file when the file does not already existcpcopies a source to a destinationmvmoves or renames an itemrmpermanently 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...
practiceis created inside the current directory- The command fails when a directory with that name already exists
- Run
pwdfirst 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...
rmpermanently 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.txtfile in a directory-rrecursively removes a directory and everything inside it-fforces 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
sudois 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
sudojust to make a permission error disappear - Linux and macOS commonly use it for system setup tasks
- Windows users should not need
sudoin Git Bash for these lessons; open Git Bash as Administrator only when instructed
Use A Safe Command Habit
- Run
pwd - Run
lswhen the folder contents matter - Read the command before pressing
Enter - 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
| Error | Solution |
|---|---|
No such file or directory | Run pwd and ls, then check spelling and capitalization |
Permission denied | Confirm the location and expected ownership; do not add sudo just to dismiss the error |
command not found | Check the command's spelling and that the required program is installed |
| Unexpected files | Stop and run pwd before trying another cd command |
| Unexpected file operation | Inspect the directory and use an exact name instead of * |
Knowledge Check 1/2
Questions
- What does
pwdprint? - What does
lsshow? - What is a relative path based on?
- What does an absolute path begin from?
Answers
- The current directory
- The entries in the current directory
- The current directory
- The drive root on Windows or the filesystem root on Linux and macOS
Use Next to reveal each answer
Knowledge Check 2/2
Questions
- How should you write a path that contains spaces?
- Which
lsoption shows hidden entries such as.git? - After
cp source.txt backup.txtsucceeds, which files remain? - Why is
*dangerous withrm?
Answers
- Put the complete path in quotes, such as
cd "My Project" ls -a- Both
source.txtand the copiedbackup.txt - 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
pwdand inspect directory contents withls - Prefer clear relative paths while recognizing platform-specific absolute paths
- Use
nanoor the basicviworkflow when editing from a terminal - Name exact source and destination paths for file operations
- Treat
rmas permanent and stop when the current location is unexpected