Using zip command in linux

How do I zip/unzip on the unix command line?

@joachim, you should ask the question in a question and provide an answer in an answer. There’s no problem with answering your own questions but not as part of the question itself, since that breaks down the whole community-votes-on-the-best-answer advantage of SO. In addition, this is probably better suited for superuser since it’s not programming related.

@rlb.usa: I work in a lot of different places on a lot of different servers. Using SA for a note like this helps other people too, as I see it.

9 Answers 9

Typically one uses tar to create an uncompressed archive and either gzip or bzip2 to compress that archive. The corresponding gunzip and bunzip2 commands can be used to uncompress said archive, or you can just use flags on the tar command to perform the uncompression.

If you are referring specifically to the Zip file format, you can simply use the zip and unzip commands.

zip squash.zip file1 file2 file3 

this unzips it in your current working directory.

Just as a side note: zip can create many different flavors of .ZIP archives, as well as use many different compressors. On the other hand, there are a number of other compression tools named similarly: 7-Zip, gzip, bzip2, rzip, etc. which are not interchangeable.

I’m confused, I did this and get a long list of inflations and extractions but when I check the directory it is empty, save for the original zip file? Where is it all going?

For the record, use unzip -d myfolder squash.zip to extract into a separate new directory ( myfolder ) instead of putting all the files directly into the current directory, which can be annoying if the zip file contains many files and directories in its top level. This mimics how most UI unzip tools work.

There are a truly vast number of different ways to compress and uncompress under UNIX derivatives so I’m going to assume you meant «zip» in the generic sense rather than a specific file format.

You can zip files up (in compressed format) with the GNU tar program:

Читайте также:  Access permission denied linux

which will do the current directory. Replace . with other file names if you want something else.

That’s assuming of course that you have a tar capable of doing the compression as well as combining of files into one.

If not, you can just use tar cvf followed by gzip (again, if available) for compression and gunzip followed by tar xvf .

For specific handling of ZIP format files, I would recommend downloading 7zip and using that — it recognises a huge variety of file formats, including the ZIP one.

Or use tar jcvf file.tar.bz2. to compress in bzip2 format or tar Jcvf file.tar.xz . for xz compression.

@Starfish, that’s certainly within your rights, thanks for at least explaining why, so many people don’t take the time to do that 🙂 I took «zip» in the more generic sense (compression) rather than a specific format, so I’ll make that more clear.

Be aware that tar archives may not work correctly when moving between different platforms, mac to linux or vice versa. Zip is much safer, as it works correctly on all systems.

I suppose technically this is gzip, so that’s fine. I guess. Pretty obviously not what the question asked for though.

If you don’t have zip and unzip packages installed and you have java, you can use jar to unzip:

Well, when it comes to distributing files for a variety of operating systems, I’d recommend 7-zip.

Usually in the package p7zip , you’ll get the 7z and 7za command, with which you can create your own 7z archives.

7za can also decompress standard (pkzip) zip archives (and create them as well with the -tzip switch).

7za a archive.7z file1 file2 directory/ 

It can also create self-extracting archives with the -sfx switch:

7za a -sfx archive.exe files1 file2 dir 

I recommend this method in case Windows users can’t open 7z archives (in case you want to advice a tool for that: PeaZip).

If you want to use the same compression algorithm with your tarballs, use the -J switch with tar :

tar cJf archive.tar.xz file1 file2 dir 

xz is a UNIX tool, that uses LZMA2 for compression, but works the way gz , bz2 , etc works. It even works as a filter.

7z doesn’t create archives with full filesystem information on UNIX, so you’d need to use tar before using 7z (but since 7z stores other information about the tar file, I’d recommend using xz , as it is designed for it):

tar cf - file1 file2 dir | 7za z -si archive.tar.7z 

Источник

How to Zip a File in Linux

ZIP is the most popular archive file format for compressing files and directories. Compressing files into an archived format helps conserve space and network bandwidth. Even though the tape archive (tar) format is more common on Linux systems, ZIP is also used often due to its popularity.

Читайте также:  Linux create live usb

Linux offers the zip command for compressing files into ZIP format. Alternatively, creating ZIP files is possible through the GUI, too.

This guide shows how to zip files and directories through the command line and GUI in Linux.

How To Zip A File In Linux

Check if zip Is Installed

Not all Linux systems have the zip program installed by default. See whether the utility is available by checking the version:

zip --version terminal output

If the output prints the program version, continue to the following section. However, if the output shows the command is not found, install the zip and unzip utility with the following command:

sudo apt install zip unzip

Note: Installing unzip is not mandatory, but the command goes hand-in-hand with zip .

If you’re looking to unzip files, check out our guide on how to unzip a ZIP file.

Enter the sudo password and wait for the installation to complete.

Zip Files in Linux With the zip Command

The zip command helps create ZIP archive files. The general syntax for the zip command is:

Without any options, the command creates a new ZIP file. The additional options and syntax change the behavior and provide different functionalities.

zip Command Options

The zip command offers various work modes and options to help create ZIP files. The table below shows a short overview of the available options.

Tag Option or Mode Description
-u
—update
Mode Updates and adds new files. Creates a new archive if not found.
-f
—freshen
Mode Updates files without adding new ones. Creates a new archive if not found.
-d
—delete
Mode Chooses entries in an existing archive and removes them.
-U
—copy-entries
Mode Chooses entries from an existing archive and copies them into a new archive.
-e
—encrypt
Option Encrypts ZIP archive contents with a password. Starts a password entry prompt.
-i
—include
Option Includes only specified files.
-R
—recurse-patterns
Option Archives files recursively.
-sf
—show-files
Option Lists files the command would operate on, then exits.
-x
—exclude
Option Excludes specified files.
Option Regulates compression speed (0-9). Lower numbers are faster.

The zip command offers many other options you can view using the man command.

Create a ZIP Archive

The zip command, without any options, creates a new file. To test the command, do the following:

1. Create files for archiving:

The command creates five empty text files.

2. Use the zip command to archive the files:

zip files file1.txt file2.txt file3.txt file4.txt file5.txt

Create and zip files terminal output

The command outputs the actions taken and creates a files.zip archive.

List ZIP File Contents

The -sf option lists the contents of a ZIP file. Provide only the ZIP file name, for example:

zip -sf show files terminal output

The command lists the archive’s contents and exists.

Add Specific File Types to ZIP Archive

To add only specific file types to a ZIP file, use a wildcard and provide the filetype extension. For example:

zip all text files terminal output

The command adds all files with the .txt extension to the archive.

Add a Directory to ZIP Archive

Use the -r (recursive) option to add a directory to a ZIP file. For instance:

zip -r directory with files terminal output

The zip command adds the empty directory first, then populates it with the files.

Delete Files From ZIP Archive

1. To delete files from a ZIP archive, list the files from the archive using the -sf option first. For example:

2. Locate the file for deletion and run zip with the -d tag:

zip -d delete files terminal output

The command removes the specified files from the ZIP archive.

Create and Encrypt ZIP File

A password protects the ZIP archive from extraction. To add password encryption to a ZIP file, use the -e option:

zip -e encrypted file terminal output

The command starts a password entry prompt. After confirming, the files are added to the ZIP archive.

Control ZIP Compression Level

The zip command allows controlling the compression level. The ZIP file compression level and speed are reversely proportional. As the level increases, the compression takes longer.

To control the ZIP file compression level, use the following syntax:

zip files compression 5 terminal output

For the fastest compression, use -1 . For the highest level of compression, use -9 . Values between 1 and 9 provide in-between results (fast vs. level of compression).

Create a ZIP Archive Using The GUI

To create a ZIP file in Linux through the GUI, do the following:

1. Open Files and navigate to the appropriate directory.

2. Select the files for archiving, right-click the files, and choose Compress.

Ubuntu GUI Compress option

3. Enter the archive name and choose the .zip format from the dropdown menu.

Create archive ZIP GUI Ubuntu

4. Click Create to create the ZIP file.

The process creates a ZIP archive in the current location.

After reading this guide, you should know how to create a ZIP file in Linux with the zip command and GUI. For other file compression and archiving commands, check out the tar command.

Источник

Оцените статью
Adblock
detector