🎓 A1Learner
🔍
0 of 50 lessons completed

Shell Deleting Files

Deleting a file removes it from the computer. A deleted file does not go to a bin, and there is no undo.

Delete a file

rm deletes old.txt, and the second ls prints nothing.

mkdir box
touch box/old.txt
ls box
rm box/old.txt
ls box

Delete a folder

rm -r deletes old-folder and the file inside it.

mkdir crate
mkdir crate/old-folder
touch crate/old-folder/note.txt
ls crate
rm -r crate/old-folder
ls crate

Ask first with -i

rm -i asks before it deletes, and it waits for an answer of y or n.

rm -i old.txt

Delete one file, keep the rest

rm deletes only the file you name.

mkdir chest
touch chest/keep.txt
touch chest/delete.txt
ls chest
rm chest/delete.txt
ls chest

Example

mkdir pack
touch pack/temp.txt
mkdir pack/temp-folder
ls pack
rm pack/temp.txt
rm -r pack/temp-folder
ls pack

Key points

  • rm: a command that deletes a file.
  • Use rm to delete a file you no longer need.
  • Use rm -r to delete a folder and all the files in it.
  • Use rm -i to make the shell ask before each file is deleted.

In this example, mkdir and touch make a file and a folder inside pack. Then ls shows both of them. Next rm deletes the file, and rm -r deletes the folder. The last ls prints nothing, so the pack folder is empty.

Note

check the name twice before you press Enter, because deletion has no undo.

Try it Yourself

Edit the code, then press Run.

Running is simulated

Exercise

Which command deletes a folder and all its files?