Removing Files and Directories Safely
How it works
rm FILE removes a directory entry; it normally does not send the file to a desktop trash folder. rmdir DIRECTORY removes an empty directory and refuses a nonempty one. Removal of ordinary files is mainly governed by access to the containing directory, with additional restrictions possible. GNU rm -i asks before each removal; -- ends option parsing. Recursive removal (rm -r) descends into a tree and is much riskier; this lesson does not ask you to use it. If a file has another hard link or is still open, removing one name need not immediately release its data.
Prepare a safe practice environment
Use GNU/Linux and Bash as a normal user. From the extracted package root run bash lab-fixtures/files/setup.sh. It prints a fresh private directory under /tmp. Copy that path and enter it with cd (quote the path if needed); check pwd. Each code example below starts in a new lab with the same fixture: notes/hello.txt, notes/daily report.txt, notes/-draft, notes/.hidden, and empty directories archive and empty. Set export LC_ALL=C for the shown ordering and English diagnostics. Never run the modifying examples on your own files.
Inspect and remove one fixture file
pwdls -l -- notes/-draftrm -- notes/-draftls -1A notesThe initial output identifies the lab and file; the final listing has .hidden, daily report.txt and hello.txt, but not -draft.
Remove an empty directory
Prints archive and notes. The empty directory is gone.
Observe safe refusal
This is an intentional failure. The directory is not empty, so it remains. GNU rmdir produces a diagnostic and nonzero status.
The nonempty-directory refusal is valuable evidence: the target has entries that must be considered separately. In this practice the correct outcome is to leave notes intact. Check its listing before deciding on any later operation; do not treat a nonzero status as a reason to escalate the command.
Common mistakes and safe habits
Before deleting, verify the current directory and exact target; avoid wildcards and recursive or force flags while learning. Do not replace a failed rmdir with recursive deletion just to silence the error. A command-line deletion may be unrecoverable without backups. Only remove the explicitly named fixture files here.
Try it yourself
In a fresh lab inspect notes/-draft, remove that one file and verify that notes/hello.txt remains. Remove empty with rmdir; do not remove notes.
Use a fresh lab for the practice. When finished, return to the lab root if you are in a subdirectory, then leave it with cd ... Remove only the identified temporary lab folder using your file manager if desired. Never remove an unrelated path.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Removing files safely — Knowledge Check
Apply filesystem concepts in a fresh disposable Linux lab.
Which command refuses to remove a nonempty directory?
Does ordinary rm normally send a file to desktop Trash?
What should you verify first before deleting a lab file?
What does GNU rm -i do?
Which approach is safest when you only want to remove a single known fixture file?