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

I use the following pseudo-script to create a TAR of my installed software

mkdir tmp
ln -s /path/to/app1/bin              tmp/app1
ln -s /and/path/going/to/the-app-2   tmp/app2
tar -c --dereference -f apps.tar tmp

I need the --dereference option here to follow the links I just made in tmp. The reason I make the links in the first place is to store the directories with a different name in the archive than they have on the filesystem.

Until now it has worked fine. However, I now have the situation that /path/to/app1 also contains links, and those I don't want to follow.

Is this possible with some changes to the tar command? Or do I need to completely switch around the way I build the archive?

share|improve this question
add comment

4 Answers 4

I don't think there is a way to do have just a partial dereference. You could do something like

tar -cf apps.tar /path/to/app1/bin /and/path/going/to/the-app-2

and then to extract them to a different root using -C

-C, --directory=DIR

         change to directory DIR

e.g.

mkdir tmp
tar -C tmp -xf apps.tar

which would have a similar effect to the way you currently create your archive.

You can use the -C to point to any existing directory too.

share|improve this answer
    
That could work, though it would tightly couple the contents of the archive with their locations on disk. That's more a theoretical objection though. –  Bart van Heukelom Aug 28 '12 at 9:08
add comment

Do you really need the directory structure in the tarball to be different than it is on disk?

If not, then you can just tar them up as-is:

tar -cf file.tar /path/1 /path/2
share|improve this answer
    
Yes, because I need to include for example 2 directories named bin –  Bart van Heukelom Aug 28 '12 at 9:06
add comment

Instead of linking, I could copy. Unfortunately it would add some more overhead (the files are rather big). Of course it would grow at most twice as big, since the tarring itself is also a form of copying.

share|improve this answer
add comment

If you're root then your best bet is to use mount's bind command to basically hard link the directories you want to tar up, rather than symlinking to them. Then you won't need to use the dereference option.

mkdir tmp
mount --bind /path/to/app-1 tmp/app-1
mount --bind /path/to/app-2 tmp/app-2
tar cf apps.tar tmp

Be sure to unmount those directories before anything accidentally happens to them. See these warnings.

share|improve this answer
add comment

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.