Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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 z1.foo, z2.foo etc. What I want to do is to overwrite the files at the destination. However there might be additional z100.foo files at the destination from a previous job that I need to delete.

How do I securely delete files from the destination?

Thanks.

share|improve this question

closed as off-topic by Jenny D, Mark Wagner, dyasny, mdpc, Ward Jun 27 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 at 0:11

3 Answers 3

To remove files non-interactively:

ssh hostname "rm -f z100.foo z200.foo"
share|improve this answer

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

> ls -1 localdir/
a.foo
b.foo

> ssh remote-host "ls -1 remotedir/"
c.foo

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

> ssh remote-host "ls -1 remotedir/"
a.foo
b.foo

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@203.0.113.123 -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.