0

I want this - https://stackoverflow.com/a/43561012/126833

find . -name 'node_modules' -type d -prune -exec rm -rf '{}'

except - instead of getting this done automatically, I want the all of the rm -rf /path/to/project/node_modules commands in a bash file like rm_node_modules.sh for me to review and then only I'll execute as bash rm_node_modules.sh

So basically my rm_node_modules.sh should be like :

rm -rf /path/to/project-1/node_modules
rm -rf /path/to/project-2/node_modules
rm -rf /path/to/project-3/node_modules
anjanesh
  • 3,771
  • 7
  • 44
  • 58
  • It is hard to understand your question. Are you asking how to put the `find` command in a shell script? Or how to hardcode a bunch of `rm`s into a shell script? – Don't Panic Jul 14 '22 at 07:57
  • I want the above `rm_node_modules.sh` file generated for a folder that contains node_modules. I can write code in python or php to look for node_modules in a folder and print the rm command for each folder - I was just wondering if there's a one-liner bash command do achieve this. – anjanesh Jul 14 '22 at 09:42
  • Can you try `... -exec echo rm -rf '{}' > rm_node_modules.sh` ? – Philippe Jul 14 '22 at 10:54

1 Answers1

0

You are on macOS where find does not support the -printf flag which would make this a bit simpler. But something like this will do the job:

find . -name node_modules -type d -print0 | xargs -0 stat -f "rm -rf %N" > rm_node_modules.sh
Don't Panic
  • 13,965
  • 5
  • 32
  • 51
  • Thanks for the one-liner. I somehow dread the idea of an automatic `rm -rf` command. I'm afraid of a catastrophe. – anjanesh Jul 14 '22 at 12:09
  • Yes, fair enough. You can make it interactive, so you have to confirm each one, by using `rm -rfi`. – Don't Panic Jul 14 '22 at 12:11
  • I ran your command in my workspace folder and the bash file has 116 rm commands. So that would be a lot of ENTERs. I thought I would manually glace through the parent folders and decide in case there are any I dont want deleted. – anjanesh Jul 14 '22 at 14:25