Pack file with tar.gz from root directory

gziptar

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?

Best Answer

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
Related Question