In today's digital world, it is inevitable that data accumulates. Archiving and compressing files is an effective method to save storage space and prepare data for sending over the Internet. Especially on Linux, the use of commands like tar and zip is widespread. In this guide, you will learn how to effectively archive, compress, and unpack files and folders.

Key Insights

  • The commands tar and zip are central tools for archiving and compressing files on Linux.
  • With tar, you can create archives and also compress them by using the z option.
  • Unpacking archives is also done with the tar command, where different options can be used to meet specific requirements.
  • There are also alternatives, like gzip, to compress individual files.

Step-by-Step Guide

Archiving folders and files with tar

To archive files and folders, we use the tar command. You have the option to create an archive or extract an existing one. In this step, you will create an archive.

To start the command, open your terminal and navigate to the directory from which you want to archive the files. Here is an example command to create an archive:

tar cvf papier.tar papier papier2

In this example, c stands for "create", v for "verbose" (i.e., show me what is happening), and f stands for "file" indicating the name of the archive. Here we include the folders papier and papier2 in the archive. The output you see on the screen shows all the content being packed into the archive.

Effectively archiving and compressing files on Linux

Checking the archive

Once you have created the archive, you might want to check its contents. To see which files are included in the archive, you can use the following command:

tar tf papier.tar

This command lists the files in the archive without extracting them. Useful when you need a quick overview.

Effectively archiving and compressing files under Linux

Unpacking the archive

To unpack an existing tar archive, you can do this very easily. Navigate to the location where your tar file is located and use the following command:

tar xvf papier.tar

Here, x stands for "extract". You can add the v option to see which files are currently being extracted during the unpacking process.

Effectively archiving and compressing files in Linux

Creating an archive with compression

To further reduce the size of your archives, you can add the z option to compress the archive upon creation. The command looks as follows:

tar cvfz papier.tar.gz papier papier2

This will create a.tar.gz archive, indicating that the archive has been compressed both in tar format and via gzip.

Effectively archiving and compressing files under Linux

Using gzip for individual files

Another method of compression comes from using gzip. You can use this to compress individual files. For example:

gzip datei.txt

This will create a compressed file named datei.txt.gz and delete the original datei.txt. To restore the file, use:

gunzip datei.txt.gz
Effectively archiving and compressing files in Linux

Compressing all files in a directory

If you want to compress multiple files in the current directory, you can simply use wildcards. The following command compresses all files:

gzip *

This will turn all files into compressed.gz files.

Effectively archiving and compressing files in Linux

Summary - Archiving and compressing files on Linux

You have now learned how to effectively archive and compress files and folders using tar and gzip. The various options and commands offer you flexibility to manage your data efficiently.

Frequently Asked Questions

What is the difference between tar and zip?tar archives files, while zip compresses and archives files.

How do I unpack a tar.gz file?Use the command tar xvf filename.tar.gz in the terminal.

Can I also extract just one file from a tar archive?Yes, you can extract a specific file by using the command tar xvf filename.tar.gz file.txt.

What do I do if I only need an overview of the files in the archive?Use the command tar tf filename.tar for a listing.

How can I best store my archives?It is advisable to back them up on external storage devices or cloud solutions to avoid data loss.