Thursday, September 27, 2012

How to update and replace files in a tar archive?


Question 1: How to update an existing tarball contents with newfile(s)?
Answer: We have to use the function ‘u’ to update newfile(s) to an existing tarball.

Question 2: How to replace an existing tarball contents with newfile(s)?
Answer: We have to use the function ‘r’ to replace newfile(s) to an existing tarball.


I am providing the definition of function update(u) and replace(c) as per solaris man page.

Function letters           Function
r                                  Replace. The named files are written at the end of  the tarfile
u                                  Update. The named files are written at the end  of  the tarfile  if  they are not already in the tarfile, or if they have been modified since last written to that tar-file.

Example:


[st39422@dios8:/home/st39422]#ls -l fileABC.tar Aiszipped.zip Biszipped.zip
-rw-r--r--   1 st39422  users    7486342 Aug  8 14:19 Aiszipped.zip
-rw-r--r--   1 st39422  users    11256018 Aug  8 14:19 Biszipped.zip
-rw-r--r--   1 st39422  users       8192 Aug  6 12:55 fileABC.tar

Creation of a tarball: AB.tar

[st39422@dios8:/home/st39422]#tar -cvf AB.tar Aiszipped.zip Biszipped.zip
a Aiszipped.zip 7311K
a Biszipped.zip 10993K
[st39422@dios8:/home/st39422]#

Contents of the created tarball: AB.tar

[st39422@dios8:/home/st39422]#tar -tvf AB.tar
-rw-r--r-- 48153/63002 7486342 Aug  8 14:19 2012 Aiszipped.zip
-rw-r--r-- 48153/63002 11256018 Aug  8 14:19 2012 Biszipped.zip
[st39422@dios8:/home/st39422]#

Updating a file(fileABC.tar) to the existing tar archive(AB.tar):

[st39422@dios8:/home/st39422]#tar -uvf  AB.tar  fileABC.tar
a fileABC.tar 8K
[st39422@dios8:/home/st39422]#

[st39422@dios8:/home/st39422]#tar -tvf AB.tar
-rw-r--r-- 48153/63002 7486342 Aug  8 14:19 2012 Aiszipped.zip
-rw-r--r-- 48153/63002 11256018 Aug  8 14:19 2012 Biszipped.zip
-rw-r--r-- 48153/63002   8192 Aug  6 12:55 2012 fileABC.tar
[st39422@dios8:/home/st39422]#

If you try to update the already available file in the archive, it will be added at the end of the archive; provided the file is modified. Otherwise it will be ignored.

[st39422@dios8:/home/st39422]#tar -uvf AB.tar fileABC.tar
[st39422@dios8:/home/st39422]#tar -tvf AB.tar
-rw-r--r-- 48153/63002 7486342 Aug  8 14:19 2012 Aiszipped.zip
-rw-r--r-- 48153/63002 11256018 Aug  8 14:19 2012 Biszipped.zip
-rw-r--r-- 48153/63002   8192 Aug  6 12:55 2012 fileABC.tar
[st39422@dios8:/home/st39422]#

Replace an existing file will always add the mentioned file at the end of the archive:

[st39422@dios8:/home/st39422]#tar -rvf AB.tar fileABC.tar
a fileABC.tar 8K
[st39422@dios8:/home/st39422]#tar -tvf AB.tar
-rw-r--r-- 48153/63002 7486342 Aug  8 14:19 2012 Aiszipped.zip
-rw-r--r-- 48153/63002 11256018 Aug  8 14:19 2012 Biszipped.zip
-rw-r--r-- 48153/63002   8192 Aug  6 12:55 2012 fileABC.tar
-rw-r--r-- 48153/63002   8192 Aug  6 12:55 2012 fileABC.tar
[st39422@dios8:/home/st39422]#

Replacing multiple files:
[st39422@dios8:/home/st39422]#tar -rvf AB.tar fileABC.tar fileABC.tar
a fileABC.tar 8K
a fileABC.tar 8K
[st39422@dios8:/home/st39422]#tar -tvf AB.tar
-rw-r--r-- 48153/63002 7486342 Aug  8 14:19 2012 Aiszipped.zip
-rw-r--r-- 48153/63002 11256018 Aug  8 14:19 2012 Biszipped.zip
-rw-r--r-- 48153/63002   8192 Aug  6 12:55 2012 fileABC.tar
-rw-r--r-- 48153/63002   8192 Aug  6 12:55 2012 fileABC.tar
-rw-r--r-- 48153/63002   8192 Aug  6 12:55 2012 fileABC.tar
-rw-r--r-- 48153/63002   8192 Aug  6 12:55 2012 fileABC.tar
[st39422@dios8:/home/st39422]#

Updating multiple files:
[st39422@dios8:/home/st39422]#tar -uvf AB.tar fileA fileB
a fileA 1K
a fileB 1K
[st39422@dios8:/home/st39422]#tar -tvf AB.tar
-rw-r--r-- 48153/63002 7486342 Aug  8 14:19 2012 Aiszipped.zip
-rw-r--r-- 48153/63002 11256018 Aug  8 14:19 2012 Biszipped.zip
-rw-r--r-- 48153/63002   8192 Aug  6 12:55 2012 fileABC.tar
-rw-r--r-- 48153/63002   8192 Aug  6 12:55 2012 fileABC.tar
-rw-r--r-- 48153/63002   8192 Aug  6 12:55 2012 fileABC.tar
-rw-r--r-- 48153/63002   8192 Aug  6 12:55 2012 fileABC.tar
-rw-r--r-- 48153/63002    227 Aug  6 12:42 2012 fileA
-rw-r--r-- 48153/63002    227 Aug  6 12:43 2012 fileB

3 comments:

Raaaj said...

hats off to your patience ;)

Felipe said...

but this causes the archive to grow, it isn't replacing the file, it just puts two files with the same name in the archive, extracts both of them, in chronological order, extracting the last one over top of all the others, causing it to 'appear' to replace the member in the archive. The archive will keep growing indefinately following this approach.

Another way is using 'tar --delete' to delete the member, and then 'tar --update' to put the file back into the archive. This will truly cause it to replace the file.

Patrick Co Eban said...


Networking Projects for Final Year CSE Students


The IEEE Network projects Networking Projects for Final Year CSE Students has direct impact on undergraduate and graduate student education and training. Final Year Engineering Students who are software developers can structure a project around building a network firewall application Final Year Project Centers in Chennai

JavaScript Training in Chennai

JavaScript Training in Chennai