Skip to content

Instantly share code, notes, and snippets.

@mehunk
Created June 4, 2019 16:33
Show Gist options
  • Save mehunk/4caa299beaf05905cfe541ea8fd44b22 to your computer and use it in GitHub Desktop.
Save mehunk/4caa299beaf05905cfe541ea8fd44b22 to your computer and use it in GitHub Desktop.
遍历当前目录和子目录删除所有本地下载的 node_modules 依赖目录以节省磁盘空间。
#! /bin/bash
# 迭代遍历所有的文件夹,清理下载的 node_modules 文件夹
function read_dir(){
if [ -d $1"/node_modules" ]
then
rm -rf $1"/node_modules"
echo "删除依赖成功!"
fi
for file in `ls $1` # 注意此处这是两个反引号,表示运行系统命令
do
if [ -d $1"/"$file ] # 如果当前文件是目录。注意此处之间一定要加上空格,否则会报错
then
read_dir $1"/"$file # 再进入子目录迭代查找
fi
done
}
#读取第一个参数
read_dir $1
@bukuta
Copy link

bukuta commented Sep 27, 2019

find ./ -name node_modules |xargs rm -rf

@mehunk
Copy link
Author

mehunk commented Oct 13, 2019

find ./ -name node_modules |xargs rm -rf

Thank you! It really works and helps me!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment