简体   繁体   中英

How to make a alias to rsync?

I often use this command to sync remote and local.

rsync -xyz --foo=bar some_files user@remote.com:remote_dir

So I want to simplify this with an alias, like this:

up_remote () {
    local_files=${@:1:$((${#}-1))}  # treat argument[1:last-1] as local files
    remote_dir="${@[$#]}" # treat argument[last] as remote_dir
    echo $local_files
    echo $remote_dir
    rsync -xyz --foo=bar "$local_files" user@remote.com:"$remote_dir"
}

But if I pass three or more arguments it won't work:

up_remote local1 local2 remote_dir

When I debug this function with set -x I find that the function will generate rsync like this:

rsync -xyz --foo=bar 'local1 local2' user@remote.com:"$remote_dir"

Notice the single quotes( ' ) around local1 local2 . If I remove these single quotes the rsync will work correctly, but I don't know how to do this.

Any advice is welcome.

You just need to remove the double quotes around $local_files :

up_remote () {
  local_files=${@:1:$((${#}-1))}
  remote_dir="${@:$#}"
  rsync -xyz --foo=bar $local_files a.b.com:"$remote_dir"
}

Note I also changed the way that remote_dir is picked, couldn't get your way to work in my version of bash.

Not really an answer but I did it with perl, and it works:

#!/usr/bin/perl

use strict;
use warnings;

my @args = @ARGV;
my $files;

my $destination=pop(@args);

foreach(@args){
    $files.="'".$_."' ";
}

system("rsync -xyz --foo=bar $files user\@remote.com:$destination");

You can copy this script in your path or make an alias to it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM