Zip on linux examples

Содержание
  1. Linux Zip and Unzip Command with Examples
  2. Install zip & unzip command on Ubuntu / Debian / Linux Mint
  3. Install zip & unzip command on CentOS / RHEL / Fedora
  4. Zip command in Linux
  5. Example 1) Zipping a single file and multiple files with zip command
  6. Example 2) Adding a file to a zip archive (-u)
  7. Example 3) View contents of a zipped file
  8. Example 4) Zipping all the files in the current directory (* )
  9. Example 5) Delete a file from an archive (-d)
  10. Example 6) Delete files after zipping (-m)
  11. Example 7) How to zip a directory (-r)
  12. Example 8) Zip a file to a different destination
  13. Example 9) Zip both files and directories into their own archives
  14. Unzip command in Linux
  15. Example 1) How to view the contents of a zipped file (-l)
  16. Example 2) How to view detailed contents of a zipped file (-Z)
  17. Example 3) How to unzip/decompress a zipped file (-d)
  18. Example 4) Unzip a file to another directory
  19. Example 5) Unzip one or more files from an archive
  20. Example 6) Exclude certain files from being decompressed (- x)
  21. Example 7) How to suppress the output of unzip command (-q)
  22. Example 8) How to overwrite existing files (-o)
  23. Example 9) Unzip multiple files
  24. Создаем zip-архивы в командной строке
  25. Создаем простой zip-архив
  26. Создаем zip-архив папки
  27. Создаем zip-архив с паролем
  28. Распаковка zip-архива
  29. Linux zip Command Tutorial with Examples
  30. zip Command Syntax
  31. Install zip Command
  32. zip Command Help
  33. Zip A File
  34. Zip Multiple Files
  35. Add New File To Zipped File
  36. Delete/Remove A File From Zip File
  37. Zip Files and Folders Recursively
  38. Print Details About Zip Operation, Files and Folders
  39. Suppress Output Do Not Print Information

Linux Zip and Unzip Command with Examples

In this tutorial, we touch base on the zip and unzip commands and how they are used in Linux. Zip is a command used for creating an archive file or a zipped file. This allows you to compress your files and create more space for other files on your hard drive or removable drive. Additionally, zipping your files/directories makes them more portable and easier to upload, download or even attach and send them via email.The opposite of zipping is unzipping, and here we use the unzip command to decompress the files and access them individually.

Before start discussing zip and unzip command, let’s see how these commands can be installed from command line,

Install zip & unzip command on Ubuntu / Debian / Linux Mint

Open the terminal and run the following apt command,

$ sudo apt install -y zip unzip or $ sudo apt-get install -y zip unzip

Install zip & unzip command on CentOS / RHEL / Fedora

Open the terminal and execute the beneath command,

$ sudo yum install -y zip unzip or $ sudo dnf install -y zip unzip

Let’s dive in and see how to zip and unzip files and directories in Linux with zip and unzip command with examples.

Zip command in Linux

Zipping a file is as easy as ABC. The syntax is quite straightforward:

$ zip OPTIONS archive.zip file

A few points to take into consideration before you proceed:

  • You need to have write permissions on the file you are zipping and the directory location before zipping a file.
  • When you unzip or extract a file, the user owns the file/directory.

Example 1) Zipping a single file and multiple files with zip command

Let’s assume you have a text file – reports1.txt – in your current directory and you want to zip it into an archive called reports.zip .

The command for this operation will be:

$ zip reports.zip reports1.txt

zip-single-file-linux

Additionally, you can zip multiple files at a go into an archive as shown:

Читайте также:  Linux unix shell script

$ zip archive.zip file1 file2 file3

Again, let’s assume that we have 3 text files reports1.txt, reports2.txt and reports3.txt. To zip them into the archive reports.zip, run the command:

$ zip reports.zip reports1.txt reports2.txt reports3.txt

zip-multiple-files-linux

Example 2) Adding a file to a zip archive (-u)

