Pack file with tar.gz from root directory

I try to pack a .csv file with tar.gz, while being in the root directory.

The file myfile.csv is located at /mnt/sdb1/
So the full filename is /mnt/sdb1/myfile.csv

I try to save the tar.gz under /mnt/sdb1/old_files

I tried it like this:

tar -czf /mnt/sdb1/old_files/new.tar.gz mnt/sdb1/myfile.csv

But when i extract the file, then a folder with name “mnt” will be extracted which cointains another folder called “sdb1”, which contains the file.

Is it possible to compress the file only, instead of copying all the directories?

Asked By: Black

||

You have first to cd /mnt/sdb1, then launch the tar command.

FYI, you don’t need to use tarhere as its purpose is to agglomerate several files in a tarball. Since you have only one file here, just use gzip.

Answered By: dr_

Use the -C option to tar:

   -C Directory
        Causes the tar command to perform a chdir subroutine to the
        directory specified by the Directory variable.
Answered By: Jeff Schaller

use the --directory option from man tar :

-C,- -directory DIR

change to directory DIR

i.e.:

tar -C /mnt/sdb1/ -czf /mnt/sdb1/old_files/new.tar.gz myfile.csv
Answered By: FelixJN
Categories: Answers Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.