MoreFreeze
MoreFreeze

Reputation: 2936

How to make a alias to rsync?

I often use this command to sync remote and local.

rsync -xyz --foo=bar some_files [email protected]: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" [email protected]:"$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' [email protected]:"$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.

Upvotes: 1

Views: 1980

Answers (2)

IBam
IBam

Reputation: 11844

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.

Upvotes: 2

R.Dussin
R.Dussin

Reputation: 312

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.

Upvotes: -1

Related Questions