views:

22

answers:

1

Hi, I'm writing a script that can tar any given folder and place it in my home/bkp. the script will read like this,

tar czvf  /home/me/bkp/`basename $1`.tar.gz $1  

well now, to use it.,

backup-script.sh /home/me/folder/sub/to-be-backed-up/ 

Well and good.

Now when I untar it, it creates home/me/folder/sub/to-be-backed-up/*files* I just want to-be-backed-up/*files*

Is there any option while creating the tar files that i compress it relatively ?

+3  A: 

You could use the -C option of tar so that it changes to a given directory first. I often use the trick of appending /.. to the path to get the parent directory, so that your script would be something like:

# backupscript.sh <folder>
tar cvzf /home/me/bkp/`basename $1`.tar.gz -C $1/.. `basename $1`

Or you could perhaps use dirname instead to change to the right directory first

the_mandrill
In case the filename contains spaces, you should also add double-quotes around the arguments to `tar`, e.g.: `tar cvzf "/home/me/bkp/$(basename "$1").tar.gz" -C "$(dirname "$1")/.." "$(basename "$1")" `
Jabir Ali Ouassou
@all Thanks....
echo