Download files from a FTP server using Ruby.

I think the last time I had to use FTP was around 2005, when I was playing with PHP and “all the cool stuff”. But today I had to use Ruby to access a FTP server and automate some tasks.

Ruby makes this task really easy and fast. Here follow the snippet used to connect, iterate over a list of files and download them:

Net::FTP.open(HOST, USERNAME, PASSWORD) do |ftp|
  files = ftp.chdir(DIRECTORY)
  ftp.nlst('hd-*.jpg').each do |file_name|
     ftp.getbinaryfile(file_name, destination)
  end
end
      

If you enjoyed this, you might also like

Elvio Vicosa

I’m a full-stack software developer based in Berlin, Germany. I’ve spent the last 10+ years working in a wide range of software projects, from internal web dashboards to real-time applications used by millions of people.

More about me