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:<strong>rmdir books</strong>
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:
<strong>rm –Rf books</strong>
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 ~ $ <strong>rm @@enRfi work</strong>
rm: descend into directory 'work'? <strong>Y</strong>
rm: descend into directory 'work/writing'? <strong>Y</strong>
rm: descend into directory 'work/writing/books'? <strong>Y</strong>
rm: remove regular file 'work/writing/books/rapidplan.txt'? <strong>Y</strong>
rm: remove directory 'work/writing/books'? <strong>Y</strong>
rm: remove directory 'work/writing'? <strong>Y</strong>
rm: remove directory 'work'? <strong>Y</strong>
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.