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

Extracting Files from Archive

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 SlideShow button to explore the the use of tar to extract files from an archive.
Tar Extract 1
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.
Tar Extract 2
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
Tar Extract 3
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
Tar Extract 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.
Tar Extract 5
5) Tar Extract 5

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.