Append files to a existing tar file.

Hi all,

I want to check whether tar file exists in the directory or not. If tar file exists in the directory then I want to append the files to it.

I am using the below command to tar files if the file does not exist.

tar zcvf <tar file name> <Files to append>

However, if want to append some more files to the existing tar file with above command, files in the tar are getting deleted.

I tried using the below command but I am getting error as below.
"tar: You may not specify more than one `-Acdtrux' option"

tar zcvfr <tar file name> <additional files to append>

Request you to provide the code for file check and tar command if tar file exists.

Thanking you in advance.
Nagaraja Akkivalli.

Thanks and Regards
Nagaraja Akkivalli.

I'd think you want 'u' (update existing) instead of 'c' (create new) in your flags for tar.

As a little aside, you may also want to drop the z flag and pass the tar through a compression program of your choice - that way it'll work with non-gnu versions of tar and/or opens up the option to use better compression like bzip.

1 Like

Thanks Smiling Dragon for you response..!!

Let me explain it clearly....

Say, tar ABC.tgz has a.dat, b.dat and c.dat in it. If I want to add one more file(d.dat) to it then tar should not overide the existing file content. After taring 4th file then it should have all 4 (a.dat, b.dat and c.dat, d.dat) files.

Can I assume "update existing" does the same as I explained above?

Can you plz share the piece of code if you have or can you point to link where I can get this?

Thanks and Regards
Nagaraja Akkivalli

One more update.

I used below command if tar file existing.

tar rvf <tar file name> <additional files to append> , 

tar command was successful but when I untarred it, tar had only one file which was tarred first.

Please see the below code.

[user@dbname processed 04:31:05 AM]$tar zcvf FileName122011.tgz FileName_20110325101.dat
FileName_20110325101.dat
[user@dbname processed 04:31:05 AM]$tar rvf FileName122011.tgz FileName_20110325102.dat
FileName_20110325102.dat
[user@dbname processed 04:31:05 AM]$ls -ltr For*
-rwxrwSrwt 1 root root 402 Jul 16 02:48 FileName_20110519054238_0.dat
-rwxrwSrwt 1 root root 402 Jul 16 02:48 FileName_20110519053824_0.dat
-rwxrwSrwt 1 root root 415 Jul 16 02:48 FileName_20110519EP_vaL_2.dat
-rwxrwSrwt 1 root root 608 Jul 16 02:48 FileName_20110602031917_0.dat
-rwxrw-r-x 1 root root 416 Jul 17 11:42 FileName_20110325101.dat
-rwxrwSr-t 1 root root 416 Jul 17 11:51 FileName_20110325102.dat
-rwxrwxrwx 1 root root 10618 Jul 17 11:53 FileName122011.tgz
[user@dbname processed 04:31:05 AM]$rm -rf FileName_20110325102.dat
[user@dbname processed 04:31:05 AM]$rm -rf FileName_20110325101.dat
[user@dbname processed 04:31:05 AM]$ls -ltr For*
-rwxrwSrwt 1 root root 402 Jul 16 02:48 FileName_20110519054238_0.dat
-rwxrwSrwt 1 root root 402 Jul 16 02:48 FileName_20110519053824_0.dat
-rwxrwSrwt 1 root root 415 Jul 16 02:48 FileName_20110519EP_vaL_2.dat
-rwxrwSrwt 1 root root 608 Jul 16 02:48 FileName_20110602031917_0.dat
-rwxrwxrwx 1 root root 10618 Jul 17 11:53 FileName122011.tgz
[user@dbname processed 04:31:05 AM]$tar zxvf FileName122011.tgz
FileName_20110325101.dat
gzip: stdin: decompression OK, trailing garbage ignored
tar: FileName_20110325101.dat: Cannot utime: Operation not permitted
tar: Child returned status 2
tar: Error exit delayed from previous errors

This is how I did it.

If tar file does not exists use below command.
tar -cvf <tar-file-name.tar> < files to be added>

If tar file exists then use.
tar -rf <tar-file-name.tar> < files to be append>

Thanks
Nagaraja