[Novalug] how to supersede file names with spaces

Michael Henry lug-user@drmikehenry.com
Sun Dec 30 17:29:13 EST 2012


On 12/30/2012 11:13 AM, Nino R. Pereira wrote:
> rsync --rsh=ssh .* $LOCALDIR $HOST:$REMOTEDIR
>
> In my case, I back up from my computer, with
>
> LOCALDIR=/home/pereira,
>
> to a directory I created to back up to, viz.,
>
> REMOTEDIR=/home/pereira-2012
>
> and I change the external host from an IP address to the file
> name of the disk (HOST=66.92.144.240) to a variant of
>
> HOST='/media/My\ Book\ 3.0/disk/'
>
> where I tried all the combinations of '' and "" I could think
> of to make the file name 'My\ Book\ 3.0' look like a single
> string. But, rsync insists on splitting the file name at the
> spaces.
>
> Any suggestion on how to overcome this? if so I could use the
> 'rsync' command for more regular backups again.

I see a couple of issues here.  Firstly, rsync wants to see a
colon only if the part before the colon is actually a hostname.
If you are copying between two locally mounted paths, you should
leave the colon out, and leave out the '--rsh=ssh' switch since
you aren't doing anything remote (and that's the default these
days anyway).  Secondly, you need to quote paths that have
spaces both where you define the variable and where you use it.
You don't need backslashes when you are using quotes (in fact,
if you use backslashes with single quotes, you'll end up with
extra backslashes in your path).

Something like this should work:

SOURCE='/home/pereira/'
DEST='/media/My Book 3.0/disk/home/pereira-2012'
mkdir -p "$DEST"
rsync -ax "$SOURCE" "$DEST"

You need the quotes on the rsync line to prevent Bash from
splitting on spaces.  Single quotes won't work, since you want
Bash to expand the variable inside the double quotes.

The "-a" switch means "archive", which is usually what you want
when making a backup.  "man rsync" to see what switches this
turns on.  I always use "-x" as well out of habit, to prevent
rsync from crossing filesystem boundaries.

The trailing slash on SOURCE tells rsync you want to copy the
contents into the destination.  Without that, rsync will want to
put the final component of the source path into the destination.
Try it to see what I mean.

I'm unfortunately out of time, but if you still have questions,
hopefully someone else will be able to chime in.

Michael Henry




More information about the Novalug mailing list