How to recursively delete a directory forcibly in Linux#
You probably know that
rmdir is the bash shell command to delete directories in Linux. But the command will delete the directory only if it is empty.
$ rmdir boringpics rmdir: boringpics/: Directory not empty
Would you need to manually traverse into a 10 level deep directory structure and delete the files in the subdirectories so that you can delete the top directory?
Can't there be a more intuitive way? The command below will delete the directory
boringpics and its sub directories in a flash:
$ rm -rf boringpics
-r option is used for recursing into the subdirectories. The
-f forces the deletion of write protected files without prompting. If you are not sure about which files you want to delete, you might want to do someting like this:
$ rm -r boringpics
Did you notice that we used
rm instead of
rmdir to recursively delete a directory and its subdirectories? Looking strange? The use of
rm for deleting a directory-as-we-know should come as no surprise because everything is a file in Linux - even you printer!