Lesson 3 • Beginner
File Management Commands 📁
By the end of this lesson you'll create files and folders, copy and move them, rename and delete them safely, read their contents, and act on dozens of files at once with wildcards — the everyday muscle-memory of working in a terminal.
What You'll Learn
- Create files and folders with touch and mkdir -p
- Copy files and whole directories with cp -r
- Move and rename in one step with mv
- Delete safely with rm -i and rmdir — and avoid the rm -rf trap
- Read file contents with cat and less
- Match many files at once with wildcards (*, ?, [])
pwd, cd, and ls. Every command below is a real shell command that changes files on disk, so run them in your own terminal (macOS, Linux, or WSL on Windows). The listed output shows what you should expect to see.touch drops a fresh blank sheet into a folder; mkdir labels a new folder (and mkdir -p builds a whole drawer of nested folders at once). cp runs a sheet through the photocopier; mv physically relocates the original (or just re-labels it). rm is the shredder — and unlike a real office, there is no bin to fish the pieces back out of. That one difference is why this lesson spends so long on deleting carefully.1️⃣ Creating Files and Directories
Every project starts as empty files and folders. touch makes an empty file (or, if it already exists, just updates its "last modified" time). mkdir makes a directory. The flag you'll use constantly is -p: it creates every missing parent folder in a path, so one command can build a deep structure. Read this worked example, run it, then check the output matches.
# touch — create an EMPTY file (or update its timestamp if it exists).
touch index.html # one file
touch style.css app.js # several at once, space-separated
# mkdir — make a directory (folder).
mkdir projects # one directory
# mkdir -p — make a whole nested path in one go.
# Without -p, "mkdir src/components/ui" fails if src/ doesn't exist yet.
mkdir -p src/components/ui # creates src, src/components AND src/components/ui
# See what you just made (-F adds a / after directory names).
ls -Fapp.js index.html projects/ src/ style.cssls -F prints afterwards.2️⃣ Reading File Contents
Before you copy or delete something, you usually want to look inside it. cat dumps a file's entire contents to the screen — perfect for short files. For long files (like logs) that would scroll off the top, use less: it opens a scrollable viewer you move through with the arrow keys, and you quit by pressing q.
# First, put some text in a file so there is something to read.
# > writes (overwrites!) the file. >> appends to the end.
echo "Roses are red" > poem.txt
echo "Violets are blue" >> poem.txt
echo "Bash is handy" >> poem.txt
# cat — print the WHOLE file to the screen at once.
cat poem.txt
# less — open a SCROLLABLE viewer for long files.
# (Arrow keys / Space to scroll, press q to quit.) Best for big logs.
less poem.txtRoses are red
Violets are blue
Bash is handycat poem.txt prints; less opens an interactive viewer you quit with q.🎯 Your Turn: scaffold a project folder
Fill in the three blanks marked ___ using the hints in the comments, then run it and check your output against the expected lines.
# 🎯 YOUR TURN — build a small project folder.
# Replace each ___ then run it in your terminal.
# 1) Make the nested folder src/pages in ONE command
mkdir ___ src/pages # 👉 the flag that creates parent folders: -p
# 2) Create an empty file called index.html
___ index.html # 👉 the command that makes an empty file: touch
# 3) Create two files at once: style.css and app.js
touch ___ ___ # 👉 just list both names, separated by a space
# Check your work
ls -F
# ✅ Expected output:
# app.js index.html src/ style.css___, run it in your terminal, and confirm ls -F shows the expected files.3️⃣ Copy, Move, Rename, and Delete
These four jobs are the core of file management. cp duplicates (original stays); add -r to copy a directory. mv does double duty — give it a new name in the same folder and it's a rename; give it a different folder and it's a move. rm deletes, and there is no undo. rmdir removes empty folders only — a built-in safety check.
# cp — COPY (original stays put).
cp file.txt backup.txt # copy a file to a new name
cp -r src/ src-backup/ # copy a DIRECTORY — -r ("recursive") is required!
# mv — MOVE or RENAME (the original is gone from its old spot).
mv old-name.txt new-name.txt # same folder + new name = a rename
mv report.pdf archive/ # different folder = a move
mv draft.txt archive/final.txt # move AND rename in one step
# rm — REMOVE. PERMANENT: there is NO recycle bin / trash.
rm file.txt # delete one file
rm -r old-folder/ # delete a directory AND everything inside it
rm -i notes.txt # -i = interactive: asks y/n before deleting
# rmdir — remove an EMPTY directory only (safer than rm -r).
rmdir empty-folder
# Confirm what remains
ls -Frm: remove 'notes.txt'? y
archive/ backup.txt new-name.txt src/ src-backup/rm -i pauses to ask y/n; the final line is what ls -F shows.DANGER: rm is permanent — treat rm -rf with extreme care
Unlike dragging to the trash, rm erases files with no recovery. The combination rm -rf is the most dangerous command in this lesson: -r means "recurse into directories" and -f means "force, never ask and never complain". Together they will silently wipe out an entire folder tree. Never run rm -rf /, rm -rf ~, or rm -rf * casually, and beware a stray space — rm -rf / tmp/junk tries to delete your whole system instead of /tmp/junk. The habit that keeps you safe: run the command as ls first to see exactly what matches, and reach for rm -i (interactive) when deleting anything you care about.
4️⃣ Wildcards: Acting on Many Files at Once
A wildcard (or "glob") is a pattern the shell expands into a list of matching filenames before the command even runs. * matches any run of characters, ? matches exactly one character, and [abc] matches one character from the set. This is how you copy or delete dozens of files in a single line — and exactly why previewing with ls first is so important.
# Wildcards (globs) let ONE command act on MANY files.
# * matches any run of characters (zero or more)
# ? matches exactly ONE character
# [...] matches one character from the set
# Set up a few files to match against.
touch report.txt notes.txt photo.jpg img1.png img2.png img9.png
# ALWAYS preview a glob with ls BEFORE using it to delete or move.
ls *.txt # everything ending in .txt
ls img?.png # img + ONE char + .png
ls img[12].png # img1.png or img2.png — NOT img9.png
# Now use the same patterns with real commands.
cp *.png backup/ # copy every PNG into backup/
rm img[12].png # delete only img1.png and img2.pngnotes.txt report.txt
img1.png img2.png img9.png
img1.png img2.pngls previews; the last two lines (cp and rm) act on the files those patterns matched.Pro Tip
Preview every glob with ls before you destroy anything. The shell expands *.tmp the same way for ls and rm, so ls *.tmp shows you the exact list rm *.tmp would delete. Get into the habit of running the ls version, reading the list, and only then swapping in rm or mv. This one reflex prevents the most common "oh no" moment for beginners.
🎯 Your Turn: clean up with wildcards
Fill in each ___ with the right glob, then run it and compare your result to the expected output.
# 🎯 YOUR TURN — clean up a messy folder with wildcards.
# The folder contains: a.log b.log notes.txt cat.jpg cat.png
# 1) List every .log file (preview before you delete anything!)
ls ___ # 👉 the glob for "anything ending in .log": *.log
# 2) Copy BOTH cat pictures (cat.jpg and cat.png) into pics/
cp cat.___ pics/ # 👉 one wildcard char matches the single letter: ?
# 3) Delete every .log file now that you've previewed them
rm ___ # 👉 reuse the same glob as step 1: *.log
ls
# ✅ Expected output:
# a.log b.log
# cat.jpg notes.txt pics___ with a wildcard pattern, run it in your terminal, and check the files that remain match the expected output.Common Errors (and the fix)
cp: -r not specified; omitting directory 'src/'— you tried to copy a folder without-r. Add it:cp -r src/ src-backup/.- You deleted the wrong thing with
rm -rf— and there's no undo. The only real fix is prevention: preview withlsfirst, avoid-funless you must, and userm -ifor anything important. - A file silently disappeared after
mvorcp. The destination already existed and got overwritten without warning. Usemv -i/cp -i(prompt) or-n(never overwrite) next time. rmdir: failed to remove 'logs': Directory not empty—rmdironly deletes empty folders. Empty it first, or userm -r logsif you really mean to delete its contents too.mkdir: cannot create directory 'src/pages': No such file or directory— the parentsrc/doesn't exist yet. Add-pso it's created for you:mkdir -p src/pages.
📋 Quick Reference
| Command | What it does |
|---|---|
| touch file | Create an empty file (or update its timestamp) |
| mkdir -p a/b/c | Create nested directories, parents included |
| cp -r src dst | Copy a file or (with -r) a whole directory |
| mv old new | Move, or rename when the folder stays the same |
| rm -i file | Delete a file, asking y/n first (no trash!) |
| rm -r dir | Delete a directory and everything in it |
| rmdir dir | Delete an empty directory only |
| cat file | Print a file's full contents |
| less file | Scrollable viewer for long files (q to quit) |
| ls *.txt | Match files with a wildcard (preview before rm!) |
Frequently Asked Questions
Q: Does rm move files to the trash or recycle bin?
No. rm deletes files immediately and permanently — there is no trash to restore from. Always run an ls with the same name or glob first to confirm exactly what will be removed, and use rm -i to be asked y/n before each delete.
Q: What is the difference between rm -r and rmdir?
rmdir only removes a directory that is already empty, so it fails loudly if you target the wrong thing — that safety is the point. rm -r removes a directory and everything inside it. Use rmdir for empty folders and rm -r (carefully) when you genuinely want to delete contents too.
Q: Why does cp say it omitted a directory?
cp copies a single file by default. To copy a directory and its contents you must add -r (recursive): cp -r src/ src-backup/. Without -r, cp refuses and prints something like "cp: -r not specified; omitting directory 'src/'".
Q: Will mv or cp warn me before overwriting an existing file?
By default, no — they silently replace the destination if it already exists, and the old contents are gone. Add the -i flag (cp -i, mv -i) to be prompted before any overwrite, or -n (no-clobber) to refuse overwriting entirely.
Q: What is the safest way to use wildcards like * with rm?
Run the exact same pattern with ls first (for example ls *.tmp) and read the list it prints. Only once you are happy that the list is correct should you swap ls for rm. The glob expands the same way for both commands, so the ls preview shows precisely what rm would remove.
Mini-Challenge: Build & Tidy a Website Folder
No blanks this time — just a brief and an outline. Work through the steps in your terminal, then check your folder against the expected ls output in the comments. This is exactly the kind of setup-and-cleanup you'll do at the start of real projects.
# 🎯 MINI-CHALLENGE: scaffold and tidy a website folder.
#
# 1. Create this structure in as few commands as you can:
# site/
# css/
# js/
# (hint: mkdir -p site/css and mkdir -p site/js)
# 2. Create empty files: site/index.html, site/css/main.css, site/js/app.js
# 3. Copy site/index.html to site/about.html
# 4. Rename site/js/app.js to site/js/main.js (use mv)
# 5. Preview, then delete every .html file in site/ with a single glob
#
# ✅ After step 4, "ls site" should show:
# about.html css index.html js
# ✅ After step 5, "ls site" should show:
# css js
# your commands herels site at the checkpoints noted in the comments.🎉 Lesson Complete!
- ✅
touchcreates empty files;mkdir -pbuilds nested folders in one go - ✅
cpcopies (use-rfor directories);mvmoves and renames - ✅
rmis permanent — there's no trash;rm -iasks first, andrm -rfis to be feared - ✅
catprints whole files;lessscrolls long ones (qto quit) - ✅ Wildcards
*,?, and[...]act on many files — alwayslsthem first - ✅ Next lesson: Text Processing — search and transform file contents with
grep,sed, andawk
Sign up for free to track which lessons you've completed and get learning reminders.