Creating, Copying, Moving and Deleting
Use mkdir, touch, cp, mv and rm with the right options, rename files, and avoid the mistakes that permanently delete the wrong data.
Welcome back!
Step 1 of 13Learn
No trash can
Marco wanted to clean up old log files and typed rm -r /var/www /old-logs. He meant /var/www/old-logs, but the extra space turned one path into two. The website's files disappeared instantly, with no "Are you sure?" and no trash can to restore from. He spent the evening restoring from a backup.
That story is the right frame for this lesson. File commands are simple and fast, and that speed cuts both ways. Learn them together with the safety habits.
Creating things first:
mkdir reportscreates a directory.mkdir -p archive/2025/marchcreates the whole chain of directories in one go, including any parents that don't exist yet, and doesn't complain if they already do.touch notes.txtcreates an empty file, or updates the timestamp of an existing one without changing its contents.
Then copying:
cp report.txt backup.txtcopies a file to a new name.cp report.txt archive/copies it into a directory, keeping the name.cp -r site/ site-backup/copies a directory recursively, with everything inside it. Without-r,cprefuses to copy directories.cp -iasks before overwriting, andcp -ppreserves timestamps and permissions.
Step 2 of 13Learn
Moving, renaming and removing
Linux has no separate rename command for single files. Moving and renaming are the same operation:
mv draft.txt final.txtrenames a file in place.mv final.txt ~/Documents/moves it to another directory.mv old-name/ new-name/renames a directory;mvhandles directories without needing-r.
By default mv and cp silently overwrite a file that already exists at the destination. Add -i (interactive) to be asked first, or -n to never overwrite.
Removing:
rm file.txtdeletes a file. It does not go to a trash folder.rmdir empty-dirremoves a directory only if it is empty, which makes it a safe choice.rm -r project/removes a directory and everything inside it.rm -fforces removal without prompts or errors for missing files.
The combination rm -rf is the most dangerous everyday command on Linux. It deletes whole trees with no questions. Safety habits that professionals use:
- Run
lswith the exact same path first, to see what would be deleted. - Use Tab completion for paths so typos show up before you hit Enter.
- Prefer
rm -riorrm -Iwhen deleting by hand;-Iasks once before removing many files. - Never run
rm -rfwith a variable you haven't checked, especially in scripts.
Step 3 of 13Practice · Match the pairs
Match each task to the command that does it.
Tap an answer, then tap the place where it belongs (or drag it there).
- Rename
a.txttob.txt - Copy a directory with all its contents
- Create nested directories in one step
- Remove an empty directory safely
- Create an empty file
Step 4 of 13Compare
cp, mv and rm side by side
| Aspect | cp | mv | rm |
|---|---|---|---|
| What it does | Copies | Moves or renames | Deletes |
| Original afterwards | Still there | Gone from the old place | Gone for good |
| Directories | Needs -r | Works without -r | Needs -r (or rmdir if empty) |
| Safety option | -i asks before overwriting | -i asks, -n never overwrites | -i asks each time, -I asks once |
Step 5 of 13Practice · What would you do?
What would you do?
You need to delete the directory /srv/app/cache and everything in it. You have typed rm -rf /srv/app /cache but haven't pressed Enter yet. What should you do?
Step 6 of 13Diagram
A safe delete routine
The professional habits from this lesson, in the order you use them.
Tap a shape with to see more.
- ls the exact path
See what would be deleted before deleting it.
- Tab-complete the path
Typos and stray spaces show up before you press Enter.
- rm -ri or rm -I
Ask for confirmation when deleting by hand.
- ls again
Confirm the right things are gone and nothing else is.
Step 7 of 13Learn
Wildcards: acting on many files
Remember Jordan's 400 photos? Wildcards, also called globs, are how you name many files at once. The shell expands them into a list of matching names before the command runs.
| Pattern | Matches | Example |
|---|---|---|
* | any number of characters, including none | *.jpg matches every name ending in .jpg |
? | exactly one character | report?.txt matches report1.txt but not report10.txt |
[abc] | one character from the set | file[12].log matches file1.log and file2.log |
[0-9] | one character from a range | img[0-9][0-9].png matches img07.png |
Examples:
cp *.pdf ~/Documents/copies every PDF in the current directory.rm draft-*deletes every file whose name starts withdraft-.ls /var/log/*.loglists only the .log files in that directory.
Two details trip people up. First, * does not match hidden files that start with a dot, so rm * leaves .env behind. Second, because the shell expands the pattern, you can preview exactly what a command will touch by running echo or ls with the same pattern: echo rm *.tmp prints the expanded command without deleting anything.
And the rename job? A short loop, which you will write yourself in the scripting module: for f in IMG_*.jpg; do mv "$f" "italy-$f"; done.
Step 8 of 13Practice · Select all that apply
A directory contains: notes.txt, notes1.txt, notes12.txt, Notes2.txt, report.txt, .notes.txt. Which files does the pattern notes?.txt match? Select all that apply.
Select all that apply.
Step 9 of 13Flashcards
Read the glob
Tap a card to flip it.
Step 10 of 13Note
Step 11 of 13Practice · Fill in the blanks
Fill in the blanks
To copy the directory photos and everything inside it to photos-backup, run `cp photos photos-backup. To be asked before mv` overwrites an existing file, add the option .
Step 12 of 13Practice · True or false
Running rm notes.txt moves the file to a trash folder where you can restore it later.
Step 13 of 13Practice · Choose one
You want to delete every file ending in .tmp in the current directory, but first you want to see exactly which files that would include. What should you run first?
Lesson complete!