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,

Please mention the exact Operating System and version of both computers and the Shell used on both computers.

Though not impossible, it is extremely difficult to achieve this with ftp alone. Unix ftp is not unix Shell and it has a very limited command set.

Do you have administrative access to the remote computer? Do you have Remote Shell access to the remote computer?
I see from your previous posts two month ago that you use ftp a lot, but never managed to work out what Operating System or Shell you had.

Both the servers are unix servers with AIX uname. I'm using Korn Shell.

Is this the information required? if not how to find out.

Thanks in Advance,

If both servers are AIX, surely you can use Remote Shell or a cron or whatever. Anything but ftp. In fact the file transfers would be easier with Remote Copy.
The only real use for ftp nowadays is to copy files between incompatible systems.

You can redirect the "dir" in one ftp session to a log, parse the log and get the list of files you want to delete. Then delete them in another ftp session.

Or you could use Perl with Net:FTP (core module since v5.7.3).

Remove all files that are 365 days old:

perl -MNet::FTP -e'
    ( $host, $user, $pass ) = @ARGV;
    $ftp = Net::FTP->new($host) or die "$@\n";
    $ftp->login( $user, $pass ) or die $ftp->message;
    $ftp->mdtm($_) < time - (86400 * 365) and $ftp->delete($_) for $ftp->ls; 
    $ftp->quit or die $ftp->message;
    ' <host> <user> <pass>

Standard disclaimer: you should backup your data before running the above code! You may need to add a file name filter, change the working directory etc.

@binlib
Please post your AIX-compatible code to determine if a file is more than a year old based on a ftp directory listing.

My technique is to create dummy files on the source system with a timestamp deduced from the ftp directory listing, and then use the unix "find" command to determine which files to delete from the destination server.

I'm up for a challenge

M=$(date +%m)
Y=$(date +%Y )
ls -ln | awk -vY=$Y -vM=$M 'BEGIN {
  split("Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec",mt,",")
  for(i=1;i<13;i++)p[mt]=i;
}
/^[^d]/ && $8>1970 && ($8 < Y - 1 || $8 < Y && p[$6] < M){ print $9 }'

Just a side note: AFAIK the -v<varname> (with no space between -v and <varname>) is a GNU awk extension, other awk implementations (new awk etc) support the other version: -v <varname> (with a space).

2 Likes

Thanks - don't think I've come across this before. Seems having the space might turn out to be more portable.
AIX 5.3 doesn�t have any trouble with the space missing, so it's not exclusively a GNU awk feature.

The usage string certainly shows a space:

$ awk --version
awk: Not a recognized flag: -
Usage: awk [-u] [-F Character][-v Variable=Value][-f File|Commands][Variable=Value|File ...]
 
$ awk -vPI=3.14159 'BEGIN { print PI }'
3.14159
 
$ csum -h MD5 $(which nawk) $(which awk)
ba598e3a4ccee166aa8efc7ef9a05a2e  /usr/bin/nawk
ba598e3a4ccee166aa8efc7ef9a05a2e  /usr/bin/awk

Also interesting to note that awk and nawk on AIX 5.3 are the same binary

1 Like

Thanks!
I didn't know that non-GNU awk implementations (in this case AT&T) support -v<varname> .