38

I have created a tar.gz file (using the GUI, not the command line). How can I update it with a command that new files are added and modified files are updated, too?

1
34

You'd normally use the -u flag for this. From tar's man page:

 -u, --update
       only append files newer than copy in archive

so this sequence will do what you need:

# First create the tar file. It has to be UNCOMPRESSED for -u to work
tar -cvf my.tar some-directory/
# ... update some files in some-directory
# ... add files in some-directory
# Now update only the changed and added files
tar -uvf my.tar some-directory/
# Compress if desired
gzip my.tar

For a slightly more detailed look, see here: http://www.gnu.org/software/tar/manual/html_node/how-to-update.html

1
  • 6
    OP was asking about updating compressed archive which can't be updated. – QkiZ Jun 17 '19 at 9:03
26

Solution / Workaround

You can not update compressed TAR archive (.tar.gz) in one step. But, if you have enough free space you can do this:

  1. Extract .tar file from .tar.gz file:

    gunzip filename.tar.gz

  2. Update uncompressed .tar file with tar -u command:

    tar -uf filename.tar new_file

  3. Compress the updated .tar file:

    gzip filename.tar

Speedup

If you have multi-core CPU, I recommend to use pigz instead of gzip for extract and create .gz files. (pigz is a multi-threaded implementation of gzip)

Simply replace gzip/gunzip commands to pigz/unpigz.

Related manuals

6

If you want to update a particular file in *.tar.gz file, just do the following:

Enter vi from where the tar file is available

/home>vi

For eg., if you want to modify simple.tar.gz which is under /home/test/ directory the:

/home/test>vi

And in the empty editor enter :n simple.tar.gz and press Enter you will get the list of folders and files move the cursor where u want to modify And click Enter. It will shown the vi editor of the particular file. Then i option to change the file. After the successful change. Press Esc key and choose :wq to write and quit the file. Then use :q to come out of the file list.

2
  • What do you mean "Then i option to change the file"? It's a binary file inside, how are you supposed to change it in VI? It's not a hex editor. Even if it was, the question is how do you replace a file inside a tgz file. – frakman1 Jan 17 '20 at 17:37
  • @frakman1 Vi and Vim evidently allow in-place editing; thanks to this answer I was just able to update a script in a tar.gz file, which is exactly what I came here for (although I found directly opening the archive with either Vim or Vi and just selecting the file worked without needing to use any Vi(m) commands). Might not strictly answer OP's question, but it helped me. I'd recommend you check someone's answer does what they say next time before coming out with comments like 'how are you supposed to change it in VI? It's not a hex editor.' – chrBrd Jan 21 at 0:24
1

If you are going to do this repeatedly, an optimization can be:

if [[ -f my.tar.gz ]]; then
    if [[ ! -f my.tar ]]; then
        echo 'No tar, unzipping tar.gz'
        gunzip my.tar.gz
    fi
    tar -uvf my.tar file1 file2 file_new
    gzip -fk my.tar  # This keeps a copy of the tar.
    # -f --force overwrite existing .tar.gz
    # -k --keep Keep the input file (.tar file)
else
    tar -cvzf my.tar.gz file1 file2
fi

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.