• VMware

    Learn about VMware virtualization for its products like vsphere ESX and ESXi, vCenter Server, VMware View, VMware P2V and many more

  • Linux

    Step by step configuration tutorials for many of the Linux services like DNS, DHCP, FTP, Samba4 etc including many tips and tricks in Red Hat Linux.

  • Database

    Learn installation and configuration of databases like Oracle, My SQL, Postgresql, etc including many other related tutorials in Linux.

  • How to preserve Symbolic links with tar command in Unix/Linux

    Well there are times when you have to take backup of your Linux machine and copy it some other location but at the same time you don't want to loose all the symlinks which are their in your machine. If you do a normal compression/decompression, then you will loose all your symbolic links which is not a good idea for a production environment specially.

    If you go through the man page of tar
    # man tar
     -h, --dereference
     follow symlinks; archive and dump the files they point to

    So, as of now we need to use -h argument along with tar command but how and when?

    # cd /home/test
    # ls -l
    -rw-r--r--. 1 root root 0 Sep 28 12:45 1
    lrwxrwxrwx. 1 root root 1 Sep 28 12:46
    10 -> 5
    -rw-r--r--. 1 root root 0 Sep 28 12:45 2
    -rw-r--r--. 1 root root 0 Sep 28 12:45 3
    -rw-r--r--. 1 root root 0 Sep 28 12:45 4
    -rw-r--r--. 1 root root 0 Sep 28 12:45 5
    lrwxrwxrwx. 1 root root 1 Sep 28 12:46 6 -> 1
    lrwxrwxrwx. 1 root root 1 Sep 28 12:46 7 -> 2
    lrwxrwxrwx. 1 root root 1 Sep 28 12:46 8 -> 3
    lrwxrwxrwx. 1 root root 1 Sep 28 12:46 9 -> 4

    As you can see there are many symlinks which exists in the test home directory. Now let us compress and copy to some other location





    NOTE: You need to compress the directory without "-h" argument as you can see below
    [root@test home]# tar -czvf test.tar.gz test/
    test/
    test/2
    test/4
    test/.gnome2/
    test/1
    test/.bash_history
    test/8
    test/.emacs
    test/5
    test/.bash_logout
    test/9
    test/10
    test/.bashrc
    test/.bash_profile
    test/.mozilla/
    test/.mozilla/extensions/
    test/.mozilla/plugins/
    test/6
    test/7
    test/3

    Move the zipped file to another location
    [root@test home]# mv test.tar.gz /tmp/
    [root@test home]# cd /tmp/
    While extracting the directory make sure you use "-h" argument to save the symlinks as they were while compressing
    [root@test tmp]# tar -xhzvf test.tar.gz
    test/
    test/2
    test/4
    test/.gnome2/
    test/1
    test/.bash_history
    test/8
    test/.emacs
    test/5
    test/.bash_logout
    test/9
    test/10
    test/.bashrc
    test/.bash_profile
    test/.mozilla/
    test/.mozilla/extensions/
    test/.mozilla/plugins/
    test/6
    test/7
    test/3
    [root@test tmp]# cd test

    Now let us verify if it worked
    [root@test test]# ls -l
    total 0
    -rw-r--r--. 1 root root 0 Sep 28 12:45 1
    lrwxrwxrwx. 1 root root 1 Sep 28 12:49
    10 -> 5
    -rw-r--r--. 1 root root 0 Sep 28 12:45 2
    -rw-r--r--. 1 root root 0 Sep 28 12:45 3
    -rw-r--r--. 1 root root 0 Sep 28 12:45 4
    -rw-r--r--. 1 root root 0 Sep 28 12:45 5
    lrwxrwxrwx. 1 root root 1 Sep 28 12:49 6 -> 1
    lrwxrwxrwx. 1 root root 1 Sep 28 12:49 7 -> 2
    lrwxrwxrwx. 1 root root 1 Sep 28 12:49 8 -> 3
    lrwxrwxrwx. 1 root root 1 Sep 28 12:49 9 -> 4

    So. the magic did happened. Let me know your success and failures

    Deepak Prasad

    is a techie and an author who is still trying to survive in this IT generation with very little knowledge he has on Linux/Unix, VMware, SAN Storage, Automation, networking etc

    You can follow him on Facebook or Google+

    Do you also have something to share here?

    Become an author and be a part of our GoLinuxHub Team, Click here for more information
    How to preserve Symbolic links with tar command in Unix/Linux How to preserve Symbolic links with tar command in Unix/Linux Reviewed by Deepak Prasad on Sunday, December 29, 2013 Rating: 5

    5 comments:

    1. I don't believe the -h has any impact on the tar -x.....
      It is applicable when creating the tar and basically replaces the sym link with the actual file the link was pointing to.

      ReplyDelete
      Replies
      1. Hello,

        Thanks for marking the mistake. That has been corrected

        Thanks
        Deepak

        Delete
    2. This works only with root or privileged user but not as normal user, how to make tar to do same as normal user

      ReplyDelete
      Replies
      1. I verified the same as normal user and I don't see any difference in the output.

        Please make sure that you use "h" argument while uncompressing the tar.

        Delete

    Powered by Blogger.