1

I am trying to make a tar file. I have 2 folders that I need to tar. let me clear my question.

folder 1. /temp1
folder 2. /temp1

now I want my tar output to be such that when i untar it i get

 /temp1/* (stuff of temp1)
 /temp1/temp2/* (temp2 and subdirectories inside temp1).

Right now I am copying temp2 into temp1 and than tarring it. Can anyone suggest me to do in a way that I don't have to copy the stuff, As if I kill the process in between I will be left some of temp2's stuff inside temp1.

1

Symlink temp2 into temp1, and then tell tar to dereference (follow) the symlink with the -h or --dereference option.

e.g.

cd temp1
ln -s ../temp2/ .
cd ..
tar cvz -h -f example.tar.gz ./temp1
rm -f temp1/temp2

This will make a tar archive with ./temp2 beneath the ./temp1 directory (i.e. ./temp1/temp2)

I can't remember if this -h option is a GNU extension or not. If you're not using GNU tar, test carefully before using.

If you care about the modification timestamp of temp1, then use touch -r:

touch -r temp1 temp1-modtime
cd temp1
ln -s ../temp2/ .
cd ..
touch -r temp1-modtime temp1
tar cvz -h -f example.tar.gz ./temp1
rm -f temp1/temp2
touch -r temp1-modtime temp1
rm -f temp1-modtime
  • This worked but I think this is same as copying as I have to unlink after making tar to keep the original. I needed something that can be used without modifying the original. However by your approach space wont be a problem. so that's a plus. But if you can suggest something which doesn't modify the temp1 directory, then that will be useful. Thanks. – Lokesh Sharma Mar 29 '16 at 10:04
  • @LokeshSharma I needed something that can be used without modifying the original. Then make two tar files and cd into temp1 to untar temp2. – Andrew Henle Mar 29 '16 at 10:08
  • creating and removing a symlink is also a lot faster than copying and later deleting a directory full of files. Aside from the fact that it changes the modtime of temp1 (which could be fixed with touch -r if necessary), i don't see any downside to using a symlink for this - it's simple, it works, it's fast, and it's not expensive in terms of resources (time, disk, cpu power). – cas Mar 29 '16 at 10:39
0

Use tar append function with some tricks. Something like this?

$ mkdir temp3; mkdir temp3/temp1    # create another, empty temp1 dir
$ ln temp2 temp3/temp1/temp2        # link temp2 into fake temp1

and then...

$ tar cf example.tar temp1      # 1st part of archive without temp2
$ cd temp3
$ tar rf ../example.tar temp1   # append temp2 as subfolder of temp1 to archive

Using hard links makes it possible to still have symlinks in the temp directories treated as symlinks. So -h not needed. Might keep you out of trouble later on.

You can also take a look at GNU tar extensions like --transform in combination with the append command. See also: http://www.linuxquestions.org/questions/linux-newbie-8/tar-subdirectory-question-821074/

  • upon linking temp2, I got the following error ln: ‘temp2’: hard link not allowed for directory – Archemar Mar 29 '16 at 12:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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