Skip to main content

Remove file or folder recursively

· One min read
Hung Viet Nguyen

How to remove files and folders with a name pattern recursively?

Remove files recursively

  • Dry run
find . -name "\*.bak" -type f
  • Remove
find . -name "\*.bak" -type f -delete

Remove folders recursively

  • Dry run
find . -name 'node_modules' -type d -prune

If you want to remove all dist folders, but not in node_modules:

find . -name 'dist' -not -path "./node_modules/\*" -type d -prune
  • Remove
find . -name 'node_modules' -type d -prune -exec rm -rf '{}' +