Skip to content

Removing files deliberately

Module
Working with Files and Directories
Difficulty
Easy
Updated
Tested on
Ubuntu 24.04.5 LTS (lab container), GNU Bash 5.2.21, GNU Coreutils 9.4
On this page

You have finished with a disposable project directory. You try to remove it, but the terminal refuses. Switching to a command meant for directories gives another refusal: something is still inside.

By the end, you can read both messages, remove only the files you name, and remove the directory once it is empty. You can also choose when to remove a whole disposable directory and its contents together.

Finish Creating and organizing files (B06.1). Keep the existing ~/lab from B04.1’s setup, including its original notes.

Use only ~/lab/b06/removal for this lesson. Its project is separate from B06.1’s ~/lab/b06/project. Leave the earlier project, archive, and handoff where they are.

Run this setup once. If you have already started this lesson, use Reset this lesson before repeating it.

Terminal window
cd ~/lab/b06
mkdir -p removal/project
cp ../Documents/notes.txt removal/project/notes.txt
touch removal/project/checklist.txt
cd removal
pwd
ls -R
Captured output
/home/learner/lab/b06/removal
Captured output
.:
project
./project:
checklist.txt notes.txt

mkdir -p creates the missing directories. cp copies the original note, and touch creates an empty checklist, as in B06.1. From b06, ../Documents reaches the source directory without changing it. ls -R lists the working directory and each directory inside it.

Your prompt ends in ~/lab/b06/removal$. Every relative path below starts here.

Try the command from the opening:

Terminal window
rm project
Captured output
rm: cannot remove 'project': Is a directory

Read it in the three parts from B04.4. rm: names the program speaking. 'project' names its target. Is a directory explains the refusal. Plain rm removes files; by default, it does not remove directories. This message does not say that the path is missing or that you need more permission.

Try the command for directories. Guess first: will it remove project, remove either file inside it, or leave all three names in place?

Terminal window
rmdir project
Run it, then compare with your guess
Captured output
rmdir: failed to remove 'project': Directory not empty

All three names remain. rmdir: is speaking this time, and it names the same target. Directory not empty is the clue: the directory exists, but it still contains entries. rmdir removes empty directories only. It does not empty them for you.

Check the contents:

Terminal window
ls -A project
Captured output
checklist.txt notes.txt

-A includes hidden names while leaving out . and .., as in B05.3. Both disposable files survived the refusal.

Name both files explicitly, then check the directory:

Terminal window
rm project/checklist.txt project/notes.txt
ls -A project

Neither command prints anything. rm removed the two named files without asking a question. ls -A then found no entries to list. A successful removal can be silent; do not wait for a confirmation prompt before deciding whether the target is right.

The command has two arguments, one path for each file. Removing the copied notes.txt does not remove the original in ~/lab/Documents. You are still outside project, in its parent directory.

Remove the empty directory:

Terminal window
rmdir project
ls -A

Again, neither prints anything. This time rmdir succeeds: the opening problem is solved. The empty listing confirms that removal has no entries left.

Empty a disposable project before removing it
learner@rootvolt:~/lab$ cd b06/removallearner@rootvolt:~/lab/b06/removal$ rmdir projectrmdir: failed to remove 'project': Directory not emptylearner@rootvolt:~/lab/b06/removal$ rm project/checklist.txt project/notes.txtlearner@rootvolt:~/lab/b06/removal$ rmdir projectlearner@rootvolt:~/lab/b06/removal$ 

Files ~/lab/b06/removal

  • ~
    • lab/b06/removal/cwd

Step-by-step replay of a verified session · Recorded on Ubuntu 24.04.5 LTS, GNU Bash 5.2.21. rmdir refuses the nonempty project; rm removes its two named files, then rmdir removes the empty directory.
Steps (9)
  1. Type cd b06/removal
  2. Press Enter
  3. Type rmdir project
  4. Press Enter
  5. Output: rmdir: failed to remove 'project': Directory not empty
  6. Type rm project/checklist.txt project/notes.txt
  7. Press Enter
  8. Type rmdir project
  9. Press Enter

In the Files panel, the first refusal leaves both files in place. The next removal marks checklist.txt and notes.txt removed for that step. The last removal marks project removed. The static transcript and final tree show the same outcome, with no project remaining. The panel shows names disappearing, not a recoverable copy of their contents.

