shell中判读文件存在的方法

时间:2023-03-09 08:08:15
shell中判读文件存在的方法

单个文件较简单

if [[ -f filename ]]; then
echo exist
fi

文件存在并且有内容

if [[ -s filename ]]; then
echo exist
fi

带匹配符的多个文件时

if ls /path/*.log &> /dev/null; then
echo exist
fi

或者需要知道有多少个文件存在时

file_count=`find /path -name *.log | wc -l`
if [[ file_count > ]]; then
echo exits $file_count
fi