3
votes

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?

1

1 Answers

6
votes

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.