How to Remove Directories on Your Raspberry Pi

By Sean McManus, Mike Cook

You can use two commands on your Raspberry Pi for removing directories. The first one, rmdir, is the safer of the two because it refuses to remove directories that still have files or directories inside them. Use it with the name of the directory you want to remove — for example, books, — like this:

rmdir books

If you want to prune a whole branch of the directory tree, you can use the rm command to remove a directory and delete anything inside it and its subdirectories. Used with the recursive option (-R), it works its way down the directory tree, and with the force option (-f), it deletes any files in its way. It’s a rampaging beast of a command. Here’s an example:

rm –Rf books

It acts silently and swiftly, deleting the books directory and anything in it.

You can add the interactive option to cut the risk, which prompts you for confirmation of each deletion, as you can see in this example where we’ve left a file in the folder work/writing/books:

pi@raspberrypi ~ $ rm @@enRfi work

rm: descend into directory 'work'? Y

rm: descend into directory 'work/writing'? Y

rm: descend into directory 'work/writing/books'? Y

rm: remove regular file 'work/writing/books/rapidplan.txt'? Y

rm: remove directory 'work/writing/books'? Y

rm: remove directory 'work/writing'? Y

rm: remove directory 'work'? Y

You can use wildcards when removing directories, but take special care with them and make sure you don’t introduce any unwanted spaces that result in your removing * (everything). If you use rm –Rf .* to try to remove hidden directories, you also match the current directory (.) and the parent directory (..). That means it deletes every file in the current directory (hidden or not), all its subdirectories and their contents (hidden or not), and everything in the parent directory, including its subdirectories (again, whether or not they are hidden).

The Linux community is friendly and supportive, and people welcome newcomers who want to join. But occasionally, you might come across some joker online advising inexperienced users that the solution to their problems is to issue the command rm -Rf /* as root, which attempts to delete everything, starting at the root.