Raspberry Pi For Dummies, 4th Edition
Book image
Explore Book Buy On Amazon
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:

[email protected] ~ $ 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.

About This Article

This article is from the book:

About the book authors:

Sean McManus is an expert technology and business author. His previous books include Mission Python, Coder Academy, and Cool Scratch Projects in Easy Steps. Mike Cook is a lifelong electronics buff, a former lecturer in physics at Manchester Metropolitan University, and the author of more than 300 articles on computing and electronics. You'll often find him monitoring technology forums under the moniker Grumpy Mike.

This article can be found in the category: