How to zip a folder in Linux (with pictures)

Table of contents:

How to zip a folder in Linux (with pictures)
How to zip a folder in Linux (with pictures)
Anonim

The easiest way to transfer multiple files from a Linux system is to archive them using the tar command (keep in mind that files in such an archive are not compressed). If you zip a folder (directory), multiple files become one file, which can be transferred, saved or compressed to reduce its size.

Steps

865895 1
865895 1

Step 1. Understand the file format

On Linux, multiple files can be zipped using the tar command. This will turn multiple files into one file that can be compressed and transferred to another computer. The generated file will have a.tar extension. Files with the.tar extension are sometimes referred to as "tarballs".

The tar command will archive but not compress files. Therefore, the size of the archive will be equal to the total size of all files that are in it. A.tar file can be compressed using the gzip or bzip2 command to produce a.tar.gz or.tar.bz2 file. The file compression process is described at the end of this article

865895 2
865895 2

Step 2. Create an archive from one folder (directory)

The tar command comes with a number of options when creating an archive from a directory. The following is an example of using the tar command:

tar -cvf Name.tar / path / to / directory

  • tar is the command that starts the Tar archiver.
  • c - an option that is responsible for creating a file with a.tar extension. This option always comes first.
  • v is an option that is responsible for displaying detailed information on the screen. That is, during the archiving process, the screen will display files that are sent to a file with the.tar extension. Enter this option as desired.
  • f is an option that is responsible for naming a file with a.tar extension. This option always comes last.
  • Name.tar is the name of the archive. Enter any name you like, but don't forget to add the.tar extension to the end of the name. You can also add the path to the file to the file name if you want to create an archive in a different directory (that is, different from the current one).
  • / path / to / directory is the path to the directory from which the archive is created. This path is relative to your current directory. For example, if the full path is ~ / home / user / Pictures and you are in the / home directory, enter / user / Pictures. Be aware that all subdirectories will be included as well.
865895 3
865895 3

Step 3. Create an archive from several folders (directories)

To do this, at the end of the tar command, add the paths to the corresponding folders:

tar -cvf Name.tar / etc / folder1 / var / www / folder2

865895 4
865895 4

Step 4. Add files or folders to the existing archive

To add files and folders to an existing.tar file, use the appropriate option:

tar -rvf Name.tar file.txt path / to / other / directory

r is an option that is responsible for adding a file or folder to an existing archive. It replaces the c option, which is used when creating a new archive

865895 5
865895 5

Step 5. Compress the existing.tar file

To quickly compress an archive, use the gzip command. To compress the file as much as possible and get the smallest archive size, use the bzip2 command (note that this command will compress the archive longer than the gzip command).

gzip Name.tar bzip2 Name.tar

  • The gzip command will create a file with the extension.gz, that is, the file will be Name.tar.gz
  • The bzip2 command will create a file with the extension.bz2, that is, the tarName.tar.bz2 file will be obtained.
865895 6
865895 6

Step 6. Compress the archive when it is created

You can compress an already created archive, or you can compress it during creation using the appropriate options:

tar -czvf Name.tar.gz / path / to / directory tar -cjvf Name.tar.bz2 / path / to / directory

  • z is the option that is responsible for compressing the new archive using gzip. Be sure to add the.gz extension at the end of the file name.
  • j is the option that is responsible for compressing the new archive using bzip2. Be sure to add the.bz2 extension at the end of the file name.

Popular by topic