Need help in deleting old files from a ftp server


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help in deleting old files from a ftp server
# 1  
Old 01-13-2009
Question Need help in deleting old files from a ftp server

Hi,

I'm quite new to unix and perl scripting and need to write a script to delete files older than 7 days from a remote ftp server. Unix or Perl script would do...

I wrote the following code:

Code:
#!/usr/local/bin/perl
use File::Basename;
use Net::FTP;
#use Net::FTP::File;
my $verbose = 0;
my $proxy = @ARGV[0];
my $host  = @ARGV[1];
my $ftpuser = @ARGV[2];
my $ftppass = @ARGV[3];
my $remoteDir = @ARGV[4];
my $filetoDelete = @ARGV[5];
my $ftp=Net::FTP->new($host,
           Debug => 0,
           Passive => 0,
           Hash => ($verbose ? \*STDOUT : undef),
           FirewallType => 1,
           Firewall => $proxy,
   ) or exit(1);
   $ftp->login($ftpuser,$ftppass) or exit(2);
chomp($remoteDir);
if ( $remoteDir ne "/" )
{
   $ftp->cwd($remoteDir) or exit(3);
   print $remoteDir;
}
@files=$ftp->ls or exit(4);
foreach(@files) {
     print "$_ \n";
     $ftp->delete($_) ;
}
$ftp->quit;
exit(0);

I'm not able to find a way to identify files of pattern aaa* that are older than 7 days. Unix and Perl gurus, please help...

regards,
Arun

Last edited by rbatte1; 06-15-2017 at 09:32 AM.. Reason: Added CODE tags
# 2  
Old 01-14-2009
Guys,

Any help would be greatly appreciated... I'm totally stuck with this... Any other approaches to achieve the same are also welcome...

Thanks in advance,
Arun.
# 3  
Old 01-15-2009
Hi,

Even I am very new to scripting however i tried this in my local network and its working.

Iam not sure whether it will work with your requirement but it will give you an idea.

One of our application writes logs to the system as shown below:

Code:
bash-2.03$ pwd
/var/log/Application
bash-2.03$
bash-2.03$ ls -lrt Application*
-rw-r--r--   1 root     other    34752530 Jan  8 23:59 Application.log.Thu
-rw-r--r--   1 root     other    5179052 Jan  9 23:59 Application.log.Fri
-rw-r--r--   1 root     other    8420980 Jan 10 23:59 Application.log.Sat
-rw-r--r--   1 root     other    2993389 Jan 11 23:59 Application.log.Sun
-rw-r--r--   1 root     other    13932907 Jan 12 23:59 Application.log.Mon
-rw-r--r--   1 root     other    15685534 Jan 13 23:59 Application.log.Tue
-rw-r--r--   1 root     other    360387803 Jan 14 23:59 Application.log.Wed
bash-2.03$

Now in order to zip these files or delete it i set the following script as cron job at every 00:00 hrs.

Code:
#!/usr/bin/sh
INSTALL_DIR=/var/log/Application
Curr_Dir=`pwd`

cd $INSTALL_DIR
i=`ls |grep "\.log.[a-zA-Z]\{3,3\}$ |wc -l` //here iam grepping any char and minimum of 3 char as Mon, Tue etc..

	if [ 0 -ne $i]
	then
		rm $INSTALL_DIR/*.log.???
		#gzip -f9 $INSTALL_DIR/*.log.???
	fi

Thanks
-imas

Last edited by rbatte1; 06-15-2017 at 09:33 AM.. Reason: Added CODE tags
# 4  
Old 01-15-2009
Thanks for the reply, imas. This won't work for me as I'm trying to delete old files from a ftp server. ftp doesn't support grep and find command that's why I tried to write a PERL script for the same... But seems like PERL ftp also has these limitations...

Thanks anyways... Really appreciate it....

regards,
Arun.
# 5  
Old 01-15-2009
Why not write a cron job that removes the old files.. easy to do with a shell script.

Code:
find . -type f -mtime +7 | xargs rm -vf

Just an example of course.

Last edited by rbatte1; 06-15-2017 at 09:33 AM.. Reason: Added CODE tags
# 6  
Old 01-15-2009
Oh haha, I forgot to tell you.. that yeah perl does have a find command.

Perl is a great language (core), but mostly you have to use modules for a large amount of functionality.

Below are two links (even the servers I work on have these, so you should have them) to the Find::* type modules. The second one is pretty damn helpful. Smilie

File::Find - Traverse a directory tree. - search.cpan.org
File::Find::Rule - Alternative interface to File::Find - search.cpan.org
# 7  
Old 01-15-2009
Thanks for the reply, Rhije! But again I'm not sure if this would work for me... I need to do the following:
  1. Connect to a remote FTP server.
  2. Find all the files with names aaa* older than 7 days on the remote server.
  3. Delete these old files.

Earlier when I tried some other FILE features, what it does is gives me list of files on my local machine... I wanted to know if there is a way to get the list of old files from the remote ftp server...

Thanks again for replying...

regards,
Arun.

Last edited by rbatte1; 06-15-2017 at 09:34 AM.. Reason: Changed textual numbered list to be a formatted numbererd list with LIST=1 tags
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Deleting multiple files off an ftp server once they have been downloaded

Hello, I have a server that I have to ftp files off and they all start SGRD and are followed by 6 numbers. SGRD000001 SGRD000002 SGRD000003 The script I have will run every 10 mins to pick up files as new ones will be coming in all the time and what I want to do is delete the files I have... (7 Replies)
Discussion started by: sph90457
7 Replies

2. Shell Programming and Scripting

Deleting local server file from automated FTP script

Hi, I want to delete a file on the local server, while connected to remote server through FTP. I am using the below code for this $FTP_CMD -v -n $HOST <<*! >> $LOGFILE 2>&1 user $USER $PASSWORD cd $DIR ... (11 Replies)
Discussion started by: jhilmil
11 Replies

3. Solaris

FTP-ing files from Windows server to UNIX server

I need to transfer files from a Windows server to the Unix server and have to run some shell script on it to get the required output. Is it possible to transfer files from Windows server to unix server through any shell script? If so can you please help me with the details. Thanks in... (8 Replies)
Discussion started by: ssk250
8 Replies

4. Shell Programming and Scripting

FTP multiple files from one server to one server

Hi, I'm new to shell script..I have one requriement like - In one server have more than one files,I want to ftp those files to some otehr server.. Ex : test1.pdf test2.pdf Please suggest me how to do (3 Replies)
Discussion started by: venkaswa
3 Replies

5. Shell Programming and Scripting

Need help creating a script to FTP files to a server and then delete the files that were transfered.

I am trying to FTP files to a Windows server through my Linux machine. I have setup the file transfer with no problems but am having problem deleting those files from the Linux box. My current non-working solution is below. Any ideas, anyone?? :wall: Please be gentle, I'm fairly new to this... (4 Replies)
Discussion started by: jmalfhs
4 Replies

6. UNIX for Advanced & Expert Users

why file automatically deleting in ftp server

Iam putting file in ftp server. iam doing ftp to transfer a file to ftp server but after sometime(10 sec) the file is automatically deleting in the ftp. Can i know why this happens. When my friend ftp the file to the same server , the file is not deleting aftersometime... it is there. Can... (1 Reply)
Discussion started by: nani1984
1 Replies

7. Shell Programming and Scripting

script for to take files from FTP server to UNIX server and Unzipped that files

script for to take files from FTP server to UNIX server and Unzipped that files (1 Reply)
Discussion started by: sunilamarnadh
1 Replies

8. Filesystems, Disks and Memory

Not able to FTP the files to a FTP server

Hi , We are facing a weird problem in our project. we need to send some xml & audio files to a remote FTP server from a Linux box, we are doing this in Perl script using Net::FTP->. Issue here is.. when FTPed the files using Perl scripts, only empty files ( 0 byte) are getting created on the... (2 Replies)
Discussion started by: kishorepotta
2 Replies

9. Shell Programming and Scripting

FTP multiple files from remote server to local server

Hi, I am facing a weired problem in my FTP script. I want to transfer multiple files from remote server to local server everyday, using mget * in my script. I also, want to send an email for successful or failed FTP. My script works for file transfer, but it don't send any mail. There is... (2 Replies)
Discussion started by: berlin_germany
2 Replies

10. Shell Programming and Scripting

deleting multiple files through ftp

Hi, I have a situation where I need to delete multiple files from a folder once I connect to FTP server. I am using ftp script to get the files, number of files always vary from 1 to 100. once I get the files I need to delete all the files downloaded I am making a list of all the files... (4 Replies)
Discussion started by: mgirinath
4 Replies
Login or Register to Ask a Question