How do I create tar.7z file from the command line?

28,693

Solution 1

Use the following one-liner:

tar cf - <source folder> | 7z a -si <Destination archive>.tar.7z

Solution 2

It is not a good idea compressing directly with 7z spcially on unix/linux systems: 7z does not preserve permissions and or user/group info. So: first tar, and then compress.

As reported on 7zip wiki page at http://en.wikipedia.org/wiki/7z#Limitations :

Limitations

The 7z format does not store filesystem permissions (such as UNIX owner/group permissions or NTFS ACLs), and hence can be inappropriate for backup/archival purposes. A workaround on UNIX-like systems for this is to convert data to a tar bitstream before compressing with 7z.

Solution 3

Read man tar. It offers:

     -a, --auto-compress
       use archive suffix to determine the compression program
     -j, --bzip2
     --lzip
     --lzma
     --lzop
     -z, --gzip, --gunzip --ungzip
     -Z, --compress, --uncompress

Or, if none of those is right for you, and you have a compression program that reads stdin, you could:

tar cf- $HOME | my_compression_program >/tmp/compressed.output

Note that I'm writing the output somewhere other than $HOME (backing up into a directory that you're backing up leads to unconstrained file growth).

Or, you could read man 7z - it looks like you could do

dir="directory to save"
7z a -t7z -m0=lzma -mx=9 -mfb=64 -md=32m -ms=on /tmp/archive.7z $dir 

Solution 4

I would suggest that you use:

tar cf - foldername | 7z a -si -m0=lzma2 -mx=3 foldername.tar.7z

for dramatic speedup increase.

It has the advantage of using lzma2 (-m0=lzma2) (which utilizes max available cores on your system and "Fast compression" preset (-mx=3), which is basically fast and good enough. Note that LZMA2 is not only utilizing all cores on compression, but also on decompression.

Solution 5

You should use tar -Jchf <Filename>.tar.xz <Files to compress>

The -J uses the XZ compression algorithm, the same as 7zip

-c creates a new file -h preserves simlinks -f sets the filename

Share:
28,693

Related videos on Youtube

alphanumerical74
Author by

alphanumerical74

Updated on September 18, 2022

Comments

  • alphanumerical74
    alphanumerical74 over 1 year

    I would like to zip up my homework from last year. How do I tar and zip the whole folder from command line Ubuntu (I don't have GUI).

  • Wilf
    Wilf over 9 years
    Potential source: man 7z.
  • Lonnie Best
    Lonnie Best over 4 years
    Finally, with your answer, I've found what I was looking for: the ability to use all processors on the task.