Linux tar command examples

Unix/Linux tar command FAQ: Can you share some Linux tar command examples?

Sure. I'm a big believer in learning Unix/Linux commands by seeing examples, and I know from experience it will really help to see some Linux tar command examples. But first, a brief bit of background information.

tar command background

The name "tar" stands for "tape archive". As that name implies, in the old days it was a command that Unix administrators used to deal with tape drives. Where we now use the Linux tar command to create a tar file, we used to tell it to write the tar archive to a device file (in /dev).

These days the Linux tar command is 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.

1) Create a tar archive of a subdirectory

A common use of the Linux tar command is to create an archive of a subdirectory. For instance, assuming there is a subdirectory named MyProjectin 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.taris the name of the archive (file) you are creating, and MyProjectis the name of your subdirectory. It's common to name an uncompressed archive with the .tarfile extension.

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

  • The letter cmeans "create archive".
  • The letter vmeans "verbose", which tells tarto print all the filenames as they are added to the archive.
  • The letter ftells tarthat the name of the archive appears next (right after these options).

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

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

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

2) List the contents of a tar archive

To list the contents of an uncompressed tar archive, just replace the cflag with the tflag, 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 zflag 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.gzfile 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.

3) Extracting a tar archive

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

tar xvf my-archive.tar

For compressed archives the tar extract command looks like this:

tar xzvf my-archive.tar.gz

or this:

tar xzvf my-archive.tgz

4) Linux tar command with gzip - Creating a compressed archive

You can compress 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 gzip'd tar archive with one tar 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.

5) Creating a compressed archive of the current directory

Many times when using the Linux tar command 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 .

In this tar example, the '.'  at the end of the command is how you refer to the current directory.

6) Creating an archive in a different directory

You may also want to create a new tar archive like that previous example in a different directory, like this:

tar czvf /tmp/mydirectory.tgz .

As you can see, you just add a path before the name of your tar archive to specify what directory the archive should be created in.

More Linux tar command information

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

man tar

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

Finally, if you have other favorite tar command examples, 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...

Nice article

I found this quite precise and just what I was looking for....

Thanks.. Very clear post :)

Thanks.. Very clear post :)

Thank You

I find it very helpful

Thanks alot

Very neat and simple explanation for beginners too..... Good Job.... keep it up.

Thx a lot

Great post. Very helpful, saved lots of time

Post new comment

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