Linux tar command examples
Unix/Linux tar command FAQ: Can you share some Linux tar command examples?
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 the 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) Linux tar command - Create an 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 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 tar archive:
- The letter
cmeans "create archive". - The letter
vmeans "verbose", which tellstarto print all the filenames as they are added to the archive. - The letter
ftellstarthat 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 syntax of the tar command when creating an archive looks like this:
tar [flags] archive-file-name files-to-archive
2) 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.
3) 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.
4) tar command example - 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.
5) tar list example - 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.
6) tar extract example - extracting an 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
or this:
tar xzvf my-archive.tgz
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