scp files and delete files on remote directory [closed]
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 scp files and delete files older than 5 days in the destination server.
How do I securely delete files from the destination?
Thanks.
To remove files non-interactively:
ssh hostname "rm -f z100.foo z200.foo"
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.
If you would like to set username and port number you can do the following:
ssh [email protected] -p22 "rm -rf /home/amzad/test.php"