Tuesday, October 2, 2012

8 Tar Examples You Should know

Hey readers,

Today i'm gonna show you some tar command examples.

When created, tar was used for data archiving on tapes.
today we use tar to collect different kinds of files together. the tar files themselves also know as tar balls

Lets start with simple example:

here the content of the directory I use for this example

total 0
-rw-r--r--. 1 root root 0 Sep 30 16:13 file1
-rw-r--r--. 1 root root 0 Sep 30 16:13 file2
-rw-r--r--. 1 root root 0 Sep 30 16:13 file3

1. Create Tar File

tar  -cvf  all.tar  file1  file2  file3

In this example we creating tar file with the following flags:

c - create
v - verbose. gives us more outpout about the process of creating the tar file
f - file (instead of tape for example)

all.tar will be our new tar file (tarball) which include the rest of files - file1, file2 and file3

we could use long options like create verbose and files and we also could separate them: -c -v -f

2. List the contents of a tar file

tar  -tf  all.tar

The -t option (which means table of contents) will show us which files are included in a specific tar file.
in this example the output will be:


for long list, you will might want to use the -v option (verbose)

3. Create compressed tar archive- gzip,bz2 

for gzip:

tar   zcvf  all_files.tgz  file*
This will create an tar archive and compress is with gzip. Its the same as tar.gz file.
the z here means compress it with gzip

for bzip2:

tar   jcvf  all_files.tbz file*
Same example, only this time we using bzip2 compression. Its the same sa tar.bz2 file.
the j means use bzip2

4. Untar a tar file

I find myself more often extract files from tar files than create them. Here how you should extract files from the tar file "all.tar"

tar  xvf  all.tar
You could use the -C option for diffrent extract location.

5. Append files to tar file

You can always update your tar file by adding new files to it.
Suppose we created new file called "luigi". lets add "luigi" to tar file called "mario"

tar  rvf  mario.tar  luigi
We used the option -r to append the file (you can use the long option, --append).

6. Move entire directory tree with tar

This is very popular usage of tar. Lets see how it looks:

tar  cf  -  * |  (cd  /some_dir  && tar xf - )
1. Creates tar of your current directory (You can choose any path you want)
2. Opening new sub shell and changing to the destination directory
3. Extracts the tar from first stage in destination directory

*note: you can just use the -C option. but in older versions of tar this option does not exists

7. Exclude files when creating tar

Lets assume we got anther file in our directory that named file101
We want that every file in our directory will archived except file101:

tar  cvf  all_files.tar  --exclude='file101'  *
You can also use list of files with -X option

8. Extract specific file from tarball

Sometimes you'll want to extract one particular file from a tar file.
you can just specify the file name to get that done:

tar  xvf  all_files.tar  file1 
This command will extract only file1 from the tar.

That's it for this post.
Hope you'll find this useful.


Post a Comment