Linux: How to delete files recursively
Posted On June 13, 2020
The rm command deletes files. To delete a directory which is not empty and its subdirectories, use
$ rm -rf directory
However, sometimes you would need to remove specific subdirectory rather than the entire directory and its contents. For example:
|-- engine
|-- mod
|-- .svn
|-- anytext
| |-- .svn
| |-- actions
|-- crontrigger
| |-- .svn
| |-- views
We want to delete all .git directories and nothing else. We could manually do rm-r .git for every .git directory but there is an easier and more elegant way:
$ rm -rf `find . -type d -name .git`
find will find all directories name .git and rm would delete them and their contents.