0

I thought I knew how GNU tar -h (aka --dereference) worked, but now I am confused.

Consider:

$ tree dir/
dir/
├── file
└── link -> file

1 directory, 2 files

If I add -h to my tar command, shouldn't I get an archive that has two file entries, one named file and one named link, each with the same contents? Instead, I get file archived as expected but then a symlink entry in the archive for link, pointing to file:

$ tar cf archive -h dir
$ tar tvf archive
drwxr-xr-x bspencer/bspencer 0 2024-01-12 15:36 dir/
-rw-r--r-- bspencer/bspencer 13 2024-01-12 15:41 dir/file
hrw-r--r-- bspencer/bspencer  0 2024-01-12 15:41 dir/link link to dir/file

Isn't the -h option supposed to prevent the creation of symlink entries in the archive?

$ man tar |grep -A1 -- --dereference 
       -h, --dereference
              Follow symlinks; archive and dump the files they point to.

I get the same result with the long name of the argument --dereference, including when I use --create instead of the traditional letters.

$ tar --version |head -1
tar (GNU tar) 1.34
3
  • 1
    most probably because the linked file belongs to the same directory. Retry with a link to another file of some other dir to confirm.
    – MC68020
    Commented Jan 12 at 20:38
  • That did not make a difference: dir/first/deep link to dir/second/also Commented Jan 12 at 20:54
  • 1
    If link points outside of dir/ (not within. ie: points to a file that would not be part of the archive), then tar will dereference it (as I could see when testing). I find the documentation a bit misleading by not detailing the behavior.
    – A.B
    Commented Jan 12 at 21:55

1 Answer 1

0

Tar does correctly expand such an archive to contain separate files instead of links despite the output of tar tvf:

$ tree dir
dir
├── file
└── link -> file

1 directory, 2 files
$ tar cf archive -h dir
$ tar tvf archive
drwxr-xr-x bspencer/bspencer 0 2024-01-16 12:26 dir/
-rw-r--r-- bspencer/bspencer 9 2024-01-16 12:26 dir/file
hrw-r--r-- bspencer/bspencer 0 2024-01-16 12:26 dir/link link to dir/file
$ mkdir out
$ cd out && tar xvf ../archive
dir/
dir/file
dir/link
$ tree
.
└── dir
    ├── file
    └── link

2 directories, 2 files
$ ls -l dir
total 8
-rw-r--r-- 2 bspencer bspencer 9 Jan 16 12:26 file
-rw-r--r-- 2 bspencer bspencer 9 Jan 16 12:26 link

So, tar does work correctly.

I was incorrectly assuming that tar tvf would reliably show me the archive's contents. Instead, it shows "link to" even when the archive will expand to contain actual separate files.

As an aside, my guess as to why tar tvf's output talks about links is that tar stores the file contents as links to save space and just marks them to be created as separate file upon expansion. But, for some reason, tar tvf does not indicate this, and just shows the actual internal storage.

3
  • Guessing is okay but it would be better to find out for certain.
    – Ken Sharp
    Commented Jan 13 at 12:46
  • The guess is bonus. The answer to the question is that tar does work properly but tar tvf's view is not reflective of the actual expansion. Commented Jan 13 at 18:32
  • But sorry that's just wrong (see my other comment). I did test what I wrote. Lack of documentation prevents me to make an answer.
    – A.B
    Commented Jan 13 at 19:28

You must log in to answer this question.

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