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

I am a unix noob, so please be patient :-)

I have a script that scps a bunch of files to another server.

Suppose they are, etc. What I want to do is to scp files and delete files older than 5 days in the destination server.

How do I securely delete files from the destination?


share|improve this question

closed as off-topic by Jenny D, Mark Wagner, dyasny, mdpc, Ward Jun 27 '14 at 5:29

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions must demonstrate a minimal understanding of the problem being solved. Try including attempted solutions, why they didn't work, and the expected results. See How can I ask better questions on Server Fault? for further guidance." – Jenny D, Mark Wagner, dyasny, Ward
If this question can be reworded to fit the rules in the help center, please edit the question.

"overwrite the files at the destination"? If you scp files that already exist on the remote dir they'll be overwritten automatically – Aki Jul 10 '14 at 0:11

To remove files non-interactively:

ssh hostname "rm -f"
share|improve this answer
what is hostname? is it servername@ipaddress:path/to/dir/ ? – HattrickNZ May 27 at 3:27

You could accomplish both tasks in the same command by using the --delete flag to rsync.

> ls -1 localdir/

> ssh remote-host "ls -1 remotedir/"

> rsync -a --delete localdir/ remote-host:remotedir/

> ssh remote-host "ls -1 remotedir/"

The --delete option removes files from the destination directory that don't exist on the source. There are some choices about when the files are deleted and how to handle excluded files.

> man rysnc
--delete                delete extraneous files from dest dirs
--delete-before         receiver deletes before transfer (default)
--delete-during         receiver deletes during xfer, not before
--delete-after          receiver deletes after transfer, not before
--delete-excluded       also delete excluded files from dest dirs

The man page also contains this warning:

This option can be dangerous if used incorrectly! It is a very good idea to run first using the --dry-run option (-n) to see what files would be deleted to make sure important files aren't listed.

share|improve this answer

If you would like to set username and port number:

ssh root@ -p22 "rm -rf /home/amzad/test.php"

share|improve this answer

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