45

How do I tar ball/compress a directory hierarchy with soft links in Linux?

1
  • 4
    Do you want the final tarbal to have the soft-links or the target file of those links. It's unclear in your question.
    – slubman
    May 4, 2010 at 6:51

3 Answers 3

42

GNU tar normally includes symlinks in the tarball it generates. If you want it to include the file the symlink points to instead then pass it -h/--dereference when creating the tarball.

18

When used with standard arguments tar xvf, the soft links are saved in the tar archive as soft-links.  Then there are two cases:

  • If your symlink is a full pathname, it might be broken when untarred.
  • If your symlink is a relative pathname, under the folder you are tarring, it will work fine when extracted.

If you want to tar the file and not the symlink, you can use the -h / --dereference arguments.

3
  • 1
    And how is that different from another answer, which as well doesn't work?
    – EugZol
    Sep 21, 2021 at 14:14
  • 1
    @EugZol It works but you need to add h before f. So tar -hxvf for instance. Oct 22, 2021 at 10:25
  • 8
    shouldn't it be tar -hcvf in order to create an archive?
    – DiKorsch
    May 17, 2022 at 7:04
1

Just another way, using /etc as an example:

find /etc | cpio -puvdm . ; tar czf ttt.tgz etc
rm -rf etc
tar xvzf ttt.tgz
ls -l etc/rc*
2

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .