9

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 ?

halfer
  • 19,824
  • 17
  • 99
  • 186
Stacey
  • 4,825
  • 17
  • 58
  • 99
  • 1
    It's already gzip compressed thanks to your `-z`. You can verify this with `file stacey.tar`, and then simply rename it to add the missing extension – that other guy May 14 '18 at 20:39
  • I think you had to make an effort to avoid finding an answer. Here are your duplicates: [Compress a folder with tar?](https://unix.stackexchange.com/q/46969), [How to compress the directory and its contents](https://unix.stackexchange.com/q/185931), [How do I tar a directory of files and folders without including the directory itself?](https://stackoverflow.com/q/939982), [How to gzip all files in all sub-directories into one compressed file](https://stackoverflow.com/q/12331633), [gzipping up a set of directories and creating a tar compressed file](https://stackoverflow.com/q/3341131), etc. – jww May 15 '18 at 01:11
  • This sort of question is best asked on Super User, since it is essentially a consumer-level computing problem. It could go onto Ubuntu or Unix & Linux. – halfer May 06 '20 at 21:10

2 Answers2

10

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
zerocukor287
  • 555
  • 2
  • 8
  • 23
user3804598
  • 355
  • 5
  • 9
2

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 *
mwp
  • 8,217
  • 20
  • 26