Posts Tagged ‘how to zip directory structure of unix’

Ways To Zip The Directory Structure of Unix

Tuesday, April 7th, 2009

Often its the requirement of system administrators to move the directories in between the servers. Also the confusion arises which utility to use for the best results. Normally zip, tar, cpio utilities are used the most. I’m writing a short introduction of zip and tar.

Let’s take an example directory: example. The files and directories are shown as below:

ganesh@ganesh-desktop:~$ ls example/

a  b  c  d  e  f  g  h

The Zip Method

To create the zip file named, you can use the following command:

zip -r example

The -r option specifies to move recursively the directory structure of the directory example.

ganesh@ganesh-desktop:~$ zip -r example/

adding: example/ (stored 0%)
adding: example/d (stored 0%)
adding: example/e (stored 0%)
adding: example/f (stored 0%)
adding: example/c/ (stored 0%)
adding: example/c/d (stored 0%)
adding: example/c/l (stored 0%)
adding: example/c/e (stored 0%)
adding: example/c/g (stored 0%)
adding: example/c/a (stored 0%)
adding: example/b/ (stored 0%)
adding: example/b/w (stored 0%)
adding: example/b/k (stored 0%)
adding: example/b/c (stored 0%)
adding: example/g (stored 0%)
adding: example/h (stored 0%)
adding: example/a/ (stored 0%)
adding: example/a/l (stored 0%)
adding: example/a/k (stored 0%)
adding: example/a/m (stored 0%)

The tar command
The tar command has been my all time favourite. Also once you get used to it, you won’t find it difficult to use it. Here are few uses to it.

  1. You can view the contents of archive without untarring the archive.
  2. You can retain the permissions.
  3. You can retain the directory structures.
  4. You have the facility to exclude files.
  5. You can create the archive and zip/gzip/bzip it on the fly.
  6. And so on…..

Here are examples:

tar cvf example.tar example
c: Means create the archive.
v: Means verbose.(v is small)
f: Means create the file archive as named.

At the end you’ll name the directory to be archived.

ganesh@ganesh-desktop:~$ tar cvf example.tar example/

ganesh@ganesh-desktop:~$ ls example.tar

Let’s say you want to gzip the archive as well:

You can run:
tar czvf example.tgz example
z: stands for gzip archive format.

Now let’s say you want to gzip the archive:

You can run:
tar cjfv example
j: stands for bzip archive format.

Now, let’s say you want to archive a directory. You want to have same permissions as at present, same directory path values(absolute paths) then you’ll use:

ganesh@ganesh-desktop:~$ tar -pPczvf example1.tgz example
-p: Permssions should not change.
-P: The Pathes should not change. Means don’t remove starting / from the file names.

Now restoring files from the archive.

Restoring from zip archive:

Restoring from tar archive:

  1. If the archive is tgz format then:tar xvfz example.tgz
  2. If the archive is bz format then:tar xvfj
  3. If its simple archive thentar xvf example.tar
  4. If you just want to view the archive then use -t option.tar tvfj

That’s all for this session. Keeping in view the importance of tar command, I’ll be writing full turorial on tar some day.

Tags: , , , , ,
Posted in Tips and Tricks | No Comments »