Ways To Zip The Directory Structure of Unix

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 example.zip, you can use the following command:

zip -r example.zip example

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

ganesh@ganesh-desktop:~$ zip -r example.zip 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.tar.bz 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:
unzip zippedarchivename.zip

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 example.tar.bz
  3. If its simple archive thentar xvf example.tar
  4. If you just want to view the archive then use -t option.tar tvfj example.tar.bz

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 »


Leave a Reply

 Comment Form 


More articles from the category: Tips and Tricks

What is The Difference Between exec and xargs

There are very small things in linux based systems which often consfuse the users. Here in this article I’m going to discuss the specific use of find command and difference between exec and xargs.

Understanding The diff Command In Unix

It’s not an uncommon requirement for Unix system administrators to know the difference between two files. The diff command in Unix serves the purpose. Here I am going to discuss the diff command. It’s quite common but little understood command. I hope after reading this article, the Unix visitors will be able to understand the usage properly and benefit from it. Here you go…

How To Find Common Lines Between Two Text Files In Unix

Those working in Unix environment must be aware of the requirement to find common
lines between two Unix files. In this article I’m gonna introduce you to solve this problem.

How To Join Two Files Vertically

In Unix, sometimes we come across situations where we need to join two files’ output side by side vertically. This is also referred to as vertical joining of files. In such situations, paste command comes handy. With the help of this command you not only can vertically join the files, but insert some delimiter as well….

How To Recover A Superblock

If fsck or mount commands give errors then pretty good chances are there that the superblock is corrupt. The dd command comes to rescue. We know that the superblock resides in 31st block also. So, to recover that we use the following command: # dd count=1 bs=4k skip=31 seek=1 if=/dev/my_lv  of=/dev/my_lv count: How many blocks […]

Ways To Zip The Directory Structure of Unix

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.

How To Split or Cut A File Vertically(Column wise)

In Unix environment it’s often required to print specific columns from a file. If the rows of file are having some proper delimiters then other Unix command like awk come into picture and quite handy. But if say you want to print out the 5th character and then 7th to 16th character, then other commands fail and in such situations the cut command comes to rescue. Here is short primer of cut command.