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?

Suppose I want to copy my home directory to some other location and my home directory consists of few symlinks

# 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

 

Related Articles
How to unlink/delete a symbolic in Linux
How to extract files to different directory using tar in Unix/Linux
How to create Soft Link and Hard Link in Linux

6 thoughts on “How to preserve Symbolic links with tar command in Unix/Linux”

  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.

    Reply
  2. 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.

    Reply

Leave a Comment