In bash all I know is that
rmdir directoryname
will remove the directory but only if it’s empty. Is there a way to force remove subdirectories?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
The following command will do it for you. Use caution though if this isn’t your intention as this also removes files in the directory and subdirectories.
rm -rf directoryname
Method 2
if rm -rf directoryname fails you, try using rm -R -f directoryname, or rm --recursive -f directoryname.
If you are not having any luck with these, you should consider reinstalling rm or switching shells.
Method 3
Other answers show how to completely remove a directory’s content, but IMO they don’t address the literal question of the original post — that is, how can one delete subdirectories (as opposed to usual files). In other words, how can one delete empty directory structures while keeping subdirectories containing files ?
This can be achieved with find :
find directoryname -type d -delete
This command will recursively search for directories (-type d) through directoryname and -delete them only if their subdirectories or themselves don’t contain any files.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0