Commands.page Logo

How to Update Tar Archive on Ubuntu Linux

This guide explains how to add new files or update existing ones within a tar archive on Ubuntu. You will learn the specific command-line flags required to modify tar balls without extracting the entire contents. By following these steps, you can efficiently manage your backup files and software packages directly from the terminal.

Append New Files to Tar Archive

To add new files to an existing uncompressed tar archive, use the --append or -r flag. This operation adds the specified files to the end of the archive regardless of whether they already exist inside it.

tar -rf archive.tar newfile.txt

In this command, -r tells tar to append, -f specifies the filename, archive.tar is your existing archive, and newfile.txt is the file you want to add. You can list multiple files to add them all at once.

Update Existing Files in Tar Archive

If you want to replace files within the archive only if the new version is newer than the stored version, use the --update or -u flag. This is useful for backups where you only want to store changed files.

tar -uf archive.tar modifiedfile.txt

The -u flag compares the modification times. If modifiedfile.txt on your system is newer than the version inside archive.tar, it will be updated. If it is older or the same age, the archive remains unchanged.

Limitations with Compressed Archives

It is important to note that you cannot directly append or update compressed archives such as .tar.gz or .tar.bz2 using these flags. The compression structure prevents modifying the internal data stream. To update a compressed archive, you must extract the contents, make your changes, and create a new compressed archive.

tar -xf archive.tar.gz
# Make changes to files
tar -czf archive.tar.gz folder/

Stick to uncompressed .tar files if you need to frequently update or append data without recreating the entire archive.