【问题标题】:Download a fix number of directories from ftp server从 ftp 服务器下载固定数量的目录
【发布时间】:2016-06-20 12:50:01
【问题描述】:

我有一个包含数千个目录的 FTP 服务器。我想要做的是使用 shell 脚本下载特定数量的它们(例如,500 个目录)。我怎样才能做到这一点?我用 -Q 命令尝试了 wget。例如,“wget -Q25MB”,它给了我 25MB 的数据。问题是每个文件夹都有不同的大小。因此,使用此命令将在获取特定文件夹的过程中停止下载。

【问题讨论】:

  • 我们需要有关 FTP 内目录结构的更多信息。另外,请提供清楚的示例,说明您为我们解决最终错误所做的努力。
  • 我的 ftp 服务器中有数百个目录。每个目录都有许多图像文件。我想要做的是检索指定数量的它们,比如每晚 1,000,000 个中的 500 个。为了清楚起见,这是我目前拥有的文件夹的模式: 610186098 02/23/2016 12:47:00 AM 610187047 05/24/2016 07:57:00 PM 610187071 01/20/2016 04:02: 00 AM 610188005 02/23/2016 12:47:00 AM 610188075 04/29/2016 09:33:00 PM 610188078 05/24/2016 07:57:00 PM ...
  • P.S.文件夹名称是学生的身份证号码,其内容包括其教育文件的扫描件。另外,每天晚上都会有新的文件夹添加到ftp服务器的文件夹列表中,所以每天服务器中的文件夹数量是不一样的。
  • P.S.我想在晚上 10 点下载 500 个目录及其包含的内容,并从 ftp 服务器中删除成功传输的目录。在目录中间停止下载是不可接受的。
  • 这可能是一种幼稚的方法,但我会这样做:1)下载 ftp 中的文件夹列表(或至少是列表的开头),然后 2)为每个列出的文件夹,下载它,然后删除它。当达到 500 次删除时,退出。删除之前不要忘记检查每个文件夹的下载是否完成...

标签: shell download ftp


【解决方案1】:

假设下载中断时 wget 返回错误:

#!/bin/bash
to_del= # empty to_del in case you want to copy-paste this to a terminal instead of using a file
username=blablabla
password=blablabla
server=blablabla
printf -v today '%(%Y_%m_%d)T'
# Get the 500 first directory names to download
ftp -n "$server" << EOF | grep -v '^\.\.\?$' | head -n 502 > "to_download_$today.txt"
  user $username $password
  ls
  bye
EOF
# Then, you can download each folder one by one:
while read -r dir; do
  if [[ -e $dir ]]; then
    echo >&2 "WARNING: '$dir' already exists!"
    continue # We don't download or remove it. Manual action needed
  fi
  if wget "$username:$password@$server/$dir"; then
    to_del+=("$dir")
  else
    # A directory was not successfully downloaded, we delete the temporary files
    echo >&2 "WARNING: '$dir' download failed, skipping..."
    rm -rf "$dir"
  fi
done < "to_download_$today.txt"
# Now, delete the successfully downloaded folders using a single FTP connection
{
   printf 'user %s %s\n' "$username" "$password"
   for dir in "${to_del[@]}"; do
     printf 'del %s\n' "$dir"
   done
   printf 'bye\n'
} | ftp -i -n "$server"

【讨论】:

    猜你喜欢
    • 2014-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多