Find a course

Module 2: Working with FilesLesson 4 of 1311 min

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.

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 reports creates a directory. mkdir -p archive/2025/march creates 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.txt creates an empty file, or updates the timestamp of an existing one without changing its contents.

Then copying:

  • cp report.txt backup.txt copies 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, cp refuses to copy directories.
  • cp -i asks before overwriting, and cp -p preserves 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.txt renames a file in place.
  • mv final.txt ~/Documents/ moves it to another directory.
  • mv old-name/ new-name/ renames a directory; mv handles 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.txt deletes a file. It does not go to a trash folder.
  • rmdir empty-dir removes a directory only if it is empty, which makes it a safe choice.
  • rm -r project/ removes a directory and everything inside it.
  • rm -f forces 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:

  1. Run ls with the exact same path first, to see what would be deleted.
  2. Use Tab completion for paths so typos show up before you hit Enter.
  3. Prefer rm -ri or rm -I when deleting by hand; -I asks once before removing many files.
  4. Never run rm -rf with 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).

  1. Rename a.txt to b.txt
  2. Copy a directory with all its contents
  3. Create nested directories in one step
  4. Remove an empty directory safely
  5. Create an empty file

Step 4 of 13Compare

cp, mv and rm side by side

Aspectcpmvrm
What it doesCopiesMoves or renamesDeletes
Original afterwardsStill thereGone from the old placeGone for good
DirectoriesNeeds -rWorks without -rNeeds -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.

A safe delete routineProcess diagram: ls the exact path → Tab-complete the path → rm -ri or rm -I → ls again.ls the exactpathTab-completethe pathrm -ri or rm-Ils again

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.

PatternMatchesExample
*any number of characters, including none*.jpg matches every name ending in .jpg
?exactly one characterreport?.txt matches report1.txt but not report10.txt
[abc]one character from the setfile[12].log matches file1.log and file2.log
[0-9]one character from a rangeimg[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 with draft-.
  • ls /var/log/*.log lists 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?