Shell脚本检查文件是否存在?

tam*_*ani 5 shell-script timestamps files

我写了一个简单的备份脚本,它在远程服务器上备份我的东西,一切都很好,但我想要某种报告,比如“备份成功与否”。

这是我的脚本

# backup CHECK
date1=`date +"%d.%m.%y - %H.%M"`
host=`hostname`
twdate=`date +"%d.%m"`
performtw=`ls -la|grep "tw"|grep "$twdate" > tmp1.txt`
echo $performtw
if
cat tmp1.txt|grep "tw"
then
echo backup successfull
printf "tw backup success!" | mail -s "tw backup check $date1 repor$
rm tmp1.txt
else
echo backup failure
printf "sitename backup failure!" | mail -s "site backup check $date1 repor$
rm tmp1.txt
fi
exit
Run Code Online (Sandbox Code Playgroud)

但这并不能很好地工作,我问你是否有一些更简单、更强大的方法来做到这一点?基本上它只需要检查文件是否存在,名称以 xyz 开头,并且是在日期 xyz 创建的。

小智 8

您可以使用一个不错的 bash 条件执行来查看文件是否存在...

[ -f tmp1.txt ] && echo "Found" || echo "Not found"
Run Code Online (Sandbox Code Playgroud)

或者,您可以使用带有-newer标志的 find 命令...

find /some/dir -type f -newer $startdate -not -newer $enddate
Run Code Online (Sandbox Code Playgroud)