将目录下的所有子目录和文件全部上传到ftp上

本文介绍了一个使用bash脚本实现的FTP文件管理任务,包括定时检查本地目录,将文件压缩并上传到远程FTP服务器,以及定期清理服务器上的文件。通过'xunhuan'函数实现周期性操作,适用于日常运维或定期备份需求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#!/bin/bash
dir=/home/hzw ##ftp里的目录
password=test #密码
user=test001 #账号
ip=192.168.1.252 #ftp的ip
index=/drives/c/Users/admin/Desktop/home #本地目录
date=20s #定时
##port:端口号因为没用到所以没写
 ##本地的目录
stamp(){
cd $index
for file in $(ls *)
do
echo $file
done
DIRECTORY=$1
if [ "`ls -A $DIRECTORY`" = "" ]; then
  echo "目录为空"
else
main
fi

}
putfile(){
ip=192.168.1.252
d="/drives/c/Users/admin/Desktop/home/"
if [ -d "$d" ]; then
      tar -czvf dirname.tar.gz *
fi
ftp -v -n $ip << EOF
user $user  $password
binary
hash
cd $dir
lcd $index
prompt
mput dirname.tar.gz
bye
EOF
}
outfile(){
ftp -v -n <<EOF
  open $ip
  user $user $password
cd $dir
prompt off
mdelete *
bye
EOF
}
rmfile(){
cd $index
cd ..
rm -r home 
mkdir home
}
main(){
outfile
putfile
rmfile

}
xunhuan(){
while true
do
stamp
sleep $date
done
}
xunhuan
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值