Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compress and tar a folder in Linux [closed]

I am new to Linux, I have a folder called stacey. How can I create a compressed tarball from this?

I can tar the folder with tar -cvzf stacey.tar *. But can I add the 7zip at the same time as so I have a compressed tarball called stacey.tar.gz ?

like image 731
Stacey Avatar asked Dec 07 '25 18:12

Stacey


2 Answers

To make a compressed tar ball of the current directory

tar -cvz -f path_to_the_archive_to_be_created .

Example:

[user@machine temp]$ tar -cvz -f ~/dir1/temp.tar.gz .

The current directory (temp) is archived into ~/dir1/temp.tar.gz

To make a compressed tar ball of a remote directory

tar -cvz -f path_to_the_archive_to_be_created -C path_to_the_remote_directory .

Example:

[user@machine ~]$ tar -cvz -f ~/dir1/temp.tar.gz -C ~/temp .

The directory ~/temp is archived into ~/dir1/temp.tar.gz.


-c, --create  create a new archive
-v, --verbose  verbosely list files processed
-z, --gzip, --gunzip, --ungzip  filter the archive through gzip
-f, --file=ARCHIVE  use archive file or device ARCHIVE
-C, --directory=DIR  change to directory DIR
like image 70
user3804598 Avatar answered Dec 09 '25 13:12

user3804598


Passing -z on the tar command will gzip the file, so you should name your file stacey.tar.gz (or stacey.tgz), not stacey.tar:

tar -cvzf stacey.tar.gz *

If you want to 7zip the file (instead of Gzip), remove the -z, keep stacey.tar, and run 7z stacey.tar after the tar command completes:

tar -cvf stacey.tar *
7z a stacey.tar.7z stacey.tar

Or you can use a pipeline to do it in one step:

tar -cvf - * | 7z a -si stacey.tar.7z

7zip is more like tar in that it keeps an index of files in the archive. You can actually skip the tar step entirely and just use 7zip:

7z a stacey.7z *
like image 20
mwp Avatar answered Dec 09 '25 14:12

mwp



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!