Skip to content

bash function to sync to and from a remote host by using rsync and ssh

I quickly created two bash functions to use rsync and ssh for syncing directories from the local host to a remote host and vice versa.

The two methods are available on my shell function repository. The two methods are called net_bazzline_sync_from_host and net_bazzline_sync_to_host. Enjoy it :-).

####
# Uses rsync with ssh to copy data from remote to local host
#
# @param string $1 - remote user
# @param string $2 - remote host
# @param string $3 - source path on remote host
# @param string $4 - destination path on local host
#
# @author stev leibelt 
# @since 2013-07-05
####
function net_bazzline_sync_from_host ()
{
    if [[ $# -eq 4 ]]; then
        USER="$1"
        HOST="$2"
        SOURCE="$3"
        DESTINATION="$4"
    else
        echo 'invalid number of variables provided'
        echo 'command user host source destination'
    fi

    rsync -avz -e ssh $USER@$HOST:$SOURCE $DESTINATION
}

####
# Uses rsync with ssh to copy data from local to remote host
#
# @param string $1 - remote user
# @param string $2 - remote host
# @param string $3 - source path on local host
# @param string $4 - destination path on remote host
#
# @author stev leibelt 
# @since 2013-07-05
function net_bazzline_sync_to_host ()
{
    if [[ $# -eq 4 ]]; then
        USER="$1"
        HOST="$2"
        SOURCE="$3"
        DESTINATION="$4"
    else
        echo 'invalid number of variables provided'
        echo 'command user host source destination'
    fi

    rsync -avz -e ssh $SOURCE $USER@$HOST:$DESTINATION
}

bash - enhanced svn diff

Again i want to share a simple bash function for dealing with the svn diff. I was tired to add the file i want to compare multiple times so i wrote a small wrapper function. The syntax is the following. net_bazzline_svn_diff https://svn.mydomain.org/trunk https://svn.mydomain.org/branches/my_branch module/myModule/myFile.foo Enjoy it and be aware that there is no fancy validation logic inside.

#####
# Calles svn diff for two repositories.
# Call $repositoryUrlOne $repositoryUrlTwo $filePath
#
# @author stev leibelt
# @since 2013-01-30
####
function net_bazzline_svn_diff ()
{
  if [ $# -eq 3 ]; then
    svn diff "$1""/""$3" "$2""/""$3"
  else
    echo 'No valid arguments supplied'
  fi
}
Sourceode also available on github.com.

bash - enhanced mkdir

Since i am fully addicted to the shell, the customisation started. This time, i want to share a small enhancement for the "mkdir" bash command. The code i will paste below is doing the following thing. If you supply one argument, it will create the directory you want with "mkdir -p" and change into that. If you supply multiple arguments, it will behave like the normal "mkdir". How to use it? Open your .bashrc file and add the code provided below. Then define an alias like "alias mkdir=net_bazzline_mkdir". When you open your next shell, your mkdir is enhanced :-).

function net_bazzline_mkdir ()
{
  #check if at least one argument is supplied
  if [ $# -eq 0 ]
  then
    echo "No arguments supplied"
    return 1
  fi

  #if one argument is supplied, create dir and
  # change to it
  if [ $# -eq 1 ]
  then
    mkdir -p "$1"
    cd "$1"
    return 0
  fi

  #if more then one argument is supplied
  # execute default mkdir
  if [ $# -gt 1 ]
  then
    mkdir $@
    return 0
  fi
}
Sourceode available on github.com.