How to Preserve Symbolic Links in Tar Command

Last updated: December 6, 2022

How to preserve symbolic links (symlinks) especially when you tar the files is explained below. We all know how to use tar command. The following steps preserve links when you archive.

Step 1. Create the Archive with only Links

What you need to remember is that when tar archives a symbolic link, it writes a record to the archive naming the target of the link

tar -cvf archive.tar /backupfolder

Step 2. Archive with Linked Files

When you use -h with -c, it causes tar to archive the files symbolic links point to, instead of the linking themselves. When this option is used, when tar encounters a symbolic link, it will archive the linked-to file, instead of simply recording the presence of a symbolic link.

find /backupfolder -type l | xargs tar -cvfh linkedfiles.tar

Step 3. Extract

This gets you back to exactly how your symlink and its files were:

tar -xvf archive.tar
tar -xvfh linkedfiles.tar

About The Author

Bobbin Zachariah

Bobbin Zachariah

Bobbin is a seasoned IT professional with over two decades of experience. He has excelled in roles such as a computer science instructor, Linux system engineer, and senior analyst. Currently, he thrives in DevOps environments, focusing on optimizing efficiency and delivery in AWS Cloud infrastructure. Bobbin holds certifications in RHEL, CCNA, and MCP, along with a Master's degree in computer science. In his free time, he enjoys playing cricket, blogging, and immersing himself in the world of music.



Please add comments below to provide the author your ideas, appreciation and feedback.

Leave a Reply

Leave a Comment