25

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).

Peachy
  • 7,077
  • 10
  • 37
  • 45
alphanumerical74
  • 253
  • 1
  • 3
  • 5

5 Answers5

29

Use the following one-liner:

tar cf - <source folder> | 7z a -si <Destination archive>.tar.7z
muru
  • 193,181
  • 53
  • 473
  • 722
ortang
  • 2,103
  • 14
  • 13
12

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.

muru
  • 193,181
  • 53
  • 473
  • 722
Giuseppe Curto
  • 121
  • 1
  • 2
9

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 
waltinator
  • 35,099
  • 19
  • 57
  • 93
7

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.

vordhosbn
  • 171
  • 1
  • 4
1

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

Christian
  • 11
  • 2