Hi folks! Today, let’s unravel a neat
tar trick that’s often asked about: how do you tar files and folders inside a directory without including the parent directory in the tarball? This is especially useful when you want just the contents, not the folder structure.
The Classic Tar Puzzle
Imagine you have a directory
Data filled with files and other folders. You want to create a
Data.tar archive of everything inside
Data but without the
Data directory itself being part of the archive. Sounds tricky, right? Not really!
Dive into the Command Line
Here’s how you do it:
- Navigate to the Parent Directory: First, you need to be in the directory that contains
- Use Tar with Wildcards: The trick is to use wildcards. Instead of telling
Data, you tell it to archive everything inside
tar -cvf Data.tar -C Data .
-C Data changes the directory to
Data first and
. means everything inside it.
Why This Matters
This method is handy for various reasons:
- Selective Archiving: You get the contents without the extra folder layer, perfect for specific backup or deployment scenarios.
- Flexibility: It allows for more control over the structure of your archived data.
- Clean and Tidy: Ideal when you want to unpack files without creating an additional directory.
Now Let’s explore some of the other scenarios.
Scenario 1: Tar Specific File Types
Suppose you want to tar only certain types of files within the directory. You can combine find command with tar:
tar -cvf Data.tar -C Data $(find . -name "*.txt" -type f)
This command archives only
.txt files from the
Scenario 2: Excluding Certain Files
If you want to exclude specific files or patterns:
tar --exclude='*.log' -cvf Data.tar -C Data .
This excludes all
.log files from the archive.
Scenario 3: Tar and Compress on the Fly
For compressing the tarball immediately:
tar -czvf Data.tar.gz -C Data .
This creates a gzipped tarball of the contents of
Scenario 4: Incremental Backup
If you’re doing incremental backups of the content:
tar --listed-incremental=/path/to/snapshot.file -cvf Data.tar -C Data .
This creates a tarball while recording changes from the last backup.
These scenarios illustrate the versatility of
tar. Whether you’re managing backups, deploying software, or just organizing files,
tar offers a solution tailored to your needs. Always remember to navigate to the correct directory and use wildcards or specific commands to control what gets included in your tarball.
Explore, experiment, and master these tricks to make your Linux journey more efficient and enjoyable!
Hi, I am Himadri Das, I am a blogger, and an open source contributor. I have about 11 years of experience in the Information Technology domain. Currently I am working in a Startup Company as Quality Assurance Manager. I have hands-on experience on Appium, Selenium, QTP, Locust, Automation framework, Performance Testing, Functional Testing, Java, python, Shell scripting, MySql, Redis, Kafka etc. Apart from my work and writing blogs, I love to play guitar, love to travel and love to watch cricket and football. If you want to know more about me, please visit my linkedin profile .