Managing Disk Space   «Prev  Next»
Lesson 8Extracting files from an archive
Objective Use tar xvf to extract files from an archive.

Extracting Files from an Archive in Unix

The reverse of creating an archive is restoring an archive. When an archive is restored, the files are extracted, or copied out, from the archive. To extract files, the general form of the tar command is this:
% tar xvf archive
The x option is used to extract files from the specified archive. When you extract files, the x is required and must go first. The archive can be either the name of an existing tar file or the name of a device in which a tape is loaded. The v option causes tar to list file names as they are being extracted. It is highly recommended that you use tar tf to view an archive before restoring it. Files restored from an archive will overwrite files on the filesystem if the names are the same.

View the following series of images to explore the the use of tar to extract files from an archive.
The ls command shows your current directory, which contains the 
web.tar archive. The tar tf command reveals that web.tar contains the project directory tree. If you extract files from web.tar, you will overwrite the existing project directory.
1) The ls command shows your current directory, which contains the web.tar archive. The tar tf command reveals that web.tar contains the project directory tree. If you extract files from web.tar, you will overwrite the existing project directory.

Since web.tar contains a backup version of your project, you create an old directory so that you can extract the archive to a different location
2) Since web.tar contains a backup version of your project, you create an old directory so that you can extract the archive to a different location

Using the mv command, you move web.tar to the old directory. You then cd to old and use it to verify that web.tar is there
3) Using the mv command, you move web.tar to the old directory. You then cd to old and use it to verify that web.tar is there

The tar xvf command extracts files from web.tar.  Each output line starts with z, for extract. The other information lists the file names and sizes, in bytes and blocks.4
4) The tar xvf command extracts files from web.tar. Each output line starts with z, for extract. The other information lists the file names and sizes, in bytes and blocks.4

A Telnet session with various commands executed.
telnet session
Connect Edit Terminal Help
danielg % ls
project web.tar
danielg % tar -xvf web.tar
./web.tar
./project
./project/docs
./project/docs/index.html
./project/docs/doc_plan.html
./project/mktg
./project/mktg/index.html
./project/mktg/requirements.html
./project/qa
./project/qa/test_plan.html
./project/qa/index.html
./project/sw
./project/sw/index.html
./project/sw/product_spec.html
./project/index.html
danielg % find .
./web.tar
./project
./project/docs
./project/docs/index.html
./project/docs/doc_plan.html
./project/mktg
./project/mktg/index.html
./project/mktg/requirements.html
./project/qa
./project/qa/test_plan.html
./project/qa/index.html
./project/sw
./project/sw/index.html
./project/sw/product_spec.html
./project/index.html
danielg %
Description of Terminal Activity:
  • The user, `danielg`, lists the contents of the current directory which shows two items: a directory named `project` and a tar archive `web.tar`.
  • The `tar -xvf web.tar` command is executed to extract the contents of `web.tar`.
  • A `find .` command is then run to display the structure of the directory, verifying that the extracted project directory contains the same hierarchy that was in `web.tar`.


How can I use tar xvf to extract files from an archive?

You can use the tar command with the xvf (or --extract --verbose --file) options to extract files from an archive. Here are the basic steps:
  1. Open a terminal or command prompt on your system.
  2. Navigate to the directory where the archive is located.
  3. Use the tar command with the xvf options, followed by the name of the archive file, to extract the files from the archive.
    For example:
    tar xvf archive.tar
    
  4. The tar command will extract the files from the archive to the current directory. If the archive contains directories, the tar command will create those directories in the current directory and extract the files to the appropriate directories.

Here is a breakdown of the command options:
  1. tar: the command to run the tar utility
  2. -x or --extract: the option to extract files from the archive
  3. -v or --verbose: the option to display a list of files being extracted
  4. -f: specifies that the following argument is the name of the archive file

You can also use other options with the tar command to extract only certain files or directories from the archive, or to extract files to a specific directory. For example, the --exclude option can be used to exclude specific files or directories from the extraction, and the --directory option can be used to extract files to a specific directory.
Note that the specific options and syntax for the tar command may vary slightly depending on the operating system being used. It's recommended to consult the relevant documentation or seek guidance from experienced system administrators for more detailed guidance.
In the next lesson, you will wrap up this module by reviewing key commands, terms, and concepts you have learned, and then take a quiz.

SEMrush Software 8 SEMrush Banner 8