Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Do anyone know some good way to delete files on remote server that are older than X days using just SCP/SFTP? Sure I can write some script on perl etc but I feel it's overkill.
Any UNIX way?
Separate utility?


P.S. The task is to delete some outdated backup files.

share|improve this question

Sure I can write some script on perl etc but it's overkill.

You don't need a script to achieve the intended effect - a one-liner will do if you have shell access to send a command:

ssh user@host 'find /path/to/old_backups/* -mtime +7 -exec rm {} \;'

-mtime +7 matches files created one week ago from midnight of the present day.

share|improve this answer
Sad but this is using SSH and remote oneliner. There is no shell access, just SCP/SFTP. – Mike Sep 27 '10 at 10:09
@Mike - Well that one-liner can save you some time over writing a perl script, if that is the case - you could use atime instead of mtime to match the last access time (i.e. when your files were last downloaded) and run a daily cron job. – danlefree Sep 27 '10 at 16:37
there is no shell access to remote machine. – Mike Oct 2 '10 at 11:03
@Mike I was under the impression that you could negotiate adding a cron job with the administrator of the server hosting your backup files - my apologies if this is not possible. – danlefree Oct 3 '10 at 3:43

If you insist on SCP/SFTP you can list files, parse them using a simple script and delete old backup files.

Batch mode "-b" switch should help you out. It reads sftp commands from file.

share|improve this answer
Sure it's possible, but I'm looking for more elegant UNIX way if it exists. – Mike Sep 25 '10 at 10:12
Do you have any idea? maybe suggestion from your side would help? Then we can make the idea a bit better? – M_1 Sep 25 '10 at 12:08

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.