linux查找空目录并自动创建.gitkeep文件

      在git提交代码时,经常遇到空目录无法提交的问题,比较常规的做法是在空目录下创建一个.gitkeep隐藏文件,但是当工程比较大的时候,一个个去找空目录实在是麻烦。

这就需要一个脚本来解决这个问题,如下为自己写的递归查询空目录创建.gitkeep文件的脚本,可自行替换命令做其他批量操作(如常用的删除空目录等)。

#!/bin/bash

#判断文件夹是否为空
is_empty_dir(){
    return `ls -A $1 | wc -w`
}

#递归检查空目录,并创建.gitkeep文件
check_empty_dir()
{
    #echo "Enter --> $1"
    for file in `ls $1`
    do
        if [ -d "$1/$file" ];then
            if is_empty_dir $1"/"$file
            then
                echo "$1/$file is empty,touch $1/$file/.gitkeep"
                touch "$1/$file/.gitkeep"
            else
                #echo "$1/$file is not empty"
                check_empty_dir "$1/$file"
            fi
        fi
    done
}

check_empty_dir $1

用法:

./check.sh <目录名>  #目录名后不带/符号

测试目录结构如下:

测试结果: 

猜你喜欢

转载自blog.csdn.net/fangye945a/article/details/110071075
今日推荐