At times, you may find the need to add a file to a zip archive. To do so, use the -u flag . For example, to add another file reports4.txt, run:

$ zip -u reports.zip report4.txt

add-file-existing-zip-archive-linux

Example 3) View contents of a zipped file

To view contents of a zipped file, use the command as shown:

view-contents-zipped-file-linux

Example 4) Zipping all the files in the current directory (* )

If you have multiple files in your current directory, you can zip all of them at a go using the wildcard symbol as shown in the syntax below:

For example, to compress all files in the home directory to home.zip archive, execute the command below. Be sure that you are working in the home directory.

zipped-directory-linux

Example 5) Delete a file from an archive (-d)

To remove a file from an archive, invoke the -d flag . For instance, to remove reports4.txt from the zipped file, run:

$ zip -d reports.zip reports4.txt

Example 6) Delete files after zipping (-m)

As you may have noted, the original files remain even after zipping or archiving them.

files-after-zipped-linux

If you prefer you get rid of them during archival and conserve space, invoke the -m option during archival as shown:

$ zip -m reports.zip reports1.txt reports2.txt reports3.txt

remove-orginal-files-after-zipping-linux

Example 7) How to zip a directory (-r)

We have so far seen how to zip files. Often, you will be tasked with zipping directories since they take up more space for the most time.

To zip a folder, use the syntax below. The -r option zipped the folder recursively.

For example, to zip a directory data to an archive data.zip, execute:

zipped-a-folder-linux

As with files, you can also zip several folders concurrently as shown:

$ zip -r archive.zip folder1 folder2 folder3

zipped-multiple-folders-linux

Example 8) Zip a file to a different destination

To zip a file to a different destination other than the current directory simply specify the path to the zipped archive in your syntax as shown:

$ zip /path/to/destination/archive.zip file

For example, the file hello.txt is archived to file.zip file in the Documents/data/sales path.

zip-file-to-different-location

Example 9) Zip both files and directories into their own archives

If, for whatever reason, you want to compress files and directories in the current directory into their individual zip archives, use the for loop as shown:

$ for i in *; do zip -r "$.zip" "$i"; done

For example, the example below compresses the file hello.txt and folder sales into their own archives hello.zip and sales.zip respectively.

zipped-files-directories-for-loop-linux

Unzip command in Linux

Let’s now shift gears and focus on unzip command. The command is used to unzip or decompress zipped files and directories. Let’s take a look at a few example usages of the command:

Example 1) How to view the contents of a zipped file (-l)

Before you are unzipping a file or directory, you can view the archive’s contents using the -l option as shown:

list-contents-zipped-archive-linux

Example 2) How to view detailed contents of a zipped file (-Z)

To view more detailed information about the file such as file permissions and the total size of the files in the archive, use the -Z option as shown:

More-Details-zipped-files-linux

Example 3) How to unzip/decompress a zipped file (-d)

In its basic and simplest syntax, unzipping a zipped file takes the syntax below and occurs in the current working directory.

Читайте также:  Epson v30 linux driver

unzip-compressed-file-linux

Example 4) Unzip a file to another directory

To extract or unzip an archive to a different destination, use the -d flag and specify the destination path as shown

$ unzip archive.zip -d /path/to/destination/folder

$ unzip data.zip -d /home/linuxtechi/Documents

unzip-file-to-differnt-directory

Example 5) Unzip one or more files from an archive

Oftentimes, you will have a compressed file with multiple files. Instead of uncompressing the archive entirely, you may opt to just extract one or two files. To achieve this, specify the file(s) as shown in the syntax:

$ unzip archive.zip file1 file2

For example, to extract a file called hello.txt from the archive home.zip run

Additionally, you can extract the file to a different destination folder instead of the current directory. For example, the command below extracts the hello.txt file to the Documents/data/sales directory.

$ unzip home.zip hello.txt -d Documents/data/sales

unzip-single-file-zipped-archive-linux

Example 6) Exclude certain files from being decompressed (- x)

