Create a TAR Archive of a Directory

Contributor Icon Contributed by qmchenry  
Tag Icon Tagged: UNIX  

A common task in linux and UNIX operating systems is to create an archive of many files or directories in a single file. The tar command is one option for archiving and has many options and can do many things.


Wrapping an entire directory structure into a tar archive is extremely useful. This simple command will allow you to create a tar archive that contains a preserved hierarchy of directories.

In a simple example, you can create a file called archive.tar of all the files and subdirectories in the current directory with the following command:

tar cf archive.tar *

Note that this file is different from a zip archive commonly used in Windows environments because it is uncompressed.

 

8 Comments -


  1. dan said on June 17, 2009

    Thanks for saving my time with this posting.

  2. Giacomo Arru said on April 7, 2010

    thank you – my memory it’s going cracked

    just entered tar jcf filename.tar.bz2 /tmp

  3. Jhnmrtnpaolo said on August 12, 2010

    how to unarchive in unix,if the file is named a archive.tar.gz . . . . using a cat,gzip, tar. on one command line

  4. Guest said on September 8, 2010

    Jhnmrtnpaolo,
    Use this command: tar -zxvf archive.tar.gz

  5. guest said on September 8, 2010

    Jhnmrtnpaolo, you may also use -C option to type a directory you want to save the unarchived files to:
    tar -zxvf archive.tar.gz -C /directory_to_save

  6. Janak said on September 2, 2011

    thanks for the simple tip but very useful

  7. Cheryl Bates said on September 14, 2011

    How do I copy (using the tar command) all the files in a directory to tape without copying the sub directories. I just want the files.

    Thanks,
    Cheryl

  8. Quinn said on September 15, 2011

    Cheryl,

    Just add an n option to the command.. so

    tar cnf archive.tar *

    will grab just the files matching * and will not recursively dig into subdirectories.

 

RSS feed for comments on this post. TrackBack URL

Leave a comment -