Compress and Decompress Files and Directories (Linux): Unterschied zwischen den Versionen

Aus MattWiki
Keine Bearbeitungszusammenfassung
Keine Bearbeitungszusammenfassung
 
(5 dazwischenliegende Versionen desselben Benutzers werden nicht angezeigt)
Zeile 1: Zeile 1:
== Packen ==
== Compress ==
 
tar -zcvf archive-name.tar.gz directory-name


=== How Compress directories ===
Unlike <code>zip</code>, <code>gzip</code> functions as a compression algorithm ''only''.
Unlike <code>zip</code>, <code>gzip</code> functions as a compression algorithm ''only''.


Because of various reasons some of which hearken back to the era of tape drives, Unix uses a program named <code>tar</code> to archive data, which can then be compressed with a compression program like <code>gzip</code>, <code>bzip2</code>, <code>7zip</code>, etc.
Because of various reasons some of which hearken back to the era of tape drives, Unix uses a program named <code>tar</code> to archive data, which can then be compressed with a compression program like <code>gzip</code>, <code>bzip2</code>, <code>7zip</code>, etc.


In order to "zip" a directory, the correct command would be
In order to "zip" a directory, the correct command would be:
<code>tar -zcvf archive.tar.gz directory/</code>
tar -zcvf archive.tar.gz directory/  
This will tell <code>tar</code> to '''c''' (create) an archive from the files in ''<code>directory</code>'' (<code>tar</code> is recursive by default), compress it using the '''z''' (gzip) algorithm, store the output as a '''f''' (file) named ''<code>archive.tar.gz</code>'', and '''v'''(verbosely) list (on /dev/stderr so it doesn't affect piped commands) all the files it adds to the archive.
This will tell <code>tar</code> to '''c''' (create) an archive from the files in ''<code>directory</code>'' (<code>tar</code> is recursive by default), compress it using the '''z''' (gzip) algorithm, store the output as a '''f''' (file) named ''<code>archive.tar.gz</code>'', and '''v'''(verbosely) list (on /dev/stderr so it doesn't affect piped commands) all the files it adds to the archive.


The <code>tar</code> command offers <code>gzip</code> support (via the <code>-z</code> flag) purely for your convenience. The <code>gzip</code>command/lib is completely separate. The command above is effectively the same as
The <code>tar</code> command offers <code>gzip</code> support (via the <code>-z</code> flag) purely for your convenience. The <code>gzip</code>command/lib is completely separate.  
<code>tar -cv directory | gzip > archive.tar.gz</code>
 
To decompress and unpack the archive into the current directory you would use
The command above is effectively the same as:
<code>tar -zxvf archive.tar.gz</code>
tar -cv directory | gzip > archive.tar.gz  
That command is effectively the same as
To decompress and unpack the archive into the current directory you would use:
<code>gunzip < archive.tar.gz | tar -xv</code>
tar -zxvf archive.tar.gz
That command is effectively the same as:
gunzip < archive.tar.gz | tar -xv
<code>tar</code> has many, many, MANY other options and uses as well; I heartily recommend reading through its manpage sometime.
<code>tar</code> has many, many, MANY other options and uses as well; I heartily recommend reading through its manpage sometime.
tar -zcvf archive-name.tar.gz directory-name


== Entpacken ==
== Decompress ==


=== tar ===
  tar xfv foo.tar.gz
  tar xfv foo.tar.gz
  tar -xjf foo.tar.bz2
  tar -xjf foo.tar.bz2
  tar -xvJf foo.tar.xy
  tar -xvJf foo.tar.xy
  tar -xvJf foo.tar.xz
  tar -xvJf foo.tar.xz
=== bzip ===
  bunzip2 foo.tar.bz2
  bunzip2 foo.tar.bz2
=== zip ===
unzip foo.zip
=== gunzip ===
gunzip foo.log.1.gz          # Decompress file and delete gz file
gunzip -k foo.log.1.gz        # Decompress file and preserve gz file
gunzip -N foo.log.1.gz        # Decompress file and preserve original timestamps and delete gz file
Further reading: https://www.cyberciti.biz/faq/how-to-gzip-and-keep-original-file-on-unix-or-linux-command-line/
[[Kategorie:Linux]]
[[Kategorie:Linux]]
[[Kategorie:Terminal]]
[[Kategorie:Terminal]]

Aktuelle Version vom 20. September 2025, 14:50 Uhr

Compress

How Compress directories

Unlike zipgzip functions as a compression algorithm only.

Because of various reasons some of which hearken back to the era of tape drives, Unix uses a program named tar to archive data, which can then be compressed with a compression program like gzipbzip27zip, etc.

In order to "zip" a directory, the correct command would be:

tar -zcvf archive.tar.gz directory/ 

This will tell tar to c (create) an archive from the files in directory (tar is recursive by default), compress it using the z (gzip) algorithm, store the output as a f (file) named archive.tar.gz, and v(verbosely) list (on /dev/stderr so it doesn't affect piped commands) all the files it adds to the archive.

The tar command offers gzip support (via the -z flag) purely for your convenience. The gzipcommand/lib is completely separate.

The command above is effectively the same as:

tar -cv directory | gzip > archive.tar.gz 

To decompress and unpack the archive into the current directory you would use:

tar -zxvf archive.tar.gz

That command is effectively the same as:

gunzip < archive.tar.gz | tar -xv

tar has many, many, MANY other options and uses as well; I heartily recommend reading through its manpage sometime.

tar -zcvf archive-name.tar.gz directory-name

Decompress

tar

tar xfv foo.tar.gz
tar -xjf foo.tar.bz2
tar -xvJf foo.tar.xy
tar -xvJf foo.tar.xz

bzip

bunzip2 foo.tar.bz2

zip

unzip foo.zip

gunzip

gunzip foo.log.1.gz           # Decompress file and delete gz file
gunzip -k foo.log.1.gz        # Decompress file and preserve gz file
gunzip -N foo.log.1.gz        # Decompress file and preserve original timestamps and delete gz file

Further reading: https://www.cyberciti.biz/faq/how-to-gzip-and-keep-original-file-on-unix-or-linux-command-line/