Just as you can extract a specific file(s) from an archive, so can you exclude certain files from being decompressed. This is possible using the ( -x) option in the syntax shown

For example, the example below excludes files file1.txt & file2.txt from being extracted.

$ unzip archive.zip -x file1.txt file2.txt

exclude-files-while-unzipping-linux

Example 7) How to suppress the output of unzip command (-q)

As you may have noted, details of the unzip operation showing the location of the extraction and files being extracted is printed on the terminal. To suppress this info, use the -q switch as shown:

Example 8) How to overwrite existing files (-o)

When you run the unzip command twice in the same directory, you will be prompted whether to replace, ignore the extraction or rename the existing files as shown in the output below.

Prompt-before-overwriting-unzip-linux

If you want to overwrite the existing file, without being prompted, use the -o option as shown:

Overwrite-files-during-unzip-linux

Example 9) Unzip multiple files

Finally, to unzip multiple files at a go, use a wildcard in the syntax shown:

Take note of the single quote enclosing the *.txt parameter.

This brings us to the end of this guide; we have showcased the usage of the zip and unzip commands and that various options you can use. For more information about their usage, feel free to visit their man pages. We hope you have a firm grip of the usage of both commands and that you can comfortably zip and unzip your files.

Источник

Создаем 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. Ее можно запускать без опций, только указывая имя архива:

Читайте также:  Running linux in ram

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

Источник

Linux zip Command Tutorial with Examples

Linux zip Command Tutorial with Examples

Zip is a compression algorithm and format used to package given files and folders to use less disk space. The zip files have generally the *.zip extension and different tools that can be used to create, add, remove or extract zip files. But the most prolific tool and command is the zip command in Linux operating systems.

zip Command Syntax

The zip command syntax is like below.

zip OPTION ZIP_FILE FILES_FOLDERS
  • OPTION is used to provide different options to the zip file.
  • ZIP_FILE is the file name of the zip file.
  • FILES_FOLDERS can be single or more files and folders which will be zipped into the ZIP_FILE.

Install zip Command

In some cases the zip command is not installed by default. We may need to install zip command explicitly.

Install zip Command In Ubuntu, Debian, Mint, Kali:

Install zip Command In Fedora, RHEL, CentOS:

zip Command Help

The zip command is very old and actively developed command and provides detailed help information about its usage. The simplest and less informative way to get help is running the zip command without any option or parameter.

More detailed help can be listed with the -h2 option to the zip command like below.

Zip A File

Let’s start with a simple example where we zip a single file named cities.txt . The zip file is named as cities.zip like below. There is no need to add an extra parameter to zip it.

adding: cities.txt (deflated 15%)

Zip Multiple Files

We can also zip multipe files in a single step. We just provide the files we want to zip. In the following example we zip files named cities.txt , MyFile.csv and myshell.txt into zip file named my.zip .

Add New File To Zipped File

New files can be added to the existing zip file. Just provide the zip file name and the file name we want to add. In the following example we add the file numbers.txt into the zip file named my.zip .

Delete/Remove A File From Zip File

A single file can be removed from the zipped file. The -d option is used to remove the specified file. In the following example, we remove the file named myshell.txt from the zipped file named my.zip .

If the specified file is deleted or removed from the zip file the previous deleting: line is printed with the deleted file name.

Zip Files and Folders Recursively

Normally when we specify a folder to zip only the folder is zipped and its contents are not zipped. In order to zip a folder or directory with its files and child folders and directories it should be zipped recursively. The -r option is used to zip files and folders recursively.

$ zip -r Downloads.zip Downloads/

The zip command and operation executes different steps. More detailed information about the zip operation can be printed as verbose mode. The -v option is used to print detailed verbosely which can be also called as debugging the zip.

$ zip -r -v Downloads.zip Downloads/

Suppress Output Do Not Print Information

We can prevent the zip command printing information about the zip process and related files and folders names. We should suppress the output with the -q option like below.

$ zip -r -q Downloads.zip Downloads/

Источник

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