File Management Commands
Reviewed & published by Brayan K
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.
Part of the free Command Line course at LearnCodingFast — hands-on lessons with worked examples and the output they print, plus practice exercises and a quick quiz.
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 (*, ?, [])
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 -F2️⃣ 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.txt🎯 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.css3️⃣ 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 -FDANGER: 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.pngPro 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 picsCommon 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 with ls first, avoid -f unless you must, and use rm -i for anything important.
- A file silently disappeared after mv or cp. The destination already existed and got overwritten without warning. Use mv -i / cp -i (prompt) or -n (never overwrite) next time.
- rmdir: failed to remove 'logs': Directory not empty — rmdir only deletes empty folders. Empty it first, or use rm -r logs if you really mean to delete its contents too.
- mkdir: cannot create directory 'src/pages': No such file or directory — the parent src/ doesn't exist yet. Add -p so 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!) |
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 here🎉 Lesson Complete!
- ✅ touch creates empty files; mkdir -p builds nested folders in one go
- ✅ cp copies (use -r for directories); mv moves and renames
- ✅ rm is permanent — there's no trash; rm -i asks first, and rm -rf is to be feared
- ✅ cat prints whole files; less scrolls long ones (q to quit)
- ✅ Wildcards *, ?, and [...] act on many files — always ls them first
Practice quiz
Which command creates an empty file?
- mkdir
- touch
- cp
- rm
Answer: touch. touch creates an empty file (or updates a file's timestamp).
Which mkdir flag creates missing parent folders in one go?
- -p
- -r
- -a
- -f
Answer: -p. mkdir -p builds the whole nested path, parents included.
What does the cp command do?
- Moves a file
- Deletes a file
- Copies a file (original stays)
- Renames a file
Answer: Copies a file (original stays). cp copies; the original stays in place.
To copy a whole directory with cp you must add which flag?
- -i
- -p
- -a
- -r
Answer: -r. cp -r (recursive) is required to copy a directory and its contents.
What does mv do when the destination is a new name in the same folder?
- Renames the file
- Copies the file
- Deletes the file
- Creates a folder
Answer: Renames the file. Same folder plus a new name is a rename.
After running rm on a file, where does it go?
- The trash
- The recycle bin
- A backup folder
- Nowhere - it is permanently deleted
Answer: Nowhere - it is permanently deleted. rm deletes immediately and permanently; there is no trash.
Which command removes only an empty directory?
- rm -r
- rmdir
- mv
- touch
Answer: rmdir. rmdir only removes an empty directory - a built-in safety check.
In a wildcard pattern, what does * match?
- Exactly one character
- Only digits
- Any run of characters (zero or more)
- Only letters
Answer: Any run of characters (zero or more). * matches any run of zero or more characters.
In a wildcard pattern, what does ? match?
- Exactly one character
- Zero or more characters
- A whole word
- A directory
Answer: Exactly one character. ? matches exactly one character.
Before deleting files with a glob like rm *.tmp, what is the safe habit?
- Add the -f flag
- Preview with ls *.tmp first
- Run it twice
- Reboot the machine
Answer: Preview with ls *.tmp first. ls expands the glob the same way, so preview with ls before rm.
Continue this course
- Previous: File System Navigation
- Next: Pipes and Redirection — Chain commands together with | and redirect output with > and >>
- Quick reference: Command Line cheat sheet › Files & Folders
Frequently asked questions
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.
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.
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/'".
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.
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.