【问题标题】:Delete node_modules folder recursively from a specified path using command line使用命令行从指定路径递归删除 node_modules 文件夹
【发布时间】:2017-08-14 11:59:23
【问题描述】:

我有多个 npm 项目保存在本地目录中。现在我想在没有node_modules 文件夹的情况下备份我的项目,因为它占用了大量空间,并且可以随时使用npm install 检索。

所以,我需要一个解决方案,使用命令行界面从指定路径递归删除所有 node_modules 文件夹。 任何建议/帮助都非常值得赞赏。

【问题讨论】:

    标签: bash macos command-line-interface node-modules


    【解决方案1】:

    打印出要删除的目录列表:

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

    从当前工作目录中删除目录:

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

    您也可以使用trash (brew install trash) 进行分阶段删除:

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

    【讨论】:

    • -prune 是一个重要的优化。它会发现不递归到node_module 目录(寻找嵌套的node_modules)
    • 对于一个项目,我在很多“内部”node_modules 文件夹中得到/node_modules/gulp-server-livereload/node_modules: Directory not empty。如何解决这个问题?
    • 这就像,我是认真的,我第 20 次在这个答案中复制这段代码...... xD
    • '{}' +是什么意思?
    • {} 是一个占位符,find 用它找到的文件路径替换。 + 告诉 find 将所有文件路径附加到单个命令,而不是为每个命令运行 rm
    【解决方案2】:

    试试 https://github.com/voidcosmos/npkill

    npx npkill
    

    它会找到所有 node_modules 并让你删除它们。

    【讨论】:

    • 比上面更好的选择,因为我通常有 2-3 个项目,我想保持 node_modules 完整
    • 这应该是置顶评论!
    • 我更喜欢核对所有文件夹并根据需要重新安装它们。谁有时间手动挑选?
    【解决方案3】:

    改进接受的答案,

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

    我发现该命令将运行很长时间来获取所有文件夹,然后运行删除命令,以使命令可恢复我建议使用 \; 并查看正在运行的命令的进度使用 @987654325 @查看正在删除的目录。

    注意:你必须先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 '{}' \;
    

    编辑:

    对于喜欢交互方式的人,请参阅@jeckep 答案,在您希望修剪的目录中运行它。

    npx npkill
    

    【讨论】:

    • 对已接受的答案@Sidharth 进行了很好的改进,非常感谢
    • 干得好@Sidharth
    • 供(我自己的)将来参考 - 我想删除 node_modules 的内容,但保留目录(稍后将它们添加到 Dropbox 忽略列表)。这对我有用:find . -name 'node_modules' -type d -prune -print -exec bash -c 'rm -rf "$0"/* "$0"/..?* "$0"/.[!.]*' {} \; 通配符用于匹配隐藏文件和文件夹(以 . 开头),根据 unix.stackexchange.com/a/77313/486273
    • 改进这个答案,使用 luminS (github.com/wchang22/LuminS) – find . -name 'node_modules' -type d -prune -exec lms rm '{}' +
    【解决方案4】:

    我遇到了这个解决方案,

    • 首先使用find找到文件夹并指定文件夹名称。
    • 递归执行删除命令-exec rm -rf '{}' +

    运行以下命令递归删除文件夹

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

    【讨论】:

      【解决方案5】:

      bash 删除 node_modules 的函数。它将递归地从当前工作目录中删除所有node_modules 目录, 打印找到的路径时。

      您只需在$PATH 中的某个位置输入

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

      【讨论】:

        【解决方案6】:

        在 Windows 上,我使用以下 .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)"
        
        【解决方案7】:

        操作系统:Ubuntu

        删除服务器中所有node_modules 的简单技巧(可以减少大量空间)是运行:

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

        这里我们需要排除/usr/lib/*,因为如果你不这样做,它会删除你的npm,你需要重新安装它:)

        【讨论】:

          【解决方案8】:

          从多个项目中删除 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 程序甚至可能是 windows 的一行中完成的事情来说似乎有点矫枉过正
          【解决方案9】:

          如果你想移动而不是删除它:

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

          这将保持目录结构。

          【讨论】:

            【解决方案10】:

            这真的很好用

            find . -name "node_modules" -exec rm -rf '{}' +
            

            【讨论】:

            • 这是以前答案的重复,只是没有解释。
            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2010-10-06
            • 2016-06-08
            • 2013-08-27
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多