Commands.page Logo

Ubuntu Tar Flag to Archive Symbolic Links as Links

This article provides a concise guide on managing symbolic links during archive creation in Ubuntu using the tar command. It specifically identifies the flag required to preserve symbolic links as links instead of archiving their target files, ensuring your backup structure remains accurate.

When using the tar command in Ubuntu, the default behavior is to archive symbolic links as links. However, to explicitly ensure that symbolic links are stored as links rather than dereferencing them to their target files, you should use the --no-dereference flag. This is particularly important if you want to guarantee this behavior in scripts or counteract any aliases that might default to dereferencing.

The opposite flag is -h or --dereference, which follows symbolic links and archives the actual files they point to. Using this option when you intend to keep links will result in larger archives and a loss of the original link structure.

To create a tar archive while preserving symbolic links, use the following command structure:

tar --no-dereference -cvf archive_name.tar /path/to/directory

In this example, the --no-dereference option ensures that any symbolic links within /path/to/directory are saved as links. The -c flag creates a new archive, -v enables verbose output to show progress, and -f specifies the filename of the archive. By avoiding the -h flag and optionally including --no-dereference, you maintain the integrity of your directory structure.