判断指定目录是否有文件

判断指定目录是否有文件

linux 中如何判断指定目录下是否有文件(包括隐藏文件和符号链接)呢?

脚本名:decide_blank_folder.sh

脚本内容:

Shell代码  

#!/bin/sh  

# whether the specified folder has files,including symbolic file and hidden file  

is_blank_dir_yes=2  

is_blank_dir_no=5  

isHasFileInDir()  

{  

        result3=`find "$1" ! -type d`  

        if [ x"$result3" == x"" ];then  

                return 1  

                # has no file  

        else  

        echo "$this_dir"  

                return 3  

                # has file(s)  

        fi  

}  

  

is_blank_folder_compl()  

{  

        local fold_name="$1"  

        ls "$fold_name" >/dev/null 2>&1  

        if [ $? -ne 0 ];then  

                return 4  

                # has no this folder  

        fi  

        local result=`ls -A "$fold_name"`  

        if [ x"$result" == x"" ];then  

                return $is_blank_dir_yes  

                # is blank  

         else  

                for i in `find "$fold_name" ! -type d`;do  

                        return $is_blank_dir_no  

                done  

                #init_bool=5  

  

                return $is_blank_dir_yes  

         fi  

}  

  

if [ -z "$1"  ];then  

    echo "no argument";  

        exit 255  

fi  

  

is_blank_folder_compl "$1"  

retval=$?  

echo "return value: $retval"  

if [ $retval -eq $is_blank_dir_yes ];then  

        echo "has no file(s)"  

else  

        if [ $retval -eq $is_blank_dir_no ];then  

                echo "has file......."  

        fi  

fi  

测试如下:

[root@localhost test]# ./decide_blank_folder.sh abc/

return value: 2

has no file(s)

知识点补充:

(1)如何查看隐藏文件?

使用ls -A

(2)`find "$fold_name" ! -type d` 中的感叹号表示什么?

表示否定,即搜索除文件夹之外的所有内容,搜索时排出文件夹。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值