Skip to content

Instantly share code, notes, and snippets.

@denisbrodbeck
Last active February 24, 2017 17:26
Show Gist options
  • Save denisbrodbeck/5d027f3a258728a901dc602627e69704 to your computer and use it in GitHub Desktop.
Save denisbrodbeck/5d027f3a258728a901dc602627e69704 to your computer and use it in GitHub Desktop.
Delete directories based on find results in Linux
# Search directories with name node_modules
find . -name 'node_modules' -type d
# Displays parent folders and child folders
./laravel/passport/node_modules (parent)
./laravel/passport/node_modules/yargs-parser/node_modules (child)
./laravel/passport/node_modules/gauge/node_modules (child)
./hapi/hapi-auth-jwt2/node_modules (parent)
./hapi/hapi-auth-jwt2/node_modules/acorn-jsx/node_modules (child)
./hapi/hapi-auth-jwt2/node_modules/optimist/node_modules (child)
# we want to delete parent folders anyway so no need to search inside those for child results
# Add '-prune' flag
find . -name 'node_modules' -type d -prune
# Displays parent folders only
./laravel/passport/node_modules
./hapi/hapi-auth-jwt2/node_modules
# Delete find results
# The {} gets replaced with the find result
find . -name 'node_modules' -type d -prune -exec rm -r {} \;
# Based on http://unix.stackexchange.com/questions/89925/how-to-delete-directories-based-on-find-output
# and http://unix.stackexchange.com/questions/30577/whats-the-in-find-path-exec-command-do
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment