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 $destinationpathI 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.
31 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_tocompressSo 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).