Skip to content

Keep it small and sweet: creating archives using command line

December 19, 2009

So, next up in my living in command line is creating archives using command line. I know this is like a big jump but well it’s pretty straight forward and simple.

I used the wiki guide of ubuntu to learn this but going to explain a bit more probably. Though actually it’s pretty much what is there in the wiki page because it mentions various archive types and I’ll be dealing with the most common ones only.

So the command that we’ll be using to create archives is tar. The archives that I’ll be teaching you to create are tape archives, gunzip archives and bz2?

First up tape archives. These don’t really offer a great compression but they are the base to most other efficient archiving formats. Tape archives derive their name from the fact that they contents are “taped” together like when we put a lot of things in a box and tape them up. They were mainly used to avoid the often painful recursive copying of directories which often can cause problems (like it did for me when copying files to a VM from host).

So to actually help you do it hands on, create a new directory say test and put all the files you want to archive into it.

To create a tape archive of these, execute the following command from the parent directory of test
tar -cf myarchive.tar test/

You should see a file with the name myarchive.tar in the present working.

To extract a tape archive, execute
tar -xf myarchive.tar

You should find the directory test in the present directory.(Note: Remove the directory test before extracting)

Gunzip archives are an improvement over the tape archives since the offer better compression compared to them.

To create a gunzip archive of the above files,
tar zcvf myarchive.tar.gz test/

To extract a gunzip archive,
tar zxvf myarchive.tar.gz

bzip2 archives are a much better improvement over the gunzip archives since they offer much more compression but require that much more processing power than gunzip archives.

To create a bzip2 archive,
tar jcvf myarchive.tar.bz2 test/

To extract a bzip2 archive
tar jxvf myarchive.tar.bz2

If you notice that -c is the option to create archive and -x is to extract the archive. This is common to all formats. Also -j is for bzip2 archives and -z is for gunzip archives.

Well I hope you understood how to create archives using command line. Till next time, keep learning. Ciao!

Leave a Comment

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

%d bloggers like this: