Commands.page Logo

Tar Command Option to Follow Symlinks in Ubuntu

This article explains how to configure the tar utility in Ubuntu to archive the contents of symbolic links rather than the links themselves. It identifies the specific command-line flag required to dereference symlinks during creation and provides practical examples for immediate use.

By default, the tar command archives symbolic links as links, preserving the path reference rather than the actual file data. To change this behavior and archive the contents of the target file, you must use the -h or –dereference option. This tells tar to follow the symlink and include the underlying file in the archive.

Basic Usage

To create an archive while following symlinks, add the -h flag to your standard creation command.

tar -cvhf archive.tar /path/to/directory

In this example: * -c: Create a new archive. * -v: Verbose output. * -h: Follow symbolic links (dereference). * -f: Specify the archive file name.

Why Use This Option

Using the dereference option is essential when you need a complete backup of data regardless of file location. If you omit this flag, restoring the archive on a system where the linked files do not exist at the original paths will result in broken links. Always verify that following links does not create infinite loops within your directory structure.