使用命令行从指定路径递归删除node_modules文件夹

328

我有多个npm项目保存在本地目录中。现在我想备份我的项目,但不包括node_modules文件夹,因为它占用了大量空间,并且可以使用npm install随时恢复。

那么,如何使用命令行界面从指定路径递归删除所有node_modules文件夹呢?

13个回答

2

虽然来晚了,但这是否会在 Linux 和 MacOS 上递归删除所有 node_modules?

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

没起作用 - Normal
尝试执行 rm -rf node_modules */node_modules **/node_modules - zaplec

0

Python脚本用于从多个项目中删除node_modules文件夹。只需将其放置在包含多个项目的项目文件夹中并运行即可。

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)
    

3
似乎有些过度了,因为这个任务可以在基本的Linux程序甚至Windows中用一行代码完成。 - Emobe

0
进一步解释之前的回答: 这是一个脚本,它会自动删除所有的 */node_modules 和 */dist 文件夹,以清除任何已安装的库。
#!/bin/bash
# Script to clean all:
#   - */node_modules
#   - */dist

scriptDir=$(cd -P -- "$(dirname -- "$0")" && pwd -P)
cd "$scriptDir"

echo "Removing node_modules..."
args=(
    . \( -name 'dist' -o -name 'node_modules' \)    # find all folders with name dist or node_modules
    -prune                                          # exclude recursive search in subdirectories
    -print                                          # output results
    -exec rm -rf '{}' +                             # delete all folders
)
find "${args[@]}"
echo "Done."

并没有真正改进十年前已经给出的答案。请在回答旧问题时确保带来一些真正新颖的东西;例如一些在当时不存在的新方法。 - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接