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.
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.
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
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
In that command, I used three options to create the tar archive:
cmeans "create archive".
vmeans "verbose", which tells
tarto print all the filenames as they are added to the archive.
tarthat the name of the archive appears next (right after these options).
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
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 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.
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
tar xzvf my-archive.tgz
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.
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.
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.
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.