Tar a directory, but don"t store full absolute paths in the archive


How to Tar a Directory without Storing Full Absolute Paths in the Archive
Have you ever encountered the issue of wanting to tar a directory but not store the full absolute paths in the archive? It can be quite cumbersome to deal with long and unnecessary directory structures when extracting files. In this blog post, we will address this common issue and provide you with some easy solutions.
Understanding the Problem
Let's start by understanding the problem. When you use the tar
command to create an archive, it preserves the full absolute paths of the directories and files within the archive. This means that when you extract the archive, the files will be extracted with their original directory structure intact.
For example, if you have a directory called /var/www/site1/
and you tar it using the following command:
tar -cjf site1.bz2 /var/www/site1/
When you list the contents of the archive using the command tar -tf site1.bz2
, you will see that the directory structure is preserved:
var/www/site1/style.css
var/www/site1/index.html
var/www/site1/page2.html
var/www/site1/page3.html
var/www/site1/images/img1.png
var/www/site1/images/img2.png
var/www/site1/subdir/index.html
The Desired Outcome
Now, let's talk about what you want to achieve. You mentioned that you would like to remove the part /var/www/site1
from the directory and file names within the archive. This way, when you extract the backup, the files will be extracted in the current directory, eliminating the need to move them afterwards. However, you also mentioned that you want to preserve the sub-directory structure.
Based on your example, the desired outcome is to have the following directory structure when listing the contents of the archive:
style.css
index.html
page2.html
page3.html
images/img1.png
images/img2.png
subdir/index.html
Easy Solutions
To achieve the desired outcome, here are a couple of easy solutions you can try:
1. Using the -C
or --directory
option
The -C
or --directory
option allows you to change the current directory before performing any operations. By providing a dot (.
) as the argument to the -C
option, you can instruct tar
to use the current directory as the base directory for relative file names.
Here's how you can modify your command:
tar -cjf site1.bz2 -C /var/www/site1/ .
This will change the current directory to /var/www/site1/
before creating the archive. As a result, the file names stored in the archive will no longer include the full absolute path.
2. Using the --transform
option
The --transform
option allows you to apply transformations to the file names before they are stored in the archive. You can use this option along with a simple sed
command to remove the leading directory path.
Here's how you can modify your command:
tar -cjf site1.bz2 --transform 's|^/var/www/site1/||' /var/www/site1/
The --transform
option takes a sed expression as an argument. In this case, we're using s|^/var/www/site1/||
to replace the leading /var/www/site1/
with an empty string. This effectively removes the leading directory path from the file names stored in the archive.
Your Call to Action
Now that you have learned how to tar a directory without storing full absolute paths in the archive, it's time to put this knowledge into action. Try out the solutions provided and see which one works best for your specific use case.
Have you encountered any other challenges while working with tar? Let us know in the comments below! Feel free to share any other tips or tricks you may have discovered along the way.
Happy tar-ing! 📦💪
Take Your Tech Career to the Next Level
Our application tracking tool helps you manage your job search effectively. Stay organized, track your progress, and land your dream tech job faster.
