Space in Dir Name with shell script and tar

Enrique Perez-Terron enrio@online.no
Thu Jan 18 02:43:00 GMT 2007


From: "Troy" <talexander@veeco.com>
To: <cygwin@cygwin.com>
Sent: Thursday, January 18, 2007 2:50 AM
Subject: Space in Dir Name with shell script and tar


>
> I am trying to make a shell script to do automatic bckups with tar.
>
> My problem is that tar stumbles on the spaces in the dir names if they are
> in a variable.
>
> Probiably best shown by example
>
> The dir I want to back up is
> g:/Mark I+/Experimental Data/
>
> I if do this in the script
> tar -czvpf $NAME_OF_BACKUP.tgz /cygdrive/g/Mark' 'I+/Experimental' 'Data/*

Try this:

    tar -czvpf $NAME_OF_BACKUP.tgz "/cygdrive/g/Mark I+/Experimental Data"

That is, enclose the name of the directory in double quotation marks, and 
drop the trailing /*

> it works fine but if I do this
>
> DIRECTORY_TO_BACKUP="/cygdrive/g/Mark' 'I+/Experimental' 'Data"

Problem: You are enclosing the apostrophes in the double quotation marks. 
That means the
apostrophes become part of the value of the variable DIRECTORY_TO_BACKUP

> tar -czvpf $NAME_OF_BACKUP.tgz $DIRECTORY_TO_BACKUP/*

Problem: Bash will replace the variable name with the variable's value, but 
it will not interpret
the apostrophes specially. Interpretation of apostrophes occur at an earlier 
stage. Next, bash
will perform word splitting on the variable's value. This means that the 
spaces contained in
the variable's value will be treated specially, as word splitting points.

Try this

     DIRECTORY_TO_BACKUP="/cygdrive/g/Mark I+/Experimental Data"
     tar -czvpf $NAME_OF_BACKUP.tgz "$DIRECTORY_TO_BACKUP"

i.e., enclose the variable's assigned value in double quotation marks (or 
apostrophes, if you prefer,
but for your convenience, use a single pair around all of the value. Next, 
enclose the variable
substitution in double quotation marks. This time it must be double quotes, 
not apostrophes,
since apostrophes prevent variable substitution.

You can tack /* at the end if you want, like this "$DIR"/* but if you want 
to back up everything
in the directory it is better to leave out /*. With /*, files and 
directories whose names begin with
a dot (like .bashrc) are *not* included in the backup. (This applies to 
files at the top level.
A file named "$DIR"/some-subdir/.bashrc is backed up with or without /*.)
>
> I get
> tar: Removing leading `/' from member names

This is normal.  You are specifying absoluth paths, and then, upon restore, 
you would be forced
to restore to exactly the same path. When tar removes the leading / it 
becomes possible to
restore the data to a different directory, eg to /restored/cygdrive/c/...

Here you see another point. It is better to do

     cd "$DIR"
     tar cvfpz bakupname.tgz .

with a single dot as the backup directory. Then, upon restore, you do not 
need to have a prefix
of directories cygdrive/c/...


> tar: /cygdrive/g/Mark': Cannot stat: No such file or directory
> tar: 'I+/Experimental': Cannot stat: No such file or directory
> tar: 'Data/*: Cannot stat: No such file or directory
> tar: Error exit delayed from previous errors
>
> What am I doing wrong?
> -- 
> View this message in context: 
> http://www.nabble.com/Space-in-Dir-Name-with-shell-script-and-tar-tf3031755.html#a8423703
> Sent from the Cygwin Users mailing list archive at Nabble.com.
>
>
> --
> Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
> Problem reports:       http://cygwin.com/problems.html
> Documentation:         http://cygwin.com/docs.html
> FAQ:                   http://cygwin.com/faq/
>
>
> 



--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/



More information about the Cygwin mailing list