Sometimes you want an entire directory gone, including directories inside it. Build a small cache, a place for disposable working files:

Terminal window
mkdir -p cache/day1
touch cache/day1/build.txt
ls -R cache
Captured output
cache:
day1
cache/day1:
build.txt

You could remove build.txt, then day1, then cache. Another choice is one recursive operation, like the recursive copy in B06.1, but with removal as its job.

Guess first: which names survive this command: cache, cache/day1, cache/day1/build.txt, or the parent removal?

Terminal window
rm -r cache
ls -A
Run it, then compare with your guess

Neither command prints anything. Only the parent removal survives. The listing is empty, and the prompt still ends in ~/lab/b06/removal$.

-r means recursive: remove the named directory, its contents, and the directories inside it. It does not mean “empty this directory but keep it”. It also does not select the parent directory.

Choose rmdir when the directory should already be empty. Its refusal tells you your expectation was wrong. Choose rm -r only when you intend to remove the entire named subtree, meaning that directory and everything below it.

In Files, moving an item to Trash keeps it somewhere you can restore it from. rm and rmdir do not use that desktop operation. Running them in a terminal window on the same desktop does not send their targets to Trash.

The commands captured here ask no questions. rm -i explicitly asks before each removal, but an accepted prompt still removes the file; it does not save a backup. Check the target before running the command, with or without that option.

Inside ~/lab/b06/removal, compare a missing path with and without -f:

Terminal window
rm missing.txt
Captured output
rm: cannot remove 'missing.txt': No such file or directory
Terminal window
rm -f missing.txt

The second command prints nothing. -f (force) never prompts and stays silent about missing paths, so it removes the warnings this lesson teaches you to read. That is why the common rm -rf deserves extra care. This course does not use -f unless a lesson says so.

Goal: Clean a review package while preserving the report and its containing directories.

Prepare a different layout, still inside ~/lab/b06/removal:

Terminal window
mkdir -p review/send/day1
mkdir review/empty review/keep
touch review/send/day1/old.txt
cp ../../Documents/report.txt review/keep/report.txt
ls -R review
Captured output
review:
empty keep send
review/empty:
review/keep:
report.txt
review/send:
day1
review/send/day1:
old.txt

From removal, ../../Documents goes up through b06 to lab, then into Documents. The report is a copy.

Remove the whole send subtree in one removal command, and remove empty with a command that refuses nonempty directories. Keep review, keep, and report.txt. Then check the names and read the report.

Expected: Only keep/report.txt remains under review, with its original report text.

Hint

The two targets need different choices. One includes a directory inside it; the other has no contents. Neither command needs to target review itself.

Solution
Terminal window
rm -r review/send
rmdir review/empty
ls -R review
cat review/keep/report.txt
Captured output
review:
keep
review/keep:
report.txt
Captured output
Quarterly lab report

The two removal commands succeed silently. ls -R confirms the surviving names, and cat confirms that the report still contains its text.

Use this after finishing, or after stopping partway through. First return to the parent and inspect only this lesson’s directory:

Terminal window
cd ~/lab/b06
ls -AR removal

-A includes hidden entries; -R lists subdirectories. After the solution, the captured listing is:

Captured output
removal:
review
removal/review:
keep
removal/review/keep:
report.txt

If you stopped earlier, you will see the files left at that point instead.

Terminal window
rm -r ~/lab/b06/removal

It succeeds silently. Run the setup under Before you start again to restore the initial project and its two files. If removal is already absent, skip this removal and run the setup. This reset restores only the lesson workspace; it does not restore a VM snapshot or recreate earlier exercises.

  • Plain rm removes the files you name. By default, it refuses directories.
  • rmdir removes an empty directory and refuses one that still contains entries.
  • rm -r removes a named directory and its entire subtree. Inspect that target first.
  • Command-line removal normally bypasses Trash. A confirmation prompt does not make a backup.
  • Check the surviving names with ls -A; successful removal can be silent.

Next, Filenames and metadata (B06.3) handles names with spaces or leading hyphens and inspects file types, sizes, ownership, and times.

  • On your system: man rm and man rmdir. The Ubuntu 24.04 pages are rm(1) and rmdir(1), including recursive removal and prompting behavior.
  • GNOME Help: Delete files and folders, on moving items to Trash and permanently deleting them in Files.