cancel
Showing results for 
Search instead for 
Did you mean: 
TudorRaduta
Community Manager
Community Manager
  • 167 Views

The tar mistake that can break your system

A single slash can decide whether you restore or overwrite

Happy Wednesday, everyone!

For today's "Problem & a Polish," let's talk about a subtle but critical mistake when creating archives, one that's directly related to the RHCSA objective to "archive, compress, unpack, and uncompress files."

The Problem: A junior admin is asked to back up the web server's configuration. They run: tar -czvf httpd_backup.tar.gz /etc/httpd. Later, they need to restore it in a test directory, but when they extract it, the archive overwrites the live /etc/httpd directory on their system! This happens because `tar` preserved the absolute path from the root `/` directory.

This mistake can be catastrophic, as you might accidentally overwrite critical production files. Luckily, there's a simple, safe way to do it.

The Polish: Always archive with relative paths.

The best practice is to change into the parent directory first, then create the archive using the directory name. This makes your archive portable and safe to extract anywhere.

cd /etc
tar -czvf /path/to/your/httpd_backup.tar.gz httpd

Now, when you extract this archive, it will create an httpd directory in your current location, which is exactly what you want.

Your Turn!

  1. Have you ever been caught by the absolute vs. relative path issue with tar or another command?
  2. What's your favorite "must-use" flag for the tar command? (e.g., --exclude, -v, etc.)

Share your experiences in the comments!

0 Kudos
4 Replies
ricardodacosta
Moderator
Moderator
  • 116 Views

tar automatically makes paths relative when archiving to a file.

ricardodacosta_0-1760556316457.png

 

TudorRaduta
Community Manager
Community Manager
  • 91 Views

That’s a great observation and exactly the kind of detail that shows how well you know your tools. tar does remove the leading / by default on RHEL, which adds an extra layer of safety.

The key takeaway here is less about the flag itself and more about the habit and knowing what paths you’re archiving and where you’re extracting them will always keep you out of trouble.

0 Kudos
Ravi_Shanker
Flight Engineer
Flight Engineer
  • 46 Views

This is classic example of how to use relative path vs absolute path. The same results could have been achieved by using ./ and any where you restore it, it would restore and not on the original. Only old school unix admins would know this, possibly there were not these many switches then.

tar -czvf httpd_backup.tar.gz ./etc/httpd 

Restore it anywhere except root and it would create ./etc/httpd in that location.

 

# tar -czvf resolv.conf-_backup.tar.gz ./etc/resolv.conf

# cd /tmp

# tar -xzvf /resolv.conf-_backup.tar.gz

# cd etc/

# ls -l
total 4
-rw-r--r--. 1 root root 97 Oct 16 18:23 resolv.conf

# pwd
/tmp/etc

I did not know Linux tar removed / from the path. This was my safe bet moving across different systems Linux to AIX using find | backup or tar commands. Thanks @ricardodacosta for sharing the knowledge pearl..

 

 

 

 

Certification ID: 111-010-393
Chetan_Tiwary_
Community Manager
Community Manager
  • 20 Views

@Ravi_Shanker 

When you use the tar command, it usually keeps things safe by converting absolute paths (like /etc/config/file.txt) into relative paths (just etc/config/file.txt).

This is a good default behavior because when you extract the archive later, the files drop into your current directory instead of overwriting files in their original, system-wide locations.

If you specifically need the files to be extracted back to their exact, original root locations, you must tell tar to keep those absolute paths. You do this by using the --absolute-names (or the shorter -P) option when you create the archive.

0 Kudos
Join the discussion
You must log in to join this conversation.