Removing Directories
Deleting folders needs a little more care than deleting files. You have two tools.
Empty folders: rmdir
The rmdir command (remove directory) deletes a folder, but only if it is empty:
rmdir old-folder
If the folder still contains anything, rmdir refuses:
rmdir reports
rmdir: failed to remove 'reports': Directory not empty
This refusal is a safety feature: it stops you wiping out files by accident.
Folders with content: rm -r
To delete a folder and everything inside it, use rm with the -r option
(recursive):
rm -r reports
This removes reports, every file in it, every sub-folder, and their contents, all
permanently.
Handle with care
rm -r is powerful and dangerous. It deletes entire trees with no confirmation and no
undo. Always ls the folder first to be certain of what is inside.
| Command | Use it for |
|---|---|
rmdir | an empty folder (safe) |
rm -r | a folder with content (powerful, careful!) |
Mission: Decommission Old Bays
Two storage bays on the station are scheduled for decommissioning. The empty-bay folder has already been cleared out, but old-data still holds leftover files, and one of them, a.log, must be preserved before the bay is wiped.
- Inspect the workspace, then look inside
old-data, to see exactly what is there before you delete anything. - Create an
archivefolder and copyold-data/a.loginto it as a safety backup. - Remove the empty
empty-bayfolder, the safe way that only works on empty folders. - Remove the
old-datafolder and everything inside it.
Expected result
Both bays are gone, a.log survives inside archive, and the mission folder is untouched.