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 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.
tar
or another command?tar
command? (e.g., --exclude
, -v
, etc.)Share your experiences in the comments!
tar automatically makes paths relative when archiving to a file.
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.
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..
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.
Red Hat
Learning Community
A collaborative learning environment, enabling open source skill development.