vscode - latex workshop 插件会在文件目录下生成非常多的空文件夹,在作者修正这一bug前,为了保证目录的清爽,在ChatGPT的帮助下写了一个删除空文件夹的zsh脚本。

1
2
3
4
5
6
7
8
9
10
11
12
13
#!/bin/zsh

# 删除当前目录及所有子目录下的空文件夹
function delete_empty_folders {
for dir in **/*(/); do
if [[ $(ls -A "$dir" | wc -l) -eq 0 ]]; then
echo "Deleting empty folder: $dir"
rmdir "$dir"
fi
done
}

delete_empty_folders

为了再每次 git commit 之前自动执行该脚本,可以在 .git/hooks/pre-commit 中添加代码

1
2
3
4
5
6
7
#!/bin/zsh

# Run the script to delete empty folders
./deleteEmptyFolders.zsh

# Exit with 0 to allow the commit to proceed
exit 0

最后,需要给这两个文件执行和读写的权限,执行如下代码。

1
2
chmod +xrw deleteEmptyFolders.zsh
chmod +xrw .git/hooks/pre-commit

即可。