Linux tar command examples


The tar command combines multiple files into a single large file. It is separate from the compression tool, so it allows you to select which compression tool to use or whether you even want compression. In addition, tar is able to read and write to devices, thus making it a good tool for backing up to tape devices.

Although the name of the tar program includes the word “tape,” it isn’t necessary to read or write to a tape drive when creating archives. In fact, you’ll rarely use tar with a tape drive in day-to-day situations (backups aside). The reason it was named tar in the first place was that when it was originally created, limited disk space meant that tape was the most logical place to put archives.

Typically, the -f option in tar would be used to specify the tape device file, rather than a traditional UNIX file. You should be aware, however, that you can still tar straight to a device.

The syntax for the tar command is

[[email protected] ~]$ tar option ... filename ...

Some of the options for the tar command are shown here:

Option for tar Description

-c Creates a new archive
-t Views the contents of an archive
-x Extracts the contents of an archive
-f Specifies the name of the file (or device) in which the archive is located
-v Provides verbose descriptions during operations
-j Filters the archive through the bzip2 compression utility
-z Filters the archive through the gzip compression utility

In order to see sample usage of the tar utility, first create a folder called junk in the PWD that contains some empty files named 1, 2, 3, 4. Type

[[email protected] ~]$ mkdir junk ; touch junk/{1,2,3,4}

Now create an archive called junk.tar containing all the files in the folder called junk that you just created by typing

[[email protected] ~]$ tar -cf junk.tar junk

Create another archive called 2junk.tar containing all the files in the junk folder, but this time, add the -v (verbose) option to show what is happening as it happens. Enter the following:

[[email protected] ~]$ tar -vcf 2junk.tar junk

To create a gzip-compressed archive called 3junk.tar.gz containing all of the files in the junk folder and to show what is happening as it happens, issue this command:

[[email protected] ~]$ tar -cvzf 3junk.tar.gz junk

To extract the contents of the gzipped tar archive created here and be verbose about what is being done, issue the command:

[[email protected] ~]$ tar -xvzf 3junk.tar.gz

The tar command is one of the few Linux/UNIX utilities that cares about the order with which you specify its options. If you issued the preceding tar command as # tar -xvfz 3junk.tar.gz, the command will fail because the -f option was not immediately followed by a filename.

If you like, you can also specify a physical device to tar to and from. This is handy when you need to transfer a set of files from one system to another and for some reason you cannot create a file system on the device. (Or sometimes, it’s just more entertaining to do it this way.) To create an archive on the first floppy device (/dev/fd0), you would enter this:

[[email protected] ~]$ tar -cvzf /dev/fd0 junk

The command tar -cvzf /dev/fd0 will treat the disk as a raw device and erase anything that is already on it.

To pull that archive off of a disk, you would type

[[email protected] ~]$ tar -xvzf /dev/fd0


In case of any ©Copyright or missing credits issue please check CopyRights page for faster resolutions.

Leave a Reply