Hone

Lessons · Terminal · deleting, with no undo

Deleting, for good

rm deletes a file with no trash can and no undo. A folder needs rm -r, which removes everything inside it too.

Hone is a place to practise programming. This is one of its lessons, written out in full and free to read without an account.

What it is for

Every developer has one story about rm. The command is not dangerous; being sure of your folder and your glob before pressing Enter is the whole skill.

How to think about it

ls first, so you see exactly what a name or a *.txt will match. Then rm. Reach for -r only for folders, and be doubly sure then.

Worked example

touch old.txt && rm old.txt
Gone.
ls | wc -l
0 files left.
mkdir d && rm d 2>/dev/null || echo "rm refused the folder"
rm will not remove a folder without -r.
rm -r d && ls | wc -l
With -r it goes, contents and all: 0.

Your turn

Delete the build folder and everything in it.

rm  build

The trap

rm -rf with a variable that turned out empty: rm -rf $DIR/ becomes rm -rf /. Quote it, check it, or use set -u.

Practise deleting, with no undo on HoneA question on it now, a coding challenge where there is one, and it is remembered for review. Free, no email needed.