Delete old files from FTP Storage


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Delete old files from FTP Storage
# 1  
Old 01-20-2015
Debian Delete old files from FTP Storage

hello,
we try to delete old files from ftp server. we are using that script.
Code:
#!/bin/bash
# get a list of files and dates from ftp and remove files older than ndays
ftpsite="HOST"
ftpuser="USER"
ftppass="PASSWORD"
putdir="PATH"

# age of files they should delete
ndays=1


# work out our cutoff date
MM=`date --date="$ndays days ago" +%b`
DD=`date --date="$ndays days ago" +%d`


echo removing files older than $MM $DD

# get directory listing from remote source
listing=`ftp -i -n $ftpsite <<EOMYF 
user $ftpuser $ftppass
binary
cd $putdir
ls
quit
EOMYF
`
lista=( $listing )

# loop over our files
for ((FNO=0; FNO<${#lista[@]}; FNO+=9));do
  # month (element 5), day (element 6) and filename (element 8)
  #echo Date ${lista[`expr $FNO+5`]} ${lista[`expr $FNO+6`]}          File: ${lista[`expr $FNO+8`]}

  # check the date stamp
  if [ ${lista[`expr $FNO+5`]}=$MM ];
  then
    if [[ ${lista[`expr $FNO+6`]} -lt $DD ]];
    then
      # Remove this file
      echo "Removing ${lista[`expr $FNO+8`]}"
      ftp -i -n $ftpsite <<EOMYF2 
      user $ftpuser $ftppass
      binary
      cd $putdir
      delete ${lista[`expr $FNO+8`]}
      quit
EOMYF2


    fi
  fi
done

But it won't work. Did anyone know the issue why it will not working?

Last edited by Seppelchen; 01-20-2015 at 12:05 PM..
# 2  
Old 01-20-2015
WHAT does not work? To me it looks like it's producing the filenames you want (even though the script itself could benefit from some polishing).
# 3  
Old 01-20-2015
Hello,we get error that something is wrong on that line who will only display the "`" that comes after "EOMYF". Error will build if we run the script on crontab.
# 4  
Old 01-20-2015
And that err msg is literally "something is wrong"?

Did you consider that cron has a rudimentary environment only, and will execute scripts using /bin/sh (unless told otherwise)?
# 5  
Old 01-20-2015
We get this message:
removing files older than Jan 19
/var/www/vhosts/domain.tld/httpdocs/sh_scripts_folder/delete_old_files_backupserver_server.sh: line 27: ftp: command not found

Please look ahead you can see that the error cause by "´"
# 6  
Old 01-20-2015
It's not caused by ` (that only terminates the multiline command and makes the shell start expansion etc.). The ftp command is not found because the PATH variable in cron jobs is very limited. Make sure you either set the PATH variable or run ftp by its absolute path.
# 7  
Old 01-20-2015
Hello,
how we could run this as absolute path? Could you please post an example?
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script needed to delete to the list of files in a directory based on last created & delete them

Hi My directory structure is as below. dir1, dir2, dir3 I have the list of files to be deleted in the below path as below. /staging/retain_for_2years/Cleanup/log $ ls -lrt total 0 drwxr-xr-x 2 nobody nobody 256 Mar 01 16:15 01-MAR-2015_SPDBS2 drwxr-xr-x 2 root ... (2 Replies)
Discussion started by: prasadn
2 Replies

2. Shell Programming and Scripting

Delete files older than 1 year through FTP

Hi All, I want to login to a remote server using FTP command and then check for files older than 1 year and delete those files. Please let me know how can i achieve this using Unix Commands. Thanks in Advance, (10 Replies)
Discussion started by: HemaV
10 Replies

3. 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

4. Shell Programming and Scripting

how to delete 3 days old files using ftp in a shell script (Solaris OS)

I need help in writng a script to delete last three days files from a remote server using only FTP / SFTP. I was using find command which is not working and i cant use ssh Immediate response is highly appreciated . Thank in advance! ---------- Post updated 12-05-10 at 09:16 PM ----------... (5 Replies)
Discussion started by: Olivia
5 Replies

5. AIX

How to delete information on Storage

Hi, I need some help with two servers, they are running on AIX 5.3, we want to remove and delete all information stored on them because we are no longer to use anymore. The servers are going to be part of donation and we want to be sure that no one can restore information stored on disks or... (1 Reply)
Discussion started by: DBAIX
1 Replies

6. Shell Programming and Scripting

delete old files from FTP server

Hi I just started learning Unix scripts. I need to access an FTP server, delete all the files except the latest 6 files and the latest 6 files should be downloaded to my local server. Could some body help me to code this logic in KSH ? Thanks Athena (1 Reply)
Discussion started by: Athena
1 Replies

7. Shell Programming and Scripting

how to restrict FTP users not to delete their files

Hello all, We have an FTP Server setup with VSFTPd and its working fine without anonymous login (we must maintain this standard) The requirement is to restrict users not to overwrite (and delete) their files. In other words, once their files are uploaded to FTP Server, they should not be... (1 Reply)
Discussion started by: prvnrk
1 Replies

8. UNIX for Dummies Questions & Answers

Reg: delete older files from ftp

Hi, I want to delete older files from ftp server. (files which are more than 5 days old). Please advice Thanks , sam (3 Replies)
Discussion started by: sam99
3 Replies
Login or Register to Ask a Question