Delete all node_modules subfolders - linux

I'm doing a HDD upgrade, and while trying to backup my NodeJS projects i realize that all node_modules subfolders have over 1 milion files.
So i'm looking for a way to remove all the node_modules subfolders and all their contents.
The projects are located in :
C:/Node/App1/node_modules/..
C:/Node/App2/node_modules/..
C:/Node/App3/node_modules/..
etc..
My OS is Windows 10, but i can try either Windows or Linux commands because i'm using cmder, and it accepts both types of commands.

For Linux:
cd Node
find . -maxdepth 2 -name node_modules -type d -exec rm -rf {} +

I tested this command in various situations and it seems to work fine.
for /d /r . %d in (node_modules) do #if exist "%d" rd /s/q "%d"
I'm not sure how it works, it's an adapted solution from Here, if you need more details.

Related

rm -rf equivalent on Windows batch script

I want to delete multiple folders & files at once and the command should not broke even if some directories or files aren't preset. Also I use wildcards(*) to combine folder & file names.
I have the following command to delete folders in Ubuntu
rm -rf dist pkitree srktable cst_sign* cst_encrypt* *.log out
However when I try to do accompany the same in Windows batch like,
rd /s /q dist pkitree srktable cst_sign* cst_encrypt* *.log out 2> NUL
I still get error status and the wildcards are not recognised. Can someone help me achieving this behaviour in Windows as well?
You can try :
powershell -Command "Remove-Item -Recurse dist, *.log"

How to remove empty folders and subfolders using cronjob

I found a script online that works when entered into custom cronjobs on cpanel.
When i run this script, the code deletes the folder and everything within it.
rm -rf public_html/storage_area/images/
I would like to delete empty sub-folders housed within the images folder and not the actual images folder itself.
I do not have much technical knowledge so any help would be much appreciated. I have tried a few php scripts that i found online but did not have much luck so if there is something that exists even better.
Thank you for any assistance.
You just need to modify the command a bit.
If you need to remove only files inside that folder you can use,
rm -rf public_html/storage_area/images/*.*
The *.* will only remove files within the folder public_html/storage_area/images/ having an extension.
If you need to remove files and sub folders, then you need to use
rm -rf public_html/storage_area/images/*
If you only need to remove sub folders which are empty, you can use
find -type d -empty -delete
Before running the above command, you may need to verify whether the command is only returning empty folders. For that you can use,
find public_html/storage_area/images/ -type d -empty -print

Is there a way to delete all node_module without opening the every single project from local drive?

I have multiple project that uses node_modules which i didn't touched in month.
If i can remove node_module only that can save my 5 to 8 GB storage.
I found Command line tool to delete folder with a specified name recursively in Windows
But that shows delete file in specific folder like
D:\Project\Doing\prject1\
D:\Project\Complete\Project1\
FOR /d /r . %d IN (project1) DO #IF EXIST "%d" rd /s /q "%d"
But i don't want to search every directory to look for node_module instead i want to delete all the node_module folder from my PC (all
the drive) how can i do that?
If I need the node_modules back I can simply run npm install so clearing space is good idea for me.
I found the solution here that worked like a charm.
find . -name "node_modules" -exec rm -rf '{}' +
To list all your node_modules use this command in a command prom:
FOR /d /r . %d in (node_modules) DO #IF EXIST "%d" echo %d"
And for deleting you can do:
FOR /d /r . %d in (node_modules) DO #IF EXIST "%d" rm -rf "%d"
This will not work in powershell only cmd

Deleting a specific sub directory under all directories

I have several tests in a directory which were compiled in C and created the image or output in 'unmanaged' folders under each test directory. I would like to delete only the unmanaged folders under every test directory (a cleanup you can say) and keep all other directories like src, cmm, scf (scatter files) etc. unchanged.
I can do this either in Windows or Linux since the directory is mapped to Windows. Please let me know how do I proceed.
find . -name unmanaged -type d -exec rm -rf {} \;

how to delete all files in directory except one folder and one file?

I have application which has one folder called vendor and one file called .env. When ever i automatically publish my source code files to folder, all old files should get deleted except these two.
How can i do this in linux by using shell?
PS : I am trying to implement rollback mechanism in Jenkins. I will copy artifacts from old build and transfer them to server using ssh. But this will be a copy operation. So I want to delete previous files before starting copy using SSH.
You can use find:
find ! \( -name 'name1' -o -name 'name2' \) -exec rm -r {} +
try with this command
rm !(<filename>)

Resources