linux 中如何判断指定目录下是否有文件(包括隐藏文件和符号链接)呢?
脚本名:decide_blank_folder.sh
脚本内容:
- #!/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` 中的感叹号表示什么?
表示否定,即搜索除文件夹之外的所有内容,搜索时排出文件夹。