programing

명령줄을 사용하여 지정된 경로에서 node_modules 폴더를 재귀적으로 삭제합니다.

lastcode 2023. 5. 17. 23:15
반응형

명령줄을 사용하여 지정된 경로에서 node_modules 폴더를 재귀적으로 삭제합니다.

로컬 디렉터리에 여러 개의 npm 프로젝트가 저장되어 있습니다.이제 node_modules 폴더 없이도 프로젝트를 백업하고 싶습니다. 이는 많은 공간이 필요하고 다음을 사용하여 언제든지 검색할 수 있기 때문입니다.npm install.

그렇다면 명령줄 인터페이스를 사용하여 지정된 경로에서 모든 node_modules 폴더를 재귀적으로 삭제하는 방법은 무엇입니까?

삭제할 디렉토리 목록을 인쇄합니다.

find . -name 'node_modules' -type d -prune

현재 작업 디렉토리에서 디렉토리 삭제:

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

또는 휴지통을 사용할 수 있습니다.brew install trash) 삭제의

find . -name node_modules -type d -prune -exec trash {} +

https://github.com/voidcosmos/npkill 을 사용해 보십시오.

npx npkill

모든 것을 찾을 것입니다.node_modules선택적으로 제거할 수 있습니다.

npkill

승인된 답변에 대한 개선:

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

위의 명령어는 모든 폴더를 가져온 다음 삭제 명령을 실행하는 데 매우 오랜 시간이 걸린다는 것을 발견했습니다.명을다시시려면다음사것좋다습니이는용하을령작하다▁to니좋▁res▁i를 사용하는 것이 \; 상황을 실행중명진행보사다다니합용음을려를 합니다.-print삭제 중인 디렉터리를 확인합니다.

참고: 먼저 루트 디렉터리에 CD를 넣은 다음 명령을 실행해야 합니다.아니면 대신에find .,사용하다find {project_directory}.

폴더를 하나씩 삭제하는 방법

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

폴더를 하나씩 삭제하고 삭제 중인 폴더를 인쇄하려면 다음과 같이 하십시오.

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

대화식 방법을 좋아하는 사람들은 제프의 대답을 참고하세요.자를 디렉토리에서 실행합니다.

npx npkill

나는 이 해결책을 발견했습니다.

  • 먼저다사폴찾더기여용을 사용하여 .find폴더 이름을 지정합니다.
  • 을 재귀적으로 합니다.-exec rm -rf '{}' +

폴더를 반복적으로 삭제하려면 다음 명령을 실행합니다.

find /path -type d -name "node_modules" -exec rm -rf '{}' +

)에서는 다음과 같은 (윈도우)를 합니다..BAT입니다.node_modules현재 폴더에서 재귀적으로:

@for /d /r . %d in (node_modules) do @if exist %d (echo %d && rd %d /s /q) 

또는 을 통해CMD.EXE:

>cmd.exe /c "@for /d /r . %d in (node_modules) do @if exist %d (echo "%d" && rd "%d" /s /q)""

bash를 제거하는 node_modules 모든 모제됩니다가 됩니다.node_modules찾은 경로를 인쇄하는 동안 디렉터리가 현재 작업 디렉터리에서 재귀적으로 생성됩니다.

당신은 당신의 어딘가에 넣기만 하면 됩니다.$PATH

rmnodemodules(){

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

}

Bash에서는 다음을 사용할 수 있습니다.

rm -rf node_modules **/node_modules

삭제하는 대신 이동하려면 다음을 수행합니다.

find . -name 'node_modules' -type d -prune -exec mkdir -p ./another/dir/{} \; -exec mv -i {} ./NODE_MODULES/{} \;

이렇게 하면 디렉터리 구조가 유지됩니다.

서버의 모든 node_modules 폴더를 제거하는 간단한 방법은 다음을 실행하는 것입니다(많은 공간을 줄일 수 있음).

# For Ubuntu
sudo find / -not -path "/usr/lib/*" -name 'node_modules' -type d -prune -exec rm -rf '{}' +

# For macOS
sudo find / -not -path "/usr/local/*" -name 'node_modules' -type d -prune -exec rm -rf '{}' +

서 우리는 서기제합야니다해외를 제외해야 합니다./usr/lib/*만약 당신이 그렇게 하지 않는다면, 그것은 당신을 삭제할 것이기 때문입니다.npm그리고 당신은 그것을 다시 설치해야 합니다 :)

참고: 승인된 답변보다 개선된 내용입니다. 먼저 승인된 답변을 사용하십시오.

만약 당신이 너무 지루하다면 계속 읽으세요.

기본적으로 이 명령은 99%의 경우에 잘 작동합니다.

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

명령줄을 통해 파일을 삭제하는 것이 Finder를 통해 폴더를 삭제하는 것보다 더 깁니다(Finder에서 삭제할 때 폴더를 ~/로 이동합니다).휴지통 디렉터리).

node_modules를 ~/로 이동하려면 다음과 같이 하십시오.휴지통 폴더를 사용해 보십시오.

find . -name 'node_modules' -type d -prune -exec sh -c 'mv -f "$1" "$(dirname "$1")/$(basename $(dirname "$1"))_$(basename "$1")" && mv "$(dirname "$1")/$(basename $(dirname "$1"))_$(basename "$1")" ~/.Trash/' sh {} \;

아시다시피 두 부분으로 구성되어 있습니다.

  1. find . -name 'node_modules' -type d -prune모든 node_dirs 찾기
  2. -exec sh -c 'mv -f "$1" "$(dirname "$1")/$(basename $(dirname "$1"))_$(basename "$1")" && mv "$(dirname "$1")/$(basename $(dirname "$1"))_$(basename "$1")" ~/.Trash/' sh {} \;node_module을 부모 폴더 이름으로 접두사를 붙여 이름을 바꾸고 휴지통으로 이동합니다.

전에

~/Development/angular-projects
 ┣ project1
 ┣ project2
 ┗ project3

명령 실행 후

~/.Trash
 ┣ ~project1_node_modules
 ┣ ~project2_node_modules
 ┗ ~project3_node_modules

그러면 쓰레기를 비우십시오. 여기에 이미지 설명 입력

또는 빈 휴지통 기능 켜기 여기에 이미지 설명 입력

여러 프로젝트에서 node_modules 폴더를 삭제하는 Python 스크립트입니다.여러 프로젝트로 구성된 프로젝트 폴더에 저장하고 실행하기만 하면 됩니다.

import os
import shutil
dirname = '/root/Desktop/proj' #Your Full Path of Projects Folder
dirfiles = os.listdir(dirname)

fullpaths = map(lambda name: os.path.join(dirname, name), dirfiles)

dirs = []

for file in fullpaths:
    if os.path.isdir(file): dirs.append(file)

for i in dirs:
    dirfiles1 = os.listdir(i)
    fullpaths1 = map(lambda name: os.path.join(i, name), dirfiles1)
    dirs1 = []
    for file in fullpaths1:
        if os.path.isdir(file):
            dirs1.append(file)
            if(file[-12:]=='node_modules'):
                shutil.rmtree(file)
                print(file)
    

파티에 꽤 늦었지만, 이것은 (적어도 Linux와 MacOS에서는) 모든 node_modules를 재귀적으로 제거하지 않습니까?

rm -rf node_modules */node_modules */*/node_modules

언급URL : https://stackoverflow.com/questions/42950501/delete-node-modules-folder-recursively-from-a-specified-path-using-command-line

반응형