Removing files deliberately
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.
Before you start
Section titled “Before you start”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.
cd ~/lab/b06mkdir -p removal/projectcp ../Documents/notes.txt removal/project/notes.txttouch removal/project/checklist.txtcd removalpwdls -R/home/learner/lab/b06/removal.:project
./project:checklist.txt notes.txtmkdir -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.
Read the two refusals
Section titled “Read the two refusals”Try the command from the opening:
rm projectrm: cannot remove 'project': Is a directoryRead 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?
rmdir projectRun it, then compare with your guess
rmdir: failed to remove 'project': Directory not emptyAll 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:
ls -A projectchecklist.txt notes.txt-A includes hidden names while leaving out . and .., as in B05.3. Both disposable files survived the refusal.
Empty the directory deliberately
Section titled “Empty the directory deliberately”Name both files explicitly, then check the directory:
rm project/checklist.txt project/notes.txtls -A projectNeither 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:
rmdir projectls -AAgain, neither prints anything. This time rmdir succeeds: the opening problem is solved. The empty listing confirms that removal has no entries left.
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
Steps (9)
- Type
cd b06/removal - Press Enter
- Type
rmdir project - Press Enter
- Output:
rmdir: failed to remove 'project': Directory not empty - Type
rm project/checklist.txt project/notes.txt - Press Enter
- Type
rmdir project - 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.
Remove one whole disposable subtree
Section titled “Remove one whole disposable subtree”Sometimes you want an entire directory gone, including directories inside it. Build a small cache, a place for disposable working files:
mkdir -p cache/day1touch cache/day1/build.txtls -R cachecache:day1
cache/day1:build.txtYou 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?
rm -r cachels -ARun 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.
Good to know
Section titled “Good to know”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:
rm missing.txtrm: cannot remove 'missing.txt': No such file or directoryrm -f missing.txtThe 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.
Try it yourself
Section titled “Try it yourself”Goal: Clean a review package while preserving the report and its containing directories.
Prepare a different layout, still inside ~/lab/b06/removal:
mkdir -p review/send/day1mkdir review/empty review/keeptouch review/send/day1/old.txtcp ../../Documents/report.txt review/keep/report.txtls -R reviewreview:empty keep send
review/empty:
review/keep:report.txt
review/send:day1
review/send/day1:old.txtFrom 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
rm -r review/sendrmdir review/emptyls -R reviewcat review/keep/report.txtreview:keep
review/keep:report.txtQuarterly lab reportThe two removal commands succeed silently. ls -R confirms the surviving names, and cat confirms that the report still contains its text.
Reset this lesson
Section titled “Reset this lesson”Use this after finishing, or after stopping partway through. First return to the parent and inspect only this lesson’s directory:
cd ~/lab/b06ls -AR removal-A includes hidden entries; -R lists subdirectories. After the solution, the captured listing is:
removal:review
removal/review:keep
removal/review/keep:report.txtIf you stopped earlier, you will see the files left at that point instead.
rm -r ~/lab/b06/removalIt 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
rmremoves the files you name. By default, it refuses directories. rmdirremoves an empty directory and refuses one that still contains entries.rm -rremoves 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.
Further reading
Section titled “Further reading”- On your system:
man rmandman 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.