Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to tar without the parent directory but with the last directory

Tags:

linux

tar

I am trying to tar a directory without the parent directories and I have found a solution so far which removes the parent directories and tar all the files under a directory called (DIR) to a DIR.tar.gz file using the following:

tar -cvzf $DIR.tar.gz -C $path/$DIR .

But if I want to untar the tared file, it will untar all the files in the current directory, however, I want to be able to tar the dir in a way which when I use tar -xzvf, it will untar it into folder called DIR. IT should be simple, but I am not able to find the solution on the net. Any hint?

like image 766
newbie Avatar asked Oct 23 '25 05:10

newbie


1 Answers

I believe what you want is:

tar -cvzf $DIR.tar.gz -C $path ./$DIR

This should change the directory to your folder path (-C) and only include the relative directory ($DIR) that you're trying to archive.

like image 50
Kel Cecil Avatar answered Oct 25 '25 12:10

Kel Cecil



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!