What an archive is
A tar archive is a sequence of entries. Each entry starts with a header that records the name, the file type, the permissions, the owner, the size, and the modification time. The data for regular files follows the header, padded to a fixed block size. Directories and symbolic links have headers but no data. Because the whole tree is one stream, you can copy it, compress it, send it over a network, or store it on tape without losing the structure.
The format is simple on purpose. It does not compress anything, it does not index the contents, and it does not check integrity. Those jobs belong to other tools that work on the stream, and later lessons in this module use gzip, xz, and sha256sum for them.
The three modes
Every tar command needs one mode option. -c creates an archive from the paths you name. -t lists the entries in an archive without extracting them. -x extracts entries into the current directory or the directory you choose with -C. The -f option names the archive file, and -v prints each name as it is processed. A common mistake is leaving out -f, in which case tar reads from or writes to a default device.
Create a small tree and archive it. The learner in the lab creates files with plain redirection, so every step can be repeated exactly.
mkdir -p project
echo alpha > project/a.txt
echo beta > project/b.txt
tar -cf project.tar projectThe archive contains the directory project/ and both files. Notice that the paths in the archive are the paths you gave to tar. If you pass a relative path, the archive stores a relative path, and extraction recreates the same relative tree.
Listing before you extract
Listing an archive first is the safest habit. It shows what the archive will write, and it reveals absolute paths or unexpected top-level names before any file is created. Add -v to see the modes, owners, sizes, and dates as well.
tar -tf project.tar
tar -tvf project.tarThe first form prints names only. The second prints a long listing like ls -l. Both read the archive without touching the disk tree, so they are the correct way to check an unknown archive from the internet.
An archive can contain absolute paths or ../ entries. Modern GNU tar strips leading slashes and refuses to write outside the target directory by default, but a list command is still the way to be sure before you extract.
Extracting into a directory
Extraction writes files relative to the current directory unless you choose another with -C. Using -C keeps a restore separate from the original tree, which is what you want when you are testing a backup.
mkdir -p restore
tar -xf project.tar -C restore
diff -r project restore/project && echo sameThe diff -r command compares the two trees recursively. When it prints nothing and the message same appears, the restored tree matches the original byte for byte, names and directory structure included. A restore that you have not compared is a guess.
Selecting members
You can add only some entries to an archive. The -C option also works on create, and it changes directory before reading the next name. The archive then stores names relative to that directory, which keeps paths short and predictable.
tar -cf only-a.tar -C project a.txt
tar -tf only-a.tarThe listing shows only a.txt. The name in the archive has no project/ prefix, because the directory change happened before the entry was read. This is the form you will use in backup scripts that gather files from several places.
Common mistakes with paths
Most restore problems come from paths, not from tar itself. An archive that stores project/a.txt extracts under whatever directory you name with -C, so a restore into the wrong directory creates a second copy without any error. Listing the archive first shows the stored names, so you can decide where they belong before anything is written.
Absolute paths are the second source of surprises. When you archive a path that starts with a slash, GNU tar removes the leading slash by default, so the stored names are relative and extraction writes under the directory you choose. Keeping the slash would write over the live system when you restore, which is why the default is safe and why you should not override it casually.
The third mistake is archiving a directory that is still changing. Tar reads files one at a time, so a file that changes during the run can be captured in a mixed state. For a consistent copy, stop the writer or copy from a snapshot, and archive the snapshot.
Reading one member without extracting
Two forms are useful for peeking at an archive. The first lists entries with a pattern, which narrows the output to the names you care about. The second extracts one file to standard output, so you can read a single member without creating anything on disk.
tar -tf project.tar '*/a.txt'
tar -xOf project.tar project/a.txtThe -O option sends the extracted data to standard output, so the second command prints alpha and creates no file. Both forms leave the working directory untouched, so they are safe to use on an archive you have not yet decided to trust. Extraction comes only after the listing looks right.
Hands-on: create, list, and restore
Combine the three modes. Create an archive of the project directory, list it, and restore it into a new directory. The final comparison is the test that the archive is complete.
tar -cf backup.tar project
tar -tf backup.tar
mkdir check
tar -xf backup.tar -C check
diff -r project check/project && echo restoredIf the comparison prints the word restored, the archive contains everything the original tree had. Keep this three step rhythm, create then list then restore and compare, for any backup you rely on.