LiveFire Labs: Online UNIX Training with Hands-on Internet Lab

"Taking a LiveFire Labs' course is an excellent way to learn Linux/Unix. The lessons are well thought out, the material is explained thoroughly, and you get to perform exercises on a real Linux/Unix box. It was money well spent."

Ray S.
Pembrook Pines, Florida

LiveFire Labs' UNIX and Linux Operating System Fundamentals course was very enjoyable. Although I regularly used UNIX systems for 16 years, I haven't done so since 2000. This course was a great refresher. The exercises were fun and helped me gain a real feel for working with UNIX/Linux OS. Thanks very much!"

Ming Sabourin
Senior Technical Writer
Nuance Communications, Inc.
Montréal, Canada

Read more student testimonials...

Receive UNIX Tips, Tricks, and Shell Scripts by Email

LiveFire Labs' UNIX Tip, Trick, or Shell Script of the Week

Basic UNIX Commands for Beginners: Using the tar Command to Create and Extract UNIX File Archives

The UNIX tar Command

The UNIX tar command is used to create an archive of files, list the contents of an archive, or extract files from an archive. An archive is a single file containing one or more files, and information about each file in the archive.

The file information may include the file's owner, the file's access mode (permissions), the file's modification time, and other details about the file.

This information is used so that each file can be restored with the attributes of the original file. An archive file created with tar is known as a tar file.

[ If you are new to UNIX and need an overview of important UNIX commands and concepts, check out our Basic UNIX Commands and Concepts Tutorial for Beginners ]

Tar was originally used to create tape archives, but is commonly used now to save tar files to disk, or to transmit them over the network while they are being created. A tar file typically has a .tar (dot tar) file extension, such as "testdir1.tar".

The syntax for the tar command is:

$ tar options files

What's unique about specifying options with the tar command is that you don't always need to put a hyphen in from of them. The following UNIX tar examples all use valid syntax for running tar with the options c, v, and f:

$ tar cvf docs1.tar /tmp/docs1

The above command is equivalent to:

$ tar -c -v -f docs1.tar /tmp/docs1

and also:

$ tar -cvf docs1.tar /tmp/docs1

Since some operating systems require a hyphen before the options when using tar, it is wise to get in the habit of using one. This tar example creates (-c option) a tar file named docs1.tar (-f option) that is saved in the current directory, and performs the operation in verbose mode (-v option) versus the default silent mode. All of the files in the /tmp/docs1 directory, including any subdirectories that may exist, are added to this archive.

The output from this operation will look like this:

$ tar -cvf docs1.tar /tmp/docs1
tar: Removing leading `/' from member names

To view the contents of this tar file, the -t (table of contents) option is used in place of the -c (create a new archive) option:

$ tar -tvf docs1.tar
drwxr-xr-x root/root 0 2012-08-15 14:27:24 tmp/docs1/
-rw-r--r-- root/root 0 2012-08-15 14:27:21 tmp/docs1/file1
-rw-r--r-- root/root 0 2012-08-15 14:27:23 tmp/docs1/file2
-rw-r--r-- root/root 0 2012-08-15 14:27:24 tmp/docs1/file3

To extract the contents of this tar file, the following command would be used:

$ tar -xvf docs1.tar

Notice that the -x (extract) option has replaced the -t (table of contents) option. This will create the subdirectory tmp in the current directory (if it doesn't exist), the subdirectory testdir1 under it, and then extract the contents of the archive into the new subdirectory.