Delete 5 days older files from sftp

As one of our requirement was to connect to remote Linux server through SFTP connection and delete some files which are older than 7 days.

I used the below piece of code for that,

SFTP_CONNECTION=`sftp user_id@host ... 
cd DESIRED_DIR;
find /path/to/files* -mtime +5 -exec rm -rf {} \;
bye
END`

But unfortunately, "find" command does not work with "SFTP" connection, and as a result, iam unable to remove files from remote server.

I even can not use "SSH" for security purpose. And the script is fired from "ksh".

So my question is, how can we delete files from remote server through "SFTP".

Or, is there any alternative command of "ls" which does the exactly same thing as,

"find /path/to/files* -mtime +5 -exec rm -rf {} \;"

Do you have, or can you install, lftp ? It is a scriptable FTP client that can use several protocols, including SFTP.

One of lftp 's commands is cls which could be used to make a listing of filename and date which could then be processed locally to identify files to be deleted. It won't be pretty. The man page can be found here so that you can read it and decide whether you want to go this route. (The man page doesn't actually give any information about 'cls' unfortunately)

Andrew

You could also do an ls -l in your first sftp and capture the output to a (local) file. You can then work out which files to delete and call an appropriate second sftp to delete the files.

You might be better to write your commands in a temporary file and use the -b flag of sftp to run it in batch mode.

I hope that this helps,
Robin