LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Tar and symlinks (https://www.linuxquestions.org/questions/programming-9/tar-and-symlinks-516993/)

Johng 01-06-2007 05:50 PM

Tar and symlinks
 
I want to save a directory that has one or more symlinks without all the files that the symlink(s) point to (ie I don't want to follow the symlinks).

The -h option is to follow symlinks, but if I don't include the -h, tar does follow the symlinks by default.

How do I stop tar (version 1.15.91) from following symlinks??

gilead 01-06-2007 07:30 PM

I'm using tar (GNU tar) 1.16 and if I don't use the -h option, I do get symlinks in the archive.

MensaWater 01-06-2007 07:47 PM

Just to make sure we have the right reference:

"follow" means go get the file it points to rather than getting the link itself.

You can do exclude of any file. Just because you get the symlink in your backup doesn't mean you get the file it points to unless it is also in your selection criteria.

Many backups include both the symlink and the file it points to but that isn't because it was "followed" but rther because they were both in the selection.

If you have two directories:

dir1
dir2

dir1 contains
file1
file2
sym1
sym2

dir2 contains
file3
file4

If sym1 points at file1 and you backup dir1 you're going to get both the symlink and the file it points at. If sym2 points to file3 and you backup dir1 you'll get a symlink that says it points to file3 but you won't get file3 itself. So on the restore (assuming you didn't do it to the machine that had dir2 on it) you would have a broken symlink.

The "-h" backup of dir1 would actually remove the symlinks and pull in file1 with the name of sym1 and file1 and file3 with the name of sym2. (Note you'd have file1 come back in both as file1 and as sym1 on the restore).

As noted though tar allows for excluding files either with the --exclude PATTERN (to specify on command line) or --exclude_from FILE (to exclude by reading a FILE into which you had listed the files to exclude)

Of course you can always use find to modify things:

find /dir1 -type f |tar ....

Would tell it to only pipe regular files into the tar command.

Johng 01-07-2007 03:31 AM

Thank you jlightner. I understand your description, and it is as I expected.

However, whereas tar version 1.15.1 does just as you describe, version 1.15.91 included with Mandriva 2007 follows the link, ie it includes the files in the tar that the link points to.

MensaWater 01-07-2007 09:05 AM

Interesting. Latest I'm using is 1.15.90-1FC5 (On Fedora Core 5) and it still operates the way I describe.

Did some Google searches but found no mention of this but some other bugs in 1.15.91 that continued into 1.16. You may wish to have a look at the man page on your system that has 1.15.91 and and see if it has any references to following links by default.

I did see some discussion of a change regarding hard links for symbolic links but that was introduced in 1.13 so isn't likely the issue.

It may be you've found a bug. If so you may wish to go to the GNU site for tar and report it. The site is:
http://www.gnu.org/software/tar/

Harmaa Kettu 01-07-2007 10:02 AM

Quote:

Interesting. Latest I'm using is 1.15.90-1FC5 (On Fedora Core 5) and it still operates the way I describe.
The newest stable release of tar (1.16.1) works that way too.

I think there are three possibilities:
  1. It is caused by some Mandriva-specific patch. Check the source rpm.
  2. The behaviour was changed in tar 1.15.91 but was later changed back.
  3. Johng has alias tar="tar -h" somewhere and does not know/remember it.

Quote:

It may be you've found a bug. If so you may wish to go to the GNU site for tar and report it. The site is:
http://www.gnu.org/software/tar/
No, it should be reported to Mandriva developers, of course assuming that (1) or (2) above is the cause and there isn't an updated package available already.


All times are GMT -5. The time now is 01:14 AM.