Table of Contents
- tar command background
- 1) Create a tar archive of a subdirectory
- 2) List the contents of a tar archive
- 3) Extracting a tar archive
- 4) Linux tar command with gzip - Creating a compressed archive
- 5) Creating a compressed archive of the current directory
- 6) Creating an archive in a different directory
- More Linux tar command information
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
1) Create a tar 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
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 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
tarthat the name of the archive appears next (right after these options).
v flag 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-archiveBack to top
2) List 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.Back to top
3) Extracting a tar archive
To extract the contents of a Linux tar 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 tar extract command looks like this:
tar xzvf my-archive.tar.gz
tar xzvf my-archive.tgzBack to top
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:
This creates the file
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.Back to top
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.Back to top
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:
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.Back to top
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 catching those problems, they have been corrected.
Thanks, this content is extremely helpful...
I found this quite precise and just what I was looking for....
Thanks.. Very clear post :)
I find it very helpful
Very neat and simple explanation for beginners too..... Good Job.... keep it up.
Great post. Very helpful, saved lots of time