Examples of the Linux tar command

Introduction

The name "tar" stands for "tape archive". As the name implies, in the old days it was a command that Unix administrators used to deal with tape drives. These days it's more often used to create compressed archives that can easily be moved around, from disk to disk, or computer to computer. One user may archive a large collection of files, and another user may extract those files, with both of them using the tar command.

Create an archive of a subdirectory

A common use of the tar command is to create an archive of a subdirectory. For instance, assuming there is a subdirectory named MyProject in the current directory, you can use tar to create an uncompressed archive of that directory with this command:

tar cvf MyProject.20090816.tar MyProject

where MyProject.20090816.tar is the name of the archive (file) you are creating, and MyProject is the name of your subdirectory. It's common to name an uncompressed archive with the .tar file extension.

In that command, I used three options to create the archive:

  • The letter c means "create archive".
  • The letter v means "verbose", which tells tar to print all the filenames as they are added to the archive.
  • The letter f tells tar that the name of the archive appears next (right after these options).

The v flag is completely optional, but I usually use it so I can see the progress of the command.

The general tar syntax of the tar command when creating an archive looks like this:

tar [flags] archive-file-name files-to-archive

Creating a compressed archive

You can compressed a tar archive with the gzip command after you create it, like this:

gzip MyProject.20090816.tar

This creates the file MyProject.20090816.tar.gz. But these days it's more common to create a compressed tar archive with one command, like this:

tar czvf MyProject.20090816.tgz MyProject

As you can see, I added the z flag there (which means "compress this archive with gzip"), and I changed the extension of the archive to .tgz, which is the common file extension for files that have been tar'd and gzip'd in one step.

Creating a compressed archive of the current directory

Many times you will want to create an archive of all files in the current directory, including all subdirectories. You can easily create this archive like this:

tar czvf mydirectory.tgz .

where the . refers to the current directory.

Writing an archive in a different directory

You may also want to write an archive like that previous example to a different directory, like this:

tar czvf /tmp/mydirectory.tgz .

Listing the contents of a tar archive

To list the contents of an uncompressed tar archive, just replace the c flag with the t flag, like this:

tar tvf my-archive.tar

This lists all the files in the archive, but does not extract them.

To list all the files in a compressed archive, add the z flag like before:

tar tzvf my-archive.tgz

That same command can also work on a file that was tar'd and gzip'd in two separate steps (as indicated by the .tar.gz file extension):

tar tzvf my-archive.tar.gz

I almost always list the contents of an unknown archive before I extract the contents. I think this is always good practice, especially when you're logged in as the root user.

Extracting an archive

To extract the contents of an archive, now just replace the t flag with the x ("extract") flag. For uncompressed archives the extract command looks like this:

tar xvf my-archive.tar

For compressed archives the extract command looks like this:

tar xzvf my-archive.tar.gz

or this:

tar xzvf my-archive.tgz

More information

You can find more tar command examples by searching this website. You can also type:

man tar

at your command line to get help on using the tar command.

Finally, if you have other favorite tar command uses, feel free to share them below in our comments section.

Really Good Precise and to

Really Good Precise and to the Point ... Please be consistent ... A mistake in the example of extracting tar file ... t is not replaced by x ... please correct it ...

Thanks, and thanks for

Thanks, and thanks for catching those problems, they have been corrected.

Thanks, this content is

Thanks, this content is extremely helpful...

Post new comment

The content of this field is kept private and will not be shown publicly.