Wednesday, April 27, 2011

How to Create a TAR File in Linux , How to decompress .tar.bz2 file and How Can I Do Archiving With Tar ?

Create a TAR File in Linux
Tape archive (TAR) files originally were used in Linux and Unix environments to back up data to tape. Today, TAR files are used more commonly to store archives of files and directories in one single file rather than to tape. They function similarly to the ZIP file format in the Windows environment. TAR files are used frequently in Linux as a single file archive, so it's ideal knowing how to create one.
Instructions

    • 1
      Run a command line window. In most Linux graphical user interface (GUI) environments, a command line window is run by clicking on the desktop computer icon.
    • 2
      Go to the directory where you want to add the TAR file. For example, if the files are in /export/home/user, the command to go to that directory would be "cd /export/home/user."
    • 3
      Create a TAR file. The command is: "tar -cvf myfile.tar filename1 filename2," where myfile.tar is name of the TAR file.
----------

How Can I Do Archiving With Tar?

The tar Command The tar (tape archive) command bundles a bunch of files together and creates an archive (commonly called a tar file or tarball) on a tape, disk drive, or floppy disk. The original files are not deleted after being copied to the tar file.
 

To create an archive using tar, use a command like this, which bundles all the files in the current directory that end with .doc into the alldocs.tar file: tar cvf alldocs.tar *.doc
Here's a second example, which creates a tar file named panda.tar containing all the files from the panda directory (and any of its subdirectories):
tar cvf panda.tar panda/
In these examples, the c, v, and f flags mean create a new archive, be verbose (list files being archived), and write the archive to a file. You can also create tar files on tape drives or floppy disks, like this:
tar cvfM /dev/fd0 panda Archive the files in the panda directory to floppy disk(s).
tar cvf /dev/rmt0 panda Archive the files in the panda directory to the tape drive.

The /dev/fd0 entry is Linux-ese for "floppy drive zero" (your A drive under DOS), and /dev/rmt0 means "removable media tape zero," or your primary tape drive. The M flag means use multiple floppy disks--when one disk is full, tar prompts you to insert another.
To automatically compress the tar file as it is being created, add the z flag, like this:
tar cvzf alldocs.tar.gz *.doc
In this example, I added the .gz suffix to the archive file name, because the z flag tells tar to use the same compression as the gzip command.
To list the contents of a tar file, use the t (type) flag in a command, like this:
tar tvf alldocs.tar List all files in alldocs.tar.
To extract the contents of a tar file, use the x (extract) flag in a command, like this:
tar xvf panda.tar Extract files from panda.tar.
This will copy all the files from the panda.tar file into the current directory. When a tar file is created, it can bundle up all the files in a directory, as well as any subdirectories and the files in them. So when you're extracting a tar file, keep in mind that you might end up with some new subdirectories in the current directory.
We've used several different flags in the sample tar commands so far. Here's a list of the most common flags:
c Create a new archive.
t List the contents of an archive.
x Extract the contents of an archive.
f The archive file name is given on the command line (required whenever the tar output is going to a file)
M The archive can span multiple floppies.
v Print verbose output (list file names as they are processed).
u Add files to the archive if they are newer than the copy in the tar file.
z Compress or decompress files automatically.
For more information on the tar command, see the tar manual.
===============================================

How to decompress .tar.bz2 file

Normally, I would bunzip the file first, then untar it next. But I found something after googling for a shortcut: it can all be done with just one tar command.

$tar -xjvf example.tar.bz2

Notice that, we use j flag to decompress bzip2 file and z flag for gzip file.

No comments:

Post a Comment