2

Good morning,

I have a tar compressed archive tar.bzip2 (created using tar jcf), which contains a file named data.txt (1). On the other side, I have another file also named data.txt (2).

Is there a way to append file (2) to the end of file (1) without untar bz2 archive, append file and then tar it back again?
By the way, I already did the three steps, I'm asking just for curiosity (or 'out of curiosity'... not sure about this expression in English).

I've been looking for the right option, but I cannot find it out:

  • -r option tells me 'abort', I guess because I tried to append a file which already exists in archive.
  • -u cannot update compressed archives, although I think it will only replace (1) by (2).
  • -A also tells me 'abort'. I'm not really sure what this option does. It seems to add the files in one archive to another existing archive. So it's not what I want.

I'm using GNU tar 1.15.1 over Linux.

Note:

I think I didn't explain it well enough, so I better use an example:
I have a file named data.tar.bz2 which contains a file named data.txt (1). Its contents could be

A

I have another file also named data.txt (2). Its contents could be

B

My purpose is that the file contained in data.tar.bz2 has both (1) and (2) file contents in one file named, of course, data.txt:

A
B

In other words do this using one command, without decompressing, untar, create temporal directories, etc...:

mkdir $tempDir
tar jxf data.tar.bz2 -C $tempDir
cat data.txt >> $tempDir/data.txt
tar jcf data.tar.bz2 -C $tempDir data.txt 
rm -r $tempDir 

Yes, I want to append, but -r option won't work because, if I'm not wrong, it appends files to archive, but it does not append the contents of an existing file in the archive. Hope this clarifies... not sure at all xD

  • Have you read the man page for tar? It doesn't seem so. Anyway, tar files are not designed to be manipulated in this way, you can stop trying, unfortunately. – wurtel Dec 17 '14 at 13:02
  • 1
    I don't understand why you say that. Not only I read man page, but also manual, before asking. Not all, of course, just the section about 'append'. I didn't find out the answer (or maybe I didn't understand), and that's I asked. Can you tell me what's wrong with this question? – Albert Dec 17 '14 at 13:22
  • @wurtel, I read my previous comment and it seems a little defensive, and it was not my intention. Sorry about that, but I can't edit it. – Albert Dec 17 '14 at 13:38
  • My comment was because you wrote "I'm not really sure what this option does" about -A, you could have added the description with a statement that you don't understand that description, for example. It's always useful to give as much info as you can. No problem about your 1st comment BTW. I have never viewed tar archives as suitable for updating, use zip for that. Maybe you might have more success with non-compressed files (as I now see the answer below states). – wurtel Dec 17 '14 at 14:02
1

If data.txt is the only file in the tar archive, you could append directly, simply by not using tar at all:

$ echo Hello | bzip2 > data.txt.bz2
$ echo Stack | bzip2 >> data.txt.bz2
$ echo Exchange | bzip2 >> data.txt.bz2
$ bunzip2 < data.txt.bz2
Hello
Stack
Exchange
  • Yes, data.txt is the only file in archive. I'll try this. Although is not exactly what I wanted, it really has no sense to use tar to pack one only file. – Albert Dec 17 '14 at 14:40
1

You can append files to a non-compressed tar archive with the --append switch, for example:

$ tar --append --file=tarfile.tar data.txt

Beware the consequences of adding multiple files of the same name to an existing archive :

http://www.gnu.org/software/tar/manual/html_node/append.html

You cannot, as far as I am aware, append or update files in a compressed archive without first decompressing it.

  • Thanks, but --append is like -r option. It's not what I'm looking for. xD – Albert Dec 17 '14 at 13:24
  • Your question says explicitly Is there a way to append file (2) to the end of file, now you say you don't want to append? What exactly do you want to do? – wurtel Dec 17 '14 at 14:04
  • I edited original question to add an example. Better? – Albert Dec 17 '14 at 14:36
1

I can think only of the Archivemount utility to do that.

1) Install archivemount (if its not already available on your machine). Its available in the EPEL repo.

2) Use the archivemount command to mount your tar file on any of the directories in your machine.

archivemount xyz.bzip2 /path/to/a/directory/

Once mounted, you will be able to browse through the archive as if it is a regular directory. From there, you can edit/update or append content to your file which is in the archive.

Here you have more details about using archivemount and below is a snippet from my server when I archivemounted a bzip2 file.

[root@server usr]# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/xvda1       15G  9.0G  5.1G  64% /
none            245M     0  245M   0% /dev/shm
[root@server usr]# archivemount puppet_codes.bzip2 /usr/local/archtest/
[root@server usr]# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/xvda1       15G  9.0G  5.1G  64% /
none            245M     0  245M   0% /dev/shm
archivemount   1000G     0 1000G   0% /usr/local/archtest
[root@server usr]# ls /usr/local/archtest
puppet_codes
[root@server usr]# cat /usr/local/archtest/puppet_codes/emaillist.txt
sreewave@example.com
sreewave@example.org
[root@server usr]#

The following is the package I have installed on my server.

[root@server ~]# rpm -qi archivemount-0.8.1-2.el6.x86_64
Name        : archivemount                 Relocations: (not relocatable)
Version     : 0.8.1                             Vendor: Fedora Project
Release     : 2.el6                         Build Date: Sun Oct 13 17:06:30 2013
Install Date: Wed Dec 17 13:45:01 2014         Build Host: buildvm-04.phx2.fedoraproject.org
Group       : System Environment/Libraries   Source RPM: archivemount-0.8.1-2.el6.src.rpm
Size        : 70774                            License: LGPLv2+
Signature   : RSA/8, Sun Oct 13 18:43:34 2013, Key ID 3b49df2a0608b895
Packager    : Fedora Project
URL         : http://www.cybernoia.de/software/archivemount/
Summary     : FUSE based filesystem for mounting compressed archives
Description :
Archivemount is a piece of glue code between libarchive and FUSE. It can be
used to mount a (possibly compressed) archive (as in .tar.gz or .tar.bz2)
and use it like an ordinary filesystem.
  • I'm afraid I can't do that. I have no permissions to install new software. Thanks anyway, I'll keep this to myself. – Albert Dec 17 '14 at 14:43

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.