Use zip command in linux

How to use zip command in Linux

The zip command in Linux is a utility used for packaging a group of files into a zip archive. The zip command can also be used to compress files, making them take up less space on your computer’s hard drive. In addition, the zip command can be used to encrypt files, making it more difficult for unauthorized users to access the contents of the zip archive.

To unzip a zip archive, you can use the unzip command. This command will decompress the files in the zip archive, allowing you to access them.

Zip Command Examples

Go through the below real examples of Linux zip command.

This command will create a zip of all files in the /backup directory. I will not archive files under the sub directories recursively.

Output:
adding: backup/anaconda.ifcfg.log (deflated 47%) adding: backup/anaconda.log (deflated 78%) adding: backup/anaconda.program.log (deflated 84%) adding: backup/anaconda.storage.log (deflated 90%) adding: backup/boot.log (deflated 72%) adding: backup/dracut.log (deflated 92%) adding: backup/httpd/ (stored 0%) adding: backup/kadmind.log (deflated 74%) adding: backup/krb5kdc.log (deflated 71%) adding: backup/mysqld.log (deflated 82%)

Use Linux wildcards to archive files of specific extensions only. Like backup only .log extension files in a directory.

Output:
adding: backup/anaconda.ifcfg.log (deflated 47%) adding: backup/anaconda.log (deflated 78%) adding: backup/anaconda.program.log (deflated 84%) adding: backup/anaconda.storage.log (deflated 90%) adding: backup/boot.log (deflated 72%) adding: backup/dracut.log (deflated 92%) adding: backup/kadmind.log (deflated 74%) adding: backup/krb5kdc.log (deflated 71%) adding: backup/mysqld.log (deflated 82%) adding: backup/pm-powersave.log (deflated 15%) adding: backup/wpa_supplicant.log (stored 0%) adding: backup/Xorg.0.log (deflated 83%) adding: backup/Xorg.9.log (deflated 83%) adding: backup/yum.log (deflated 77%)

The below command will create an archive recursively with files, directories, and its sub-directories as well. It also maintains the directory structure as source files.

Sometimes we need to create a password-protected archive. The zip command provides you an option to make password-protected archive files. To create password-protected archive use -e option. This will prompt for a password and confirm the password.

zip -e backup.zip /backup/*.log 
Output:
Enter password: Verify password: adding: backup/anaconda.ifcfg.log (deflated 47%) adding: backup/anaconda.log (deflated 78%) adding: backup/anaconda.program.log (deflated 84%) adding: backup/anaconda.storage.log (deflated 90%)
  • -6 is used as default compression level.
  • -0 is used for the lowest level compression.
  • -9 is used for the highest level compression
zip -9 high-compressed-file.zip /backup/* zip -0 lowest-compressed-file.zip /backup/* 

Check differences between a compressed file

ls -lh lowest-compressed-file.zip high-compressed-file.zip 
Output:
-rw-r--r--. 1 root root 50K Apr 11 14:14 high-compressed-file.zip -rw-r--r--. 1 root root 447K Apr 11 14:14 lowest-compressed-file.zip
Output:
Archive: backup.zip Length Date Time Name --------- ---------- ----- ---- 140 04-11-2013 14:07 backup/anaconda.ifcfg.log 11153 04-11-2013 14:07 backup/anaconda.log 15446 04-11-2013 14:07 backup/anaconda.program.log 136167 04-11-2013 14:07 backup/anaconda.storage.log 2722 04-11-2013 14:07 backup/boot.log 211614 04-11-2013 14:07 backup/dracut.log 0 04-11-2013 14:08 backup/httpd/ 1382 04-11-2013 14:07 backup/kadmind.log 1248 04-11-2013 14:07 backup/krb5kdc.log 6485 04-11-2013 14:07 backup/mysqld.log 87 04-11-2013 14:07 backup/pm-powersave.log 0 04-11-2013 14:07 backup/wpa_supplicant.log 30186 04-11-2013 14:07 backup/Xorg.0.log 31094 04-11-2013 14:07 backup/Xorg.9.log 6739 04-11-2013 14:07 backup/yum.log --------- ------- 454463 15 files

The unzip command is used to extract a zip file. Use the below command to simply extract a zip file.

Читайте также:  Find all ports in use linux

Use -t to check and archive files. This option extracts each specified file in memory and compares the CRC (cyclic redundancy check, an enhanced checksum).

Output:
Archive: backup-11Apr2013.zip testing: backup/anaconda.ifcfg.log OK testing: backup/anaconda.log OK testing: backup/anaconda.program.log OK testing: backup/anaconda.storage.log OK testing: backup/boot.log OK testing: backup/dracut.log OK testing: backup/httpd/ OK testing: backup/kadmind.log OK testing: backup/krb5kdc.log OK testing: backup/mysqld.log OK testing: backup/pm-powersave.log OK testing: backup/wpa_supplicant.log OK testing: backup/Xorg.0.log OK testing: backup/Xorg.9.log OK testing: backup/yum.log OK No errors detected in compressed data of backup.zip.

Wrap Up

We’ve shown you how to zip files in Linux using the terminal. This is a handy skill that can come in handy when you need to send someone a compressed file or save storage space on your computer. Also, the Unzipping an archive is easy and we’ll show you how to do it quickly and easily.

Источник

Создаем zip-архивы в командной строке

Рассмотрим, как создавать и распаковывать zip архивы из командной строки.

Для создания архивов служит команда zip. У нее есть более 30 разных опций, но я рассмотрю простейшие примеры.

Создаем простой zip-архив

Для создания zip-архива просто выполняем команду zip, в первом аргументе указываем имя будущего архива, а во втором сам файл, который мы сжимаем:

Если нужно сжать несколько файлов то перечисляем их через пробел:

zip myarchive.zip myfile.txt yourfile.txt theirfile.txt

Создаем zip-архив папки

Чтобы заархивировать папку, используется ключ -r:

zip -r mydir.zip verygooddir

Создаем zip-архив с паролем

Очень важной функцией утилиты zip является возможность задания пароля на распаковку архива. Для этого применяется опция -P, после которой следует написать пароль:

zip -P мойпароль -r mysecretdir.zip mysecretdir

Если вы не хотите вводить пароль в командной строке у всех на виду, то можно использовать опцию -e, чтобы вместо ввода пароля в открытую, вводить его в срытом виде:

zip -er mysecretdir.zip mysecretdir

После выполнения данной команды, вам будет предложено дважды ввести пароль. Сам пароль виден при этом не будет:

Enter password: Verify password:

Распаковка zip-архива

Для того, чтобы разархивировать zip-архив, используется команда unzip. Ее можно запускать без опций, только указывая имя архива:

Читайте также:  Eclipse pydev for linux

По умолчанию распаковка происходит в текущей директории. Чтобы распаковать архив в другую директорию, используется опция -d, после которой нужно указать путь до директории:

Источник

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:

Читайте также:  Установка принтера canon lbp 2900 linux mint

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 

Источник

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