Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

tar czf dist.tgz --exclude=".gitignore" .

This creates files in tar with leading "./" which later turns to problem when server needs to extract parts of tar.

Could someone tell me how to pack it so that wont have leading "./"?

share|improve this question
2  
Why is the leading ./ a problem? –  chutz Dec 12 '12 at 15:46
    
Take * instead of ., unless you need to include files and directories in the current dir starting with dot. –  ott-- Dec 12 '12 at 16:29

4 Answers 4

Try the --xform option, which uses a sed-like replace expression. This allows you to change the path references going into the archive rather than handle it during the extraction (such as with the --strip-components).

tar --exclude=.gitignore -cz --xform s:'./':: -f dist.tgz ./

To list the files as they are processed, add the -v and --show-transformed-name options:

tar --exclude=.gitignore -cvz --xform s:'./':: --show-transformed-name -f dist.tgz ./

The argument to pass --xform follows this structure: s/regexp/replace/[flags] Note that in my example, I used : as a delimter; you can use any character you want so long as you use it consistently. Good reference here.

share|improve this answer
    
If you don't have a tar with --xform, then you might have the pax(1) command instead, which has a similar option. –  Phil P Oct 9 '13 at 18:33

Not sure what version of tar this is, but normally you have to specify what files you want to archive. Like tar c . to compress everything in the local directory. Running your command on my machine I get:

tar: Cowardly refusing to create an empty archive

So, if you don't want to get the leading ./, which is what you get when you compress the local directory ., you can simply specify the files directly. For example:

> tar c hosts fstab | tar tv
-rw-r--r-- root/root      1225 2012-10-20 13:37 hosts
-rw-r--r-- root/root       678 2012-05-13 22:09 fstab
share|improve this answer

I've used tar -P to preserve the leading / (rather than ./) It's not entirely clear if this would help in your case, though.

-P, --absolute-names
    don't strip leading '/'s from file names 

man tar

share|improve this answer
    
'tar czf dist.tgz --exclude=".gitignore" .' –  Aram Dec 17 '12 at 9:36
    
What? Yes, throw a P in there and call it good. –  Aaron Copley Dec 21 '12 at 20:02

In the old versions of tar, it used to use the full path names you provided, in the tar, but this is somehow problematic as one might want a second copy of something and an unintentional expansion of the tar archive in the old format days, might overwrite an existing file or files.

So, in most recent versions of tar binary, distributed in almost any linux distribution, puts the ./ at the beginning of each file it packs up, so that, an unintentional expansion, expands the archive into your current working directory and below.

I am not sure why it is causing such a problem for you. if you are in the right directory level, where you want to expand this archive into, it will do just that. For instance, lets say you have a file as

/tmp/myarchive.tar

and let's assume the contents of the archive are

./etc/hosts
./etc/profile
./etc/passwd
./etc/group

if you expand this archive while your current working directory is / (i.e. root level), these files will go and overwrite the existing

/etc/hosts
/etc/profile
/etc/passwd
/etc/group

if this is what you want, you've got it. But let's say, you only wanted the hosts files out of this archive but all of a sudden you obliterated profile passwd and group files. Hope you have a good backup at this point, so that you can get them back

But, if you are the type of sysadmin, who avoids being in the root level, unless he/she needs it intentionally, you are most probably logged in ans located under /home/some-user or if you logged in as root, you are in /root directory. Let's say your in /root. when you execute the command

# tar -xvf /tmp/myarchive.tar

you will end up with

/root/etc/hosts
/root/etc/profile
/root/etc/passwd
/root/etc/group

no harm, no foul. Then

# mv /root/etc/hosts /etc/hosts

Voila. You accomplished your goal.

share|improve this answer
    
You can also use tar -C / to extract to root level without going there. –  chutz Dec 12 '12 at 16:40
    
Yes, I see that.. my example was to explain the risks of absolute vs. relative paths. –  MelBurslan Dec 12 '12 at 16:58
    
I tried all you wrote here. I don't need absolute paths. I need relative to folder I gave but without leading './'. –  Aram Dec 17 '12 at 9:37
    
./ denotes a relative folder. SO you should have nothing to worry about –  MelBurslan Dec 17 '12 at 18:21

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.