LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   replacing rm command with a rsync script :-) (https://www.linuxquestions.org/questions/programming-9/replacing-rm-command-with-a-rsync-script-768999/)

kdelover 11-13-2009 05:36 PM

replacing rm command with a rsync script :-)
 
Hello all,

I wanted to write a small script,which i would like to use in the place of rm command to move(rather delete) the files to a remote trash directory using rsync.

Does any one know how to delete directories from the source after the files are rsynced to the remote destination? the --remove-source-files works only for files,and leave many empty directories behind:(.

Secondly,is there a better way to do the samething instead of using a foreach statment?? I guess using rsync in a foreach stmtn,will spawn a new rsync connection everytime.

Code:

#!/usr/bin/perl
use strict;
use warnings;
my $user = "ryan";
my $host = "wsx04";
my $pwd ="";
my $Dir="Wsx02";
foreach (@ARGV) {
`rsync --bwlimit=2048 --remove-source-files --delete -e "ssh -c blowfish" -avzpr $_  $user\@$host:/Backup/Trash/$Dir/`;
}

Thanks All.I'm new to perl

ta0kira 11-13-2009 08:28 PM

You might create a list of files/directories first, then tar over ssh, then rm (all in a script.)
Kevin Barry

kdelover 11-14-2009 07:46 AM

id like to do that ,but i want use it just like how i use rm command :-)

ta0kira 11-14-2009 10:49 AM

You can make a script that does what you want, inferring the correct behavior from the rm options provided, then alias rm to it in your shell. You won't be able to achieve an exact solution without rewriting rm, e.g. if interactive mode is chosen, you'll probably have to back up the files anyway even though rm might leave them there, or if the user doesn't have permission to remove the files, etc. libc has a function called access to see if a user can rwx a file, but I don't know if perl implements something like that.

You can search for the most important options, such as -r, -x, and -- (plus their full-name counterparts) to infer the correct operation and files, then generate an archive, then pass the whole set of args to rm.
Kevin Barry

PS Remember that even though a user might be able to delete a file, they might not be able to recreate it with the same ownership unless they're root.


All times are GMT -5. The time now is 08:56 AM.