shell 判断目录下是否有某文件

2025-04-26 08:08:05
推荐回答(4个)
回答1:

#!/bin/bash

if [ ! -f "$1" ];then
echo "当前目录不存在文件: $1"
else
echo "当前目录存在文件: $1"
fi
运行方法: ./test.sh filename (test.sh为脚本名称,filename为文件名称)
查找目录为当前目录;

回答2:

if [ -f file ];then
    echo "this file is exist."
fi

回答3:

find ./ -name filename

回答4:

1、find $dir -type f|wc -l可以计算文件数量
2、实例
#!/bin/sh
dir=/opt/LNMP1
num=`find $dir -type f|wc -l`
if(( $num > 0 ));then
echo "$dir file number is:$num"
else
echo "$dir nofile"
fi