M BUZZ CRAZE NEWS
// general

Compressing a folder (tar) without its containing directory in the folder name

By Sarah Rodriguez

I am trying to compress a folder with the tar command.

When I am trying to compress it, it works fine. The problem is with the file name.

Source path:

/data/file/

Destination path:

/data/repo/temp/file.tar.gz
tar zcvf $srcpath $destinationpath

I am executing the command from a different folder, and while extracting the folder I am getting all the sub directories instead of file folder alone.

3

1 Answer

The easiest way to do that is to cd to the directory first:

cd /path/to/containing/folder && tar -zcvf tarfile.tar.gz foldername_tocompress

So that the (containing) folder's directory becomes the root directory of your compressed file.

A bit more advanced is using the -C option:

tar -zcvf tarfile.tar.gz -C /path/to/foldername_tocompress .

This creates a tar.gz file in the current (working) directory, containing all files/folders inside foldername_tocompress (mind the dot, saying all files/folders should be included).

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy