Bash脚本,只有在以前没有下载它时才从FTP下载文件 [英] Bash Script that downloads file from FTP only if it hasn't downloaded it previously
问题描述
我目前有一个bash脚本从FTP站点下载文件。我想修改脚本以保留它下载的每个文件的清单,并只下载不在清单中的任何新文件。有没有办法在bash中做到这一点,或者我推动什么bash实际上可以做的限制? 我的建议会要抓住您可能要下载的文件列表,将其与您的清单进行比较,然后下载不在清单中的文件。像这样的东西应该可以工作:
ftp username @ hostname<< EOF
ls * filelist.txt
EOF
cat filelist.txt | awk'{print $ 9}'| tail -n + 3> filenames.txt
,而IFS =读取-r文件
做
if [[! $(grep$ filemanifest.txt)]];那么
echo$文件不在清单中
fi
完成< filenames.txt
当然,您需要更改 filelist .txt
和 filenames.txt
来包含适当的路径。另外,更改 echo
命令以将文件名添加到另一个文件或变量,您将用它来执行实际下载。
下面是对发生了什么事情的解释:
前三行使用 ftp
来获取列表(您需要更改 *
指向正确的目录。)
cat
line分析ftp ls
命令的输出,以抓取文件名部分并将其放入另一个文件中。
然后,while循环遍历文件列表并使用grep来查看文件名是否存在于清单文件中。如果没有,您可以下载并将其添加到清单中。
让我知道您是否有任何问题。
希望这有助于!
I currently have a bash script that downloads files from an FTP Site. I would like to modify the script to keep a manifest of every file it downloads and only download any new files that are not in the manifest. Is there a way to do this in bash or am I pushing the limitations of what bash can actually do?
My suggestion would be to go grab the list of files that you might want to download, compare that to your manifest, then download the ones not in the manifest. Something like this should work:
ftp username@hostname <<EOF
ls * filelist.txt
EOF
cat filelist.txt | awk '{print $9}' | tail -n +3 > filenames.txt
while IFS= read -r file
do
if [[ ! $(grep "$file" manifest.txt) ]]; then
echo "$file is not in manifest"
fi
done < "filenames.txt"
Of course, you'll need to change filelist.txt
and filenames.txt
to include an appropriate path. Also, change the echo
command to add the filename to another file or variable that you'll use to do the actual download.
Here's an explanation of what's going on:
The first three lines use ftp
to grab a list of files (You'll want to change the *
to point to the correct directory.)
The cat
line parses the output of the ftp ls
command to grab just the filename part and put it into another file.
Then the while loop goes through the list of files and uses grep to see if the filename exists in the manifest file. If it doesn't, you can then download it and add it to the manifest.
Let me know if you have any questions.
Hope this helps!
这篇关于Bash脚本,只有在以前没有下载它时才从FTP下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!