Compressing & Decompressing files

Compressing

tar -jcvf [filename.tar.bz2] [folder_to_compress]

Or for .gz files:

tar -zcvf [filename.tgz] [folder_to_compress]
Compressing folder

Sometimes you may get problems with absolute paths. For example, the following command will compress the whole structure /tmp/data/example/ (including tmp and data folders) within the file example.tgz.

tar -zcvf example.tgz /tmp/data/example

To avoid this just use -C, in the following example the full path will by replaced by the name of the last folder called “example“.

tar -zcvf example.tgz -C /tmp/data/ example
Compressing files within a folder

To compress only files within a folder you need to use the -C flag followed by the absolute path and a dot to indicate current folder:

tar -zcvf example.tgz -C /tmp/data/example/ .

Decompress files

tar -jxvf [filename.tar.bz2] [destiny_folder]

Or for .gz files:

tar -zxvf [filename.tar.gz] -C [destiny_folder]

Leave a Reply

Your email address will not be published. Required fields are marked *