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

Matheus Mello
Matheus Mello
September 2, 2023
Cover Image for 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.

Your Product
Product promotion

Share this article

More Articles You Might Like

Latest Articles

Cover Image for How can I echo a newline in a batch file?
batch-filenewlinewindows

How can I echo a newline in a batch file?

Published on March 20, 2060

🔥 💻 🆒 Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Cover Image for How do I run Redis on Windows?
rediswindows

How do I run Redis on Windows?

Published on March 19, 2060

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! 🚀 Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Cover Image for Best way to strip punctuation from a string
punctuationpythonstring

Best way to strip punctuation from a string

Published on November 1, 2057

# The Art of Stripping Punctuation: Simplifying Your Strings 💥✂️ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Cover Image for Purge or recreate a Ruby on Rails database
rakeruby-on-railsruby-on-rails-3

Purge or recreate a Ruby on Rails database

Published on November 27, 2032

# Purge or Recreate a Ruby on Rails Database: A Simple Guide 🚀 So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? 🤔 Well, my