使用 Linux 从 FTP 位置获取文件名中包含前一周日期的所有文件
Get all the files from a FTP location with previous week's dates in the file names using Linux
我有一个奇怪的要求,我必须从 FTP(比方说 FTP1)位置获取文件并将其放在我当前的 FTP(比方说 FTP2)位置。问题是,这些是每日文件(在模式 Sales_YYYYMMDD_report.csv 中)并且每天都放在 FTP1 上,我的进程通常在星期一运行(例如 2013 年 9 月 9 日),它必须使用前一周开始的文件从星期日(例如 2013 年 9 月 1 日)到星期六(例如 2013 年 9 月 7 日)将它们放在 FTP2 位置,然后运行 ??Informatica 进程。例如,如果我在 2013 年 9 月 9 日星期一运行该进程,我必须从 FTP1 中提取所有文件名,例如
Sunday file --> Sales_20130901_report.csv
Monday file --> Sales_20130902_report.csv
Tuesday file --> Sales_20130903_report.csv
Wednesday file --> Sales_20130904_report.csv
Thursday file --> Sales_20130905_report.csv
Friday file --> Sales_20130906_report.csv
Saturday file --> Sales_20130907_report.csv#!/bin/bash
# assuming this is run from the directory where you want the files
# to end up
function getIt {
echo"ftp-ing" $1
# here comes the heart of the ftp session
ftp -inv << _EOF_
open home.machine.com
user myname mypassword
cd /the/path/where/the/file/lives
get $1
bye
_EOF_
}
# generate the seven file names for the previous seven days:
for d in {1..7}
do
theCmd="/bin/date -v -"$d"d +%Y%m%d"
theDate=`$theCmd`
fileName="Sales_"$theDate"_report.csv"
getIt $fileName
done
如何在 shell 脚本中实现这一点?我知道从另一个 FTP 获取文件的部分,但我不确定如何获取这 7 个文件。
P.S:我不能使用文件创建/最后修改的时间戳来获取文件。无论创建的时间戳和运行 Informatica 进程的日期如何,我都必须获取文件名中包含上周日期的文件,并将其放入我的 FTP2 位置,然后继续处理它们。
请帮助...
以下脚本应该包含您需要的所有元素:
Sunday file --> Sales_20130901_report.csv
Monday file --> Sales_20130902_report.csv
Tuesday file --> Sales_20130903_report.csv
Wednesday file --> Sales_20130904_report.csv
Thursday file --> Sales_20130905_report.csv
Friday file --> Sales_20130906_report.csv
Saturday file --> Sales_20130907_report.csv#!/bin/bash
# assuming this is run from the directory where you want the files
# to end up
function getIt {
echo"ftp-ing" $1
# here comes the heart of the ftp session
ftp -inv << _EOF_
open home.machine.com
user myname mypassword
cd /the/path/where/the/file/lives
get $1
bye
_EOF_
}
# generate the seven file names for the previous seven days:
for d in {1..7}
do
theCmd="/bin/date -v -"$d"d +%Y%m%d"
theDate=`$theCmd`
fileName="Sales_"$theDate"_report.csv"
getIt $fileName
done
它应该是不言自明的:但要特别注意 heredoc 的结尾("封装的 ftp 脚本",如果你喜欢的话)必须在行的开头,没有空格在它之前,之后没有空白。另外 - 根据这台机器上安全性的重要性,您可能希望对密码做一些不同的事情;也许你甚至想使用除"vanilla" ftp 以外的东西。但我认为这应该让你朝着正确的方向前进。
测试到实际的 ftp 位本身...当我注释掉脚本时正确调用 getIt() 七次,字符串代表您指定的文件名。显然我不能轻易测试 ftp...
还要注意,这只会复制到您启动脚本的目录;如果您需要它在其他地方结束,您可能需要第二个 ftp 脚本。不过,我相信你可以弄清楚。
您可以在 linux 中使用以下命令(在 Cent OS 6 上测试),将 -1 天更改为适当的日期
昨天="date +%Y%m%d --date="-1 day""
更多参考 = http://blog.midnightmonk.com/85/bash/bash-date-manipulation.shtml