BASH递归查找文件名并复制到ftp

时间:2011-06-22 19:12:48

标签: bash find

我正在研究一个Bash脚本(见下文),该脚本以递归方式在SAN上的目录中搜索特定文件名超过4小时的文件。然后将所有这些文件复制到特定的FTP位置并通过电子邮件说明副本已完成。该脚本工作正常,只是它只复制顶级目录中的文件。我在较低目录上得到的错误是:

remote: -v
ftp: local: -v: No such file or directory
local: ./Test01/test02/folder02_01_1200_m30.mp4 remote: ./Test01/test02/folder02_01_1200_m30.mp4
229 Entering Extended Passive Mode (|||45127|)
550 ./Test01/test02/folder02_01_1200_m30.mp4: File does not exist. (2)
221 Goodbye.

这是脚本

#!/bin/bash
#The location from where the script should search
GSPORIGIN='/Volumes/folder01/folder02'

#File Names to be moved
FILE1='*1200_m30.mp4'

#FTP Details
HOST='xxxx.upload.com'
USER='xxxxxxx'
PASSWD='xxxxxxxxxxxx'
#the destination directory on the FTP
DESTDIR="/8619/_!/TEST"


# Go to the location from where the search should start 
cd $GSPORIGIN
for file in `find . -type f -name "*1200_m30.mp4" -mmin -240`
do 
echo $file
if [ -f $file ] ; then
ftp -n -v $HOST << EOT
ascii
user $USER $PASSWD
prompt
cd $DESTDIR
mput -v $file
EOT
echo "$file has been copied to FTP" | mail -s "$file has been copied to FTP in Directory $DESTDIR"  xxx.xxx@xxx.com; 
else exit 1
fi
done

2 个答案:

答案 0 :(得分:2)

要执行您正在执行的操作,您必须在目标FTP上重新创建目录。 使用basename / dirname命令和mkdir命令,如下所示:

for file in `find . -type f -name "*1200_m30.mp4" -mmin -240`
do 
echo $file
if [ -f $file ] ; then

destdirname=`dirname "$file"`

ftp -n -v $HOST << EOT
ascii
user $USER $PASSWD
prompt
cd $DESTDIR
mkdir $destdirname 
mput -v $file
EOT
echo "$file has been copied to FTP" | mail -s "$file has been copied to FTP in Directory $DESTDIR"  xxx.xxx@xxx.com; 
else exit 1
fi

答案 1 :(得分:2)

要在嵌套目录中复制多个文件:我建议您查看rsync utility为您完成这项工作。

rsync将在需要时创建所有远程目录,即使经常运行,它也会使文件保持完全同步。

相关问题