Archiving with tar

Creating tar file


adding file to existing tar

update changes to previously compressed directory

extract and list tar contents


For Folder

[ahmad@SabryCentos01 /]$ sudo tar -cvf /home/hamada/etc.tar /etc

[ahmad@SabryCentos01 hamada]$ sudo tar -cvf etc.tar /etc/

[ahmad@SabryCentos01 hamada]$ sudo gzip etc.tar

For Files is the same:

[ahmad@SabryCentos01 ~]$ sudo tar -cjvf /home/magic/locale.conf.tar.gz locale.conf


[ahmad@SabryCentos01 ~]$ sudo tar -cjvf /home/magic/locale.conf.tar.bz2 locale.conf


Use compression(gzip or bzip2):

While using tar you can add:

-z (gzip) or -j (bzip2) to compress

-> Zip while compresing:

[ahmad@SabryCentos01 hamada]$ sudo tar -cvjf /home/hamada/etc.tar.bz2 /etc/

Watch out the sequence of j before f

-> Zip after compressing:


[ahmad@SabryCentos01 hamada]$ gzip etc.tar


Decompress(gunzip and bunzip2):

Using tar:

sudo tar -xvf etc.tar.gz

Using gunzip

[ahmad@SabryCentos01 magic]$ gunzip etc.tar.gz


tar cvf system-release.tar system-release

Then adding:


So The alternatives.tar is made from alternatives directory and has above files beneath.

Now moving ntp directory to the directory alternatives:

[ahmad@SabryCentos01 ~]$ mv ntp alternatives

Now updating the existing tar file with new changes:

[ahmad@SabryCentos01 ~]$ tar uvf alternatives.tar alternatives

Listing again the contents:

[ahmad@SabryCentos01 ~]$ tar tvf alternatives.tar



Single file

Same directory

[ahmad@SabryCentos01 hamada]$ sudo tar xvf etc.tar.bz2 etc/exports

Different directory

[ahmad@SabryCentos01 hamada]$ sudo tar xvf etc.tar.gz -C /home/ahmad/tamam/ etc/hosts.ori


OR all contents:

[ahmad@SabryCentos01 etc]$ sudo tar xvf etc.tar.gz -C /home/ahmad/cervantes/

List Contents:

[ahmad@SabryCentos01 hamada]$ tar tvf etc.tar.gz |more


About Ahmad Sabry ElGendi
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s