Extract It Where? Making Sense of tar Archives
Create, inspect and extract tar archives with deliberate destination paths. Use -C to reduce accidental overwrites, and understand its security limits.
Suppose you need one file from a backup. You extract the archive, the command succeeds, and now there is a new directory somewhere you were not expecting.
Or worse, the file was already there and your local changes have disappeared. Successful extraction, slightly less successful morning :)
While preparing for RHCSA, this is the part of tar I want to make clear: the paths inside the archive and the directory where we extract it work together. Let’s try it with a few disposable files.
Make an Archive We Can Inspect
Use GNU tar on a Linux lab machine, with your ordinary account. Create a new practice directory; if the name in the example already exists, choose another. Run the commands in order, stopping if one fails.
# Keep the experiment separate from existing files.
mkdir ~/rhcsa-tar-lab
cd ~/rhcsa-tar-lab
# Create a directory containing the file we will archive.
mkdir bundle
echo 'archived revision' > bundle/notes.txt
# Create notes.tar from the bundle directory.
tar -cvf notes.tar bundle
The letters have separate jobs: -c creates the archive, -v displays the filenames being processed, and -f introduces the archive filename, here notes.tar. bundle is the directory we are putting inside it.
We have packaged the files together without compressing them.
Before extracting, look inside:
# List the stored paths without extracting anything.
tar -tf notes.tar
bundle/
bundle/notes.txt
-t lists the contents. We have left out -v here to keep the output focused on the names.
Notice that the file is stored as bundle/notes.txt, not just notes.txt. That directory level will matter when we restore it.
Tell tar Where to Extract
Create an empty destination, then extract there:
mkdir restored
# Extract into restored, keeping the paths stored in the archive.
tar -xvf notes.tar -C restored
# Read the restored copy.
cat restored/bundle/notes.txt
-x means extract. Capital -C changes tar’s working directory to the directory that follows it. It does not move your shell, and the destination must already exist.
The result is easier to follow when we put the two paths together:
| Extraction directory | Path stored in the archive | Restored file |
|---|---|---|
restored/ |
bundle/notes.txt |
restored/bundle/notes.txt |
The file contains archived revision. -C chose where extraction started; it did not remove the bundle directory.
Without -C, tar would extract relative to your shell’s current directory. In this lab, that would put the archived file back over our original bundle/notes.txt.
Choose an Empty Directory, Too
An explicit destination can still contain files worth keeping. Let’s demonstrate that inside the lab:
mkdir -p working/bundle
echo 'local changes worth keeping' > working/bundle/notes.txt
# Deliberately overwrite this disposable copy with the archived version.
tar -xvf notes.tar -C working
cat working/bundle/notes.txt
mkdir -p creates the parent directories as needed. The final output is archived revision: our local changes have been replaced.
This is why I would use a fresh, empty extraction directory, rather than simply add -C and assume the problem is solved.
For an unfamiliar archive, use a directory you control, with parent directories other users cannot modify, and extract as your ordinary account. Inspect the contents before moving them into a live location. -C is not a sandbox: it chooses a destination, but does not make downloaded files trustworthy or isolate the extraction process. Genuinely untrusted material needs an isolated environment.
Recover Just the File You Need
To extract one file, add its stored name to the command:
mkdir selected
# Use the complete member name from the archive listing.
tar -xvf notes.tar -C selected bundle/notes.txt
cat selected/bundle/notes.txt
We still get selected/bundle/notes.txt. Selecting one file does not flatten its path.
If you want an archive containing only notes.txt, you can choose that layout when creating it:
# Look inside bundle before adding notes.txt to the archive.
tar -cvf flat-notes.tar -C bundle notes.txt
tar -tf flat-notes.tar
The listing now contains just notes.txt. Here, -C bundle changes where tar looks for the filename that follows it.
Compression can wait until these paths make sense. Adding -z when creating an archive uses gzip, for example tar -czvf notes.tar.gz bundle. GNU tar recognises that compression when listing or extracting the archive file, so our destination habit stays the same.
Before extracting, I want to know three things: which archive I am opening, which paths it contains, and where those paths will land. The command is much easier to trust when I can answer those questions.
Further Reading
- GNU tar: tutorial and extraction examples
- GNU tar: changing directory with -C
- GNU tar: security guidance
- Red Hat RHCSA 10 Cert Guide, Chapter 3: Essential File Management Tools, my study reference.
Leave a comment
Questions, corrections or a different approach? Join the discussion with your GitHub account.