Read

Module 8 · Archives, compression and the network

tar: listing and extracting

Look inside an archive before you trust it, unpack all of it or just one file, choose where it lands, and read a member without writing anything to disk.

What you will learn

  • List an archive's contents with `tar tf` and `tar tvf`.
  • Extract everything, or selected members, into a chosen directory with `-C`.
  • Print a member to stdout with `-O` instead of creating files.

Someone hands you site.tar. Before unpacking it, ask two questions: what is inside, and where will it land? An archive whose members are index.html, about.html and forty more loose files will scatter them all over your current directory; one whose members all start with site/ creates a tidy folder. Experienced admins always list first.

Listing: t

~% tar tf site.tar
site/
site/about.html
site/index.html
site/css/
site/css/style.css
~% tar tvf site.tar
drwxr-xr-x 0/0         0 2026-10-07 20:00:20 site/
-rw-r--r-- 0/0        18 2026-10-07 20:00:20 site/about.html
-rw-r--r-- 0/0        14 2026-10-07 20:00:20 site/index.html
drwxr-xr-x 0/0         0 2026-10-07 20:00:20 site/css/
-rw-r--r-- 0/0         8 2026-10-07 20:00:20 site/css/style.css
~% tar xf site.tar site/nothere
tar: site/nothere: not found in archive

t lists, f names the archive, and adding v turns the list into something like ls -l: permissions, owner as numeric uid/gid, size, date and name. Because the listing is plain text, the usual tools apply: tar tf site.tar | wc -l counts the entries, tar tf site.tar | grep css finds files, and tar tf site.tar > contents.txt keeps an inventory next to the archive.

Extracting: x

tar xf site.tar recreates every member relative to the current directory, with the permissions and dates stored inside. To extract somewhere else, add -C DIR; the directory must already exist, otherwise tar stops with *can't change directory*. Extraction silently overwrites files with the same name, which is exactly why you list first.

~% mkdir -p out
~% tar xvf site.tar -C out
site/
site/about.html
site/index.html
site/css/
site/css/style.css
~% tar xf site.tar -C /tmp/nowhere
tar: can't change directory to '/tmp/nowhere': No such file or directory

To take only some members, name them after the archive, exactly as the listing prints them: tar xf site.tar site/about.html creates site/about.html and nothing else (the parent directory is created as needed). A name that is not in the archive is an error. Copy the name from tar tf rather than typing it from memory.

Reading without unpacking: -O

Sometimes you only want to look at one file, say a README or a version number. -O sends the extracted data to standard output instead of creating files, so you can page it, grep it or redirect it wherever you like:

~% tar xOf site.tar site/about.html
<h1>About us</h1>
~% tar xOf site.tar site/index.html | wc -c
14
CommandEffect
tar tf a.tarNames only
tar tvf a.tarLong listing, like ls -l
tar xf a.tar -C dirUnpack everything into dir
tar xf a.tar NAMEUnpack one member
tar xOf a.tar NAMEPrint one member to stdout

Commands in this lesson

CommandWhat it does
tar tf a.tarList the member names.
tar tvf a.tarList with permissions, sizes and dates.
tar xf a.tarExtract everything into the current directory.
tar xf a.tar -C dirExtract into an existing directory.
tar xf a.tar path/in/archiveExtract only the named member.
tar xOf a.tar path/in/archivePrint a member without creating files.

Quiz

  1. Which command shows what is inside a.tar without changing anything on disk?

    • tar xf a.tar
    • tar tf a.tar
    • tar cf a.tar
    • cat a.tar
  2. `tar xf a.tar -C /tmp/new` fails with "can't change directory". Why?

    • /tmp/new does not exist yet
    • -C only works when creating
    • The archive is compressed
    • You must be in /tmp
  3. What happens if extracted files already exist with the same names?

    • tar asks before each one
    • They are overwritten without warning
    • tar renames the new ones
    • Extraction is refused
  4. What does the `O` in `tar xOf a.tar README` do?

    • Overwrites existing files
    • Writes the member to standard output instead of a file
    • Only extracts old files
    • Opens the file in an editor

Practice

  1. Save the list of member names of `/root/lab/l72/site.tar` into `/root/lab/l72/contents.txt`.

  2. Extract all of `/root/lab/l72/site.tar` into the directory `/root/lab/l72/out` (create it first), so that `/root/lab/l72/out/site/index.html` exists.

  3. Extract **only** `site/about.html` from `/root/lab/l72/site.tar` into `/root/lab/l72/one`, so that `/root/lab/l72/one/site/about.html` exists and `index.html` does not.

Open this lesson in the app to do the tasks in a real Linux machine and have them checked.