How to zip file and folder using command line Ubuntu. Also learn using zip command to create and extract zip file in command line Ubuntu. This post explains using zip in Ubuntu.
Zip Command in Ubuntu
The zip command is used to create package and compress (archive) files. zip is a compression and file packaging utility for Unix and Unix-based Systems.
The program is useful for packaging a set of files for distribution; for archiving files; and for saving disk space by temporarily compressing unused files or directories.
The zip program puts one or more compressed files into a single zip archive, along with information about the files (name, path, date, time of last modification, protection, and check information to verify file integrity). An entire directory structure can be packed into a zip archive with a single command.
Compression ratios of 2:1 to 3:1 are common for text files. zip has one compression method (deflation) and can also store files without compression. (If bzip2 support is added, zip can also compress using bzip2 compression, but such entries require a reasonably modern unzip to decompress.
When bzip2 compression is selected, it replaces deflation as the default method.) zip automatically chooses the better of the two (deflation or store or, if bzip2 is selected, bzip2 or store) for each file to be compressed.
Zip Command Format
The basic command format is
zip command-option output_file_name input_1 input_2
Command 1:
zip -r filename.zip folder
Command 2:
zip -r filename.zip folder1 folder2
Command 3:
zip -r filename.zip /path/to/folder1 /path/to/file2
Create Zip Using GUI
Simple, select the files and folders you wish to Zip. Then, right click on to them and select Compress. Now here you will have to select the compression options – zip or tar.xz or 7z. Select the one and click CREATE button. It will create the Zip archive.
Using Zip Command
The basic command format is “zip options archive inpath inpath …”
where archive is a new or existing zip archive and inpath is a directory or file path optionally including wildcards. When given the name of an existing zip archive, zip will replace identically named entries in the zip archive (matching the relative names as stored in the archive) or add entries for new names.
For example, if foo.zip exists and contains foo/file1 and foo/file2, and the directory foo contains the files foo/file1 and foo/file3, then:
zip -r foo.zip foo
or more concisely
zip -r foo foo
will replace foo/file1 in foo.zip and add foo/file3 to foo.zip. After this, foo.zip contains foo/file1, foo/file2, and foo/file3, with foo/file2 unchanged from before.
So if before the zip command is executed foo.zip has: foo/file1 foo/file2 and directory foo has: file1 file3 then foo.zip will have: foo/file1 foo/file2 foo/file3 where foo/file1 is replaced and foo/file3 is new.
Unzip Command
The unzip command is used to list, test and extract compressed files in a ZIP archive. This unpacks zip archives. The zip and unzip programs can work with archives produced by PKZIP (supporting most PKZIP features up to PKZIP version 4.6), and PKZIP and PKUNZIP can work with archives produced by zip (with some exceptions, notably streamed archives, but recent changes in the zip file standard may facilitate better compatibility).
NOTE: unzip will list, test, or extract files from a ZIP archive, commonly found on MS-DOS systems. The default behavior (with no options) is to extract into the current directory (and subdirectories below it) all files from the specified ZIP archive.
zip version 3.0 is compatible with PKZIP 2.04 and also supports the Zip64 extensions of PKZIP 4.5 which allow archives as well as files to exceed the previous 2 GB limit (4 GB in some cases). zip also now supports bzip2 compression if the bzip2 library is included when zip is compiled.