Search code examples
bashshellrsync

bash rsync can't find directory with spaces


I am trying to make a backup script and the problem is that it can't find the directory that has spaces in it.

Code for bash script.

#!/bin/bash

Desk="/Users/elev/Desktop/"

include_paths=(
    "${Desk}/Skola"
    "${Desk}/music"
    "${Desk}/jobb"
    # not working directory
    "${Desk}/programmering\ skola"

)

# ${include_paths[@]} means every value in the list
for item in "${include_paths[@]}"
do
    include_args="${include_args} ${item}"

done

DestPath="/Volumes/Atheer/backup"

rsync -av --delete ${include_args} $DestPath

as you can see I have tried to use backslash and space, still it doesn't work. I get the following error code rsync: link_stat "/Users/elev/Desktop/programmering\" failed: No such file or directory I would like it to backup directories that have spaces in them too. I appreciate the help


Solution

  • You already know how to use arrays. You should use them more. :)

    rsync -av --delete "${include_paths[@]}" "$DestPath"
    

    "${include_paths[@]}" (the quotes are essential!) expands each element in the include_paths array to a separate rsync argument.

    There's no reason to generate include_args at all.