Search code examples
linuxrsyncscp

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.


Solution

  • 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.