Learn to see where you are in the filesystem using pwd (print working directory).
pwd
pwd shows your current location in the filesystem. When you open a terminal, you typically start in your home directory (/home/username on Linux or /Users/username on macOS).
Pro Tips
pwd stands for ‘print working directory’
Think of it as ‘where am I right now?’
Use pwd whenever you’re unsure of your location
Common Mistakes to Avoid
Forgetting where you are before running commands
Assuming you’re in a specific directory without checking
List files in your current directory
ls
ls-l
ls-la
What This Does
ls lists directory contents. -l shows detailed info (permissions, size, date). -a shows hidden files (those starting with .). -la combines both flags.
Expected Outcome
First command shows visible files/folders. -l shows detailed listing with permissions and sizes. -la includes hidden files like .bashrc and .profile
*Pro Tips
ls = basic list
ls -l = long format (detailed)
ls -a = all files (including hidden)
ls -lh = human-readable sizes (KB, MB instead of bytes)
Hidden files start with a dot (.)
*Common Mistakes to Avoid
Not using -a and missing hidden configuration files
Forgetting that . is current directory and .. is parent directory
Navigate to a different directory
cd /tmp
pwd
cd ~
pwd
cd -
pwd
What This Does
cd changes your current directory. /tmp is an absolute path. ~ is shorthand for home directory. - returns to previous directory. Always pwd after cd to confirm location.
First cd takes you to /tmp. cd ~ takes you home. cd - returns to /tmp. Each pwd confirms the change.
Pro Tips
cd with no arguments goes to home directory
cd ~ explicitly goes to home
cd - toggles between last two directories (super useful!)
cd .. goes up one level to parent directory
Common Mistakes to Avoid
Typing ‘CD’ or ‘Cd’ - Unix is case-sensitive, use lowercase
Forgetting the space between cd and the path
Not understanding the difference between / (root) and ~ (home)
Create a practice directory
cd ~
mkdir devops-practice
ls-lcd devops-practice
pwd
What This Does
mkdir creates a new directory. We create it in home (~), verify with ls, then enter it with cd. pwd confirms we’re inside.
New devops-practice directory appears in ls output. pwd shows /home/username/devops-practice or similar.
Pro Tips
mkdir = ‘make directory’
Use meaningful names with hyphens or underscores (no spaces!)
Check with ls before and after to see the change
Common Mistakes to Avoid
Using spaces in directory names (use-hyphens-instead)
Trying to create a directory that already exists (error message)
Creating directories in system locations without sudo
Create nested directories
mkdir-p projects/web/frontend
ls
ls projects
ls projects/web
What This Does
mkdir -p creates parent directories as needed. Without -p, mkdir fails if parent doesn’t exist. This creates projects, then web inside it, then frontend inside web.
projects directory created. Inside it: web directory. Inside web: frontend directory. All created with one command.
Pro Tips
-p flag means ‘create parents as needed’
Without -p, you’d need three separate mkdir commands
Use -p when creating nested structures
-p doesn’t error if directory already exists (safe to re-run)
Common Mistakes to Avoid
Forgetting -p and getting ‘No such file or directory’ errors
Not understanding the directory tree structure created
Navigate using relative paths
pwd
cd projects/web
pwd
cd ..
pwd
cd ../..
pwd
What This Does
Relative paths start from current location. projects/web goes down two levels. .. goes up one level. ../.. goes up two levels. No leading / means relative.
Start in devops-practice, move to web, back to projects, back to devops-practice. pwd after each confirms.
Pro Tips
. = current directory
.. = parent directory
../.. = grandparent (two levels up)
../../other = go up two, then into ‘other’
Paths without leading / are relative
Common Mistakes to Avoid
Confusing absolute (/home/user/dir) with relative (dir) paths
Forgetting pwd and getting lost in deep directory structures
List contents recursively
pwd
ls-R
tree
What This Does
ls -R lists recursively (shows all subdirectories). tree shows a visual tree structure (may need installation). Both reveal the full hierarchy.
ls -R shows all nested directories and their contents. tree (if installed) shows beautiful tree diagram.
Pro Tips
ls -R shows everything but can be overwhelming
tree is not built-in but super useful (install with: brew install tree or apt install tree)
Use tree when you want to visualize structure
Limit depth with: tree -L 2 (only 2 levels deep)
Common Mistakes to Avoid
Running ls -R in home directory (shows everything, takes forever!)
Expecting tree to be available without installing it first
Remove directories
cd ~/devops-practice
rmdir projects/web/frontend
rmdir projects/web
rmdir projects
ls
What This Does
rmdir removes empty directories only. Must delete from deepest level upward. rm -r removes directories and contents (dangerous!). Always use rmdir when possible for safety.
Directories removed in order: frontend first, then web, then projects. ls shows they’re gone.
Pro Tips
rmdir only works on EMPTY directories (safe)
rm -r removes directory AND all contents (dangerous, no undo!)
rm -rf is extremely dangerous (force removal, no confirmation)
Always double-check with pwd before rm -r
Practice with test directories before using on real files
Tab completion saves time
cd ~
mkdir-p long-directory-name/with-many-levels/to-practice-tab
cd lon<TAB>
cd wit<TAB>
cd to-<TAB>
pwd
What This Does
Press Tab after typing first few letters. Shell completes the rest. Double-Tab shows all matches if ambiguous. This prevents typos and saves time.
Tab autocompletes each directory name. You navigate to full path by typing ~20 characters instead of 60+. Much faster!
Pro Tips
Tab is your best friend in the shell
Double-Tab shows all possible completions
Works for commands, files, directories, and options
Prevents typos in long paths
If Tab does nothing, no match exists (typo or wrong location)
Common Mistakes to Avoid
Not using Tab and typing everything manually (slow + error-prone)
Giving up when Tab doesn’t complete (try double-Tab to see options)
Clean up practice directory
cd ~
rm-r devops-practice
ls
What This Does
rm -r removes directory and all contents. Safe here since it’s practice files. Always pwd before rm -r to confirm location!
devops-practice directory completely removed. ls confirms it’s gone.
Pro Tips
Always pwd before rm -r (safety check)
Add -i flag for confirmation: rm -ri (prompts before each deletion)
There is NO undo for rm (files gone forever)
Consider using trash/trash-cli instead of rm for safety
Common Mistakes to Avoid
Forgetting you can’t undo rm (lost files forever)
Not checking pwd before rm -r (deleting wrong directory!)
Create a practice workspace
cd ~
mkdir-p shell-practice/backups
cd shell-practice
pwd
What This Does
Create a practice directory with a backups subdirectory. We’ll use this safe space to practice file manipulation without risking real files.New shell-practice directory with backups folder inside. pwd confirms you’re in ~/shell-practice
Pro Tips
Always practice destructive commands in a test directory first
Use meaningful directory names for organization
Common Mistakes to Avoid
Practicing file operations in important directories (risk of data loss)
touch creates empty files if they don’t exist. Can create multiple files in one command (space-separated). If file exists, touch updates its modification time without changing content.Four empty files created: readme.txt, app.js, config.json, data.csv. ls -l shows size 0 for all.
Pro Tips
touch is fastest way to create empty files
Can create multiple files: touch file1 file2 file3
If file exists, only updates timestamp (won’t overwrite)
Use .txt, .md, .json extensions to indicate file type
Common Mistakes to Avoid
Expecting touch to add content (it only creates empty files)
Not using file extensions (makes files harder to identify later)
cp copies files. Syntax: cp sourcedestination. Creates exact duplicate with new name. Original file remains unchanged. Useful for backups before editing.Two new files created: readme-backup.txt (copy of readme.txt) and app-v1.js (copy of app.js). ls shows all 6 files.
Pro Tips
Syntax: cp
Make backups before editing: cp file.txt file.txt.backup
cp doesn’t delete the original (unlike mv)
Add -i flag for confirmation: cp -i (asks before overwriting)
Common Mistakes to Avoid
Reversing source and destination (cp new old instead of cp old new)
Overwriting important files without -i flag
Copy files to different directory
cp readme.txt backups/
cp config.json data.csv backups/
ls backups/
What This Does
cp can copy to different directories. When destination is a directory, file keeps same name. Can copy multiple files to one directory in single command.backups/ now contains: readme.txt, config.json, data.csv. Original files still in shell-practice/.
Pro Tips
Trailing / on directory not required but makes intent clear
cp -r copies directories recursively (directory + all contents). -r flag is required for directories. Creates complete duplicate of directory tree.project-backup directory created with exact copy of index.html and style.css. ls -R shows both directories with identical contents.
Pro Tips
MUST use -r flag to copy directories
-r means ‘recursive’ (includes all subdirectories)
Copies entire directory tree, not just top level
Use -a instead of -r to preserve timestamps and permissions
Common Mistakes to Avoid
Forgetting -r flag when copying directories (error: omitting directory)
Not understanding cp without -r only works for files
Move and rename files with mv
mv readme-backup.txt README.md
ls-lmv README.md project/
ls project/
What This Does
mv moves OR renames files. Same syntax as cp but original disappears. When destination is different directory, file moves. When same directory, file is renamed.readme-backup.txt renamed to README.md, then moved into project/. No longer in shell-practice/.
Pro Tips
mv both moves AND renames (depending on arguments)
Same directory = rename: mv old.txt new.txt
Different directory = move: mv file.txt other-dir/
mv is instant (doesn’t copy then delete)
Use -i flag to prevent accidental overwrites: mv -i
Common Mistakes to Avoid
Expecting mv to create a copy (it moves, not copies)
Accidentally overwriting existing files without -i flag
Not realizing mv can rename (thinking it only moves)
Move multiple files
ls
mv app.js app-v1.js config.json backups/
ls
ls backups/
What This Does
mv can move multiple files to one directory in single command. Last argument is destination (must be existing directory). All other arguments are source files.app.js, app-v1.js, and config.json moved to backups/. No longer in shell-practice/. backups/ now has 6 files total.
Pro Tips
Last argument is always the destination
Destination must be a directory when moving multiple files
Use -v flag to see what’s being moved: mv -v file1 file2 dest/
Faster than multiple mv commands
Common Mistakes to Avoid
Last argument not being a directory (error when moving multiple files)
Moving important files without checking destination first
Delete files with rm
ls
rm data.csv
ls
rm backups/readme.txt backups/config.json
ls backups/
What This Does
rm permanently deletes files. NO TRASH/RECYCLE BIN. No undo. Can delete multiple files in one command. Use with extreme caution.data.csv deleted from shell-practice/. readme.txt and config.json deleted from backups/. Files gone forever.
Pro Tips
rm is PERMANENT - no undo, no recovery
Use -i flag for confirmation: rm -i file (asks before deleting)
Can delete multiple files: rm file1 file2 file3
Check pwd before rm to ensure you’re in correct directory
Consider using trash-cli instead of rm for safety
Common Mistakes to Avoid
Deleting files without -i flag (no confirmation)
Using rm * without understanding what * matches (deletes everything!)
Expecting to recover deleted files (they’re gone forever)
Not checking pwd before rm (deleting from wrong directory)
Understand file wildcards
touch test1.txt test2.txt test3.log demo.txt
ls*.txt
ls test*rm test*.txt
ls
What This Does
* matches any characters. *.txt matches all .txt files. test* matches anything starting with ‘test’. Wildcards expand before command runs. Powerful but dangerous with rm.ls *.txt shows all .txt files. ls test* shows test1, test2, test3. rm test*.txt deletes test1.txt and test2.txt. demo.txt and test3.log remain.
Pro Tips
* matches zero or more characters
? matches exactly one character
*.txt matches all text files
test* matches test1, test2, testing, etc.
ALWAYS test with ls before using with rm!
Double-check: ls *.txt then rm *.txt
Common Mistakes to Avoid
Using rm * without understanding what it matches (deletes everything!)
Not testing wildcard with ls before rm (accidental deletion)
Spaces around *: rm * .txt deletes ALL files plus errors on .txt
What This Does
-i flag adds interactive mode (asks confirmation). Use with cp, mv, rm to prevent accidental overwrites/deletions. Respond y (yes) or n (no) to each prompt.cp -i prompts if app-v1.js exists. rm -i asks confirmation before deleting each file. Gives you chance to say no.
Pro Tips
Always use -i flag when learning: rm -i, cp -i, mv -i
Create shell aliases: alias rm=’rm -i’
Test wildcards with ls before rm
Keep backups of important files
pwd before destructive operations
There is NO undo in terminal
Common Mistakes to Avoid
Getting comfortable and skipping -i flag
Pressing ‘y’ without reading the prompt
Assuming deleted files can be recovered (they can’t)
Clean up practice workspace
cd ~
rm-r shell-practice
ls
What This Does
rm -r removes directory and all contents recursively. Use for directories. Always pwd first to confirm location.shell-practice directory and everything inside deleted. Clean slate.
Pro Tips
rm -r deletes directories + contents
rm -rf forces deletion without confirmation (VERY dangerous)
Always check pwd before rm -r
Consider keeping practice directories for future reference
Common Mistakes to Avoid
Using rm -rf habitually (forces deletion, no safety net)
What This Does
echo writes text to files. > creates new file. -e enables escape sequences like \n (newline). We’re creating test files with known content to practice viewing and searching.Three files created: hello.txt (1 line), log.txt (5 lines with ERROR), fruits.txt (5 lines of fruit names).
Pro Tips
echo writes to stdout (screen) by default
> redirects output to file (creates or overwrites)
>> appends to file instead of overwriting
-e flag interprets \n as newline
Common Mistakes to Avoid
Using > instead of » and accidentally overwriting files
Forgetting quotes around text with spaces
View entire file with cat
cat hello.txt
cat log.txt
cat fruits.txt
What This Does
cat (concatenate) prints entire file content. Good for small files. Name comes from ability to concatenate multiple files. Outputs everything at once.Each file’s complete contents printed to screen. hello.txt shows one line, log.txt shows 5 lines, fruits.txt shows 5 fruit names.
Pro Tips
cat is best for small files (<100 lines)
cat file1 file2 concatenates multiple files
cat -n adds line numbers
For large files, use less instead (cat will flood your screen)
Common Mistakes to Avoid
Using cat on huge files (floods terminal, hard to read)
Not knowing when to use less instead
Page through files with less
less log.txt
What This Does
less opens file in pager (one screen at a time). Navigate with arrows/Page Up/Down. Press q to quit. Space for next page. / to search. Better than cat for large files.log.txt opens in less. You can scroll up/down. Press q to exit. Interactive viewing experience.
Pro Tips
Space = next page
b = previous page
/ then text = search forward
? then text = search backward
n = next search match
q = quit
less > more (more is older, less is better)
Common Mistakes to Avoid
Not knowing how to exit (press q)
Using cat instead of less for large files
View beginning of files with head
head log.txt
head-n 2 fruits.txt
head-3 fruits.txt
What This Does
head shows first 10 lines by default. -n sets number of lines. head -n 2 or head -2 shows first 2 lines. Useful for previewing files or checking headers.First command shows all 5 lines (file shorter than 10). Second shows first 2 fruits. Third shows first 3 fruits.
Pro Tips
head defaults to 10 lines
head -n 5 or head -5 (both work)
Useful for CSV headers: head -1 data.csv
Quick file preview without opening entire file
Common Mistakes to Avoid
Not specifying -n when you want specific line count
Expecting to see more than first 10 lines without -n flag
tail shows last 10 lines by default. -n sets number of lines. Opposite of head. Essential for viewing recent log entries.First command shows last lines of log (all 5 since file is short). Second shows last 2 fruits (date, elderberry). Third shows last 3 fruits.
Pro Tips
tail defaults to 10 lines
tail -n 20 or tail -20 (both work)
Essential for log files: tail -n 50 /var/log/syslog
Combine with grep: tail -100 log.txt
grep ERROR
Common Mistakes to Avoid
Confusing head and tail
Not using tail for large log files (using cat instead)