LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-20-2007, 07:53 AM   #1
xushi
Senior Member
 
Registered: Jun 2003
Location: UK
Distribution: Gentoo
Posts: 1,288

Rep: Reputation: 45
Exclamation bash: how to tar without absolute path?


Hi all,

What i need to do is to backup many folders, but the resulting archives' names and content should be just the directory name, and not the full path.. I can't figure out how to do so.

For example, here is part of my script that does the task,

Code:
BACKUP="/var/www/localhost/htdocs /home/xushi/Work /home/anotheruser/myfolder /etc/apache2"
DATE=`date +%F`
DEST="/home/xushi/backup/"
TARGET=$DEST$DATE/
 
# tar all the folders.
for i in $BACKUP
do
        NF=$( echo $i | sed 's/\//_/g')-"$DATE".tar.bz2
        
        echo Backing up $i to $TARGET$NF
        echo tar -cjf $TARGET$NF $i
done
echo "Done."
But the resulting filenames are (for example) like _var_www_localhost_htdocs-2007-05-20.tar.bz2 and the content is in /var/www/localhost/htdocs/ instead of just htdocs-2007-05-20.tar.bz2 and htdocs/ respectivly

What am i missing? I was told that i need to use -C, but i can't figure out how/where to use it..

I appreciate the help.

Thanks.
 
Old 05-20-2007, 09:13 AM   #2
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 735

Rep: Reputation: 76
Hi.

Here's a short example of tar using relative paths, e.g. ".", and changing to a directory with "-C":
Code:
#!/bin/sh

# @(#) s1       Demonstrate tar with relative paths in different directories.

DIRS="./d1 ./d1/d2"

# Remove debris from previous runs, create fresh test files.

rm -rf d1

mkdir -p $DIRS
touch d1/d2/file1 d1/d2/file2

echo
echo " Contents of test directory:"
ls -FR d1

echo
echo " Tell tar to Change to a directory and tar from \".\""
echo
tar cvf my-tar -C d1/d2 .

echo
echo " Contents of tar:"
echo
tar tvf my-tar

exit 0
which produces:
Code:
% ./s1

 Contents of test directory:
d1:
d2/

d1/d2:
file1
file2

 Tell tar to Change to a directory and tar from "."

./
./file1
./file2

 Contents of tar:

drwxr-xr-x makyo/makyo   0 2007-05-20 09:08:57 ./
-rw-r--r-- makyo/makyo   0 2007-05-20 09:08:57 ./file1
-rw-r--r-- makyo/makyo   0 2007-05-20 09:08:57 ./file2
Note well that the final parameter on the tar is a dot.

If you want the directory name, e.g. "./d2/file1", provide the path to "-C" one level up: "-C d1".

See man tar and info tar for details. Best wishes ... cheers, makyo

( edit 1: add, clarify )

Last edited by makyo; 05-20-2007 at 09:24 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Absolute path qspares Linux - Software 6 10-02-2007 03:11 AM
Image Path reference in Linux (Absolute path) javabuddy Linux - General 7 06-05-2006 07:45 AM
tar saves absolute path warshke Linux - Software 2 03-27-2006 07:43 AM
absolute path results with ls hawkmauk Programming 12 01-16-2006 09:33 PM
relative to absolute path vishalbutte Programming 4 01-14-2006 03:17 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:29 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration