Solved

Script to download and delete all files

Posted on 2010-11-29
5
1,182 Views
Last Modified: 2013-12-26
I have been working on a script that will log in to a remote FTP directory, download all of the files in the directory, and then delete all of the remote files.

My plan was to use curl for this, but I have found that the file names are not consistent at all, so it's not too easy to assign a matching pattern.

Can anyone give me a hand with a simple script for this?
0
Comment
Question by:djudd77
  • 3
  • 2
5 Comments
 
LVL 12

Accepted Solution

by:
JESii earned 500 total points
ID: 34235601
Here is a Ruby script that will do what you want, I think...

You'll need to replace the ftp server name with your site and any directory name with what you want (if any), and then run it as a test.

Once you're satisfied with it, you can simply remove the "#" front the front of the "ftp.delete..." statement and you're good to go.

Mac has Ruby pre-installed so this should work from Terminal.
require 'net/ftp'

ftp = Net::FTP.new('ftp-server-name.com')
  ftp.login
  puts "Logged in..."
  puts "Directory set..."
  files = ftp.chdir('dir/name')
  files = ftp.nlst('*')
  puts "File list obtained... #{files}"
  files.each do |fname|
	puts "Downloading file #{fname}"
  	ftp.getbinaryfile(fname, fname)
	puts "Deleting file #{fname}"
	#ftp.delete(fname)
	end
  ftp.close

Open in new window

0
 
LVL 1

Author Comment

by:djudd77
ID: 34235627
I'm totally not familiar with Ruby. Is there a way to pass the username and password in this script?

I very much appreciate the help.
0
 
LVL 12

Expert Comment

by:JESii
ID: 34236328
Sure... Change line 4 to:
  ftp.login(user=ARGV[0], ARGV[1])
and then call it like so:
  ruby ftp_process.rb username password
0
 
LVL 1

Author Comment

by:djudd77
ID: 34236800
Very awesome, thank you for all your help.
0
 
LVL 12

Expert Comment

by:JESii
ID: 34238338
You're welcome; thank you for the points.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Information security is a multi-billion dollar industry. Just as lucrative is the black market industry which trades stolen identities, credit card numbers and software exploits all over the world. Nothing is hack-proof. The best one can do is make …
Set up iPhone and iPad email signatures to always send in high-quality HTML with this step-by step guide.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now