LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-03-2007, 09:36 AM   #1
sang_froid
Member
 
Registered: Oct 2006
Posts: 179

Rep: Reputation: 15
automated ftp


Hi
I am trying to delete some specific files ( files other than created today) from the server on a cron basis. I wrote a small script, but I am stuck up in how to delete only specific files.


#!/usr/bin/expect -f

set IP [lindex $argv 0]
set timeout -1
spawn ftp $IP
expect ):
send "username\n"
expect word:
send "password\n"
expect ftp>
send "prompt\n"
expect ftp>
send "passive\n"
expect ftp>
send "cd logs/\n"
expect ftp>


After this I am stuck up. It is because I want to delete files with old dates ie, I don't want to delete the file dated today.


The files are like logfile-070501,logfile-070502 and so on..

I cannot run the command "rm logfile-0705*" ( as it will delete today's file too)

By the way, I have the list of files to be deleted in a separate file. I am wondering if by running any other additional script, we can delete selected files from the remote server .

I tried with ! command of ftp, but it runs the commands on local machine.

Your help will be highly appreciated.Thanks
 
Old 05-04-2007, 06:14 AM   #2
avallach
Member
 
Registered: Sep 2006
Location: Silesia
Distribution: Debian GNU/Linux 4.0, ArchLinux, OpenBSD
Posts: 190
Blog Entries: 2

Rep: Reputation: 31
Can't you use some tools like logrotate ?

Sorry for a bit offtopic
 
Old 05-04-2007, 02:10 PM   #3
sang_froid
Member
 
Registered: Oct 2006
Posts: 179

Original Poster
Rep: Reputation: 15
nopes...the thing is diff.

System logrotates it...but i need to ftp and del only selected old files..
 
Old 05-05-2007, 05:20 AM   #4
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
Best practice limits what commands an ftp access to a server can execute.
If you can:
Code:
usr/bin/ftp -n<<EOF
open servernode
USER usern passwd
cd /path/to/logs
ls -l *
bye 
EOF  > listingfile
and get meaningful results, you may be able to parse out files with older dates from the dta in listingfile.

Otherwise, you will have to try one of:
1. rsh over to the server to get a complete file listing and/or delete old files
2. set up a cron job on the server to clean up old files
 
Old 05-05-2007, 05:54 AM   #5
twantrd
Senior Member
 
Registered: Nov 2002
Location: CA
Distribution: redhat 7.3
Posts: 1,440

Rep: Reputation: 52
Yup, i would follow Jim's advice here. However, since you already have a list of files you want to remove, then just put in a for loop and you should be good to go.

-twantrd
 
Old 05-05-2007, 06:09 AM   #6
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
I think that a cron job on the server(s) to delete files based on the file modification date would be a better solution.
You could use the find command to match the date and pattern both:
find /path/to/logs/ -name "logfile-*" -ctime -1 -exec dir rm '{}' \;
Note: there is an option of find to align -ctime to midnight, but I'm too lazy to look it up.

Otherwise, since cron will be performed daily, you can use yesterdays date to construct a wildcard to use in the ftp script.

You could then use something like JimMcNamara's script:

Code:
YESTERDAY=$(date --date=yesterday +%y%m%d)
SERVERNODE=<ip-address>

ftp user:password@${SERVERNODE} << EOF
cd logs/
del log-${YESTERDAY}
bye
EOF >localcleaninglog-$(date +%y%m%d)

I've used the form "ftp user:password@ipaddress <ftpscript"
in a loop to automate the renaming of files in a large number of devices (5 racks of duets) after we found out that we needed
to change all of the filenames to include leading zeros. It worked from my laptop running linux, but didn't work in windows
or using cygwin in windows. Also, the --date=yesterday is a GNU option of date.

Last edited by jschiwal; 05-05-2007 at 06:21 AM.
 
Old 05-07-2007, 10:53 AM   #7
sang_froid
Member
 
Registered: Oct 2006
Posts: 179

Original Poster
Rep: Reputation: 15
At the first, ftp user : password@${SERVERNODE} << EOF didn't work

Quote:
Originally Posted by jschiwal
I think that a cron job on the server(s) to delete files based on the file modification date would be a better solution.
You could use the find command to match the date and pattern both:
find /path/to/logs/ -name "logfile-*" -ctime -1 -exec dir rm '{}' \;
Note: there is an option of find to align -ctime to midnight, but I'm too lazy to look it up.

Otherwise, since cron will be performed daily, you can use yesterdays date to construct a wildcard to use in the ftp script.

You could then use something like JimMcNamara's script:

Code:
YESTERDAY=$(date --date=yesterday +%y%m%d)
SERVERNODE=<ip-address>

ftp user:password@${SERVERNODE} << EOF
cd logs/
del log-${YESTERDAY}
bye
EOF >localcleaninglog-$(date +%y%m%d)

I've used the form "ftp user: password@ipaddress <ftpscript"
in a loop to automate the renaming of files in a large number of devices (5 racks of duets) after we found out that we needed
to change all of the filenames to include leading zeros. It worked from my laptop running linux, but didn't work in windows
or using cygwin in windows. Also, the --date=yesterday is a GNU option of date.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Automated FTP script to grab ONLY newest dated files kaiserbeto Linux - Newbie 4 10-20-2006 02:46 AM
automated ftp Anthraxnz Linux - Newbie 4 10-15-2005 09:36 PM
Automated FTP Upload Zingaro2002 Linux - Networking 7 05-05-2004 01:38 AM
Automated Backup via FTP KePSuX Linux - Newbie 3 02-11-2004 08:25 AM
automated FTP backup josephswagner Linux - Software 2 06-06-2003 04:42 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:27 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration