Unzip with linux command

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:

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:

Читайте также:  Iptv kodi для linux

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 

Источник

Linux unzip command

Computer Hope

On Unix-like operating systems, the unzip command lists, extracts, and tests compressed files in a ZIP archive.

This page covers the Linux version of unzip.

Description

unzip lists, tests, or extracts files from archives of the zip format, which are most commonly found on MS-DOS and Windows systems. The default behavior (with no options) is to extract into the current directory (and possibly the subdirectories below it) all files from the specified zip archive. A companion program, zip, creates zip archives. Both zip and unzip are compatible with archives created by PKWARE’s PKZIP and PKUNZIP programs for MS-DOS.

Syntax

unzip [-Z] [-cflptTuvz[abjnoqsCDKLMUVWX$/:^]] file[.zip] [file(s) . ] [-x xfile(s) . ] [-d exdir]

Arguments

unzip accepts the following arguments:

Options

Additionally, unzip accepts the following options:

-Z zipinfo mode. If the first option on the command line is -Z, the remaining options are taken to be zipinfo options.
-A [OS/2, Unix DLL] print extended help for the DLL’s programming interface (API).
-c extract files to stdout/screen («the CRT»). This option is similar to the -p option except that the name of each file is printed as it is extracted, the -a option is allowed, and ASCII-EBCDIC conversion is automatically performed if appropriate. This option is not listed in the unzip usage screen.
-f freshen existing files, i.e., extract only those files that already exist on disk and that are newer than the disk copies. By default, unzip queries before overwriting, but the -o option may be used to suppress the queries. Note that under many operating systems, the TZ (timezone) environment variable must be set correctly for -f and -u to work properly (under Unix the variable is usually set automatically). The reasons for this are somewhat subtle but have to do with the differences between DOS-format file times (always local time) and Unix-format times (always in GMT/UTC) and the necessity to compare the two. A typical TZ value is «PST8PDT» (US Pacific time with automatic adjustment for Daylight Savings Time).
-l list archive files (short format). The names, uncompressed file sizes and modification dates and times of the specified files are printed, along with totals for all files specified. If unzip was compiled with OS2_EAS defined, the -l option also lists columns for the sizes of stored OS/2 extended attributes (EAs) and OS/2 access control lists (ACLs). Also, the zipfile comment and individual file comments (if any) are displayed. If a file was archived from a single-case file system (for example, the old MS-DOS FAT file system) and the -L option was given, the filename is converted to lowercase and is prefixed with a caret (^).
-p extract files to pipe (standard output, stdout). Nothing but the file data is sent to stdout, and the files are always extracted in binary format, just as they are stored (no conversions).
-t test archive files. This option extracts each specified file in memory and compares the CRC (cyclic redundancy check, an enhanced checksum) of the expanded file with the original file’s stored CRC value.
-T [most OSes] set the timestamp on the archive(s) to that of the newest file in each one. This corresponds to zip’s -go option except that it can be used on wildcard zipfiles (e.g., «unzip -T \*.zip«) and is much faster.
-u update existing files and create new ones if needed. This option performs the same function as the -f option, extracting (with query) files that are newer than those with the same name on disk, and in addition it extracts those files that do not already exist on disk. See -f above for information on setting the timezone properly.
-v list archive files (verbose format) or show diagnostic version info. This option has evolved and now behaves as both an option and a modifier. As an option it has two purposes: when a zipfile is specified with no other options, -v lists archive files verbosely, adding to the basic -l info the compression method, compressed size, compression ratio and 32-bit CRC. In contrast to most of the competing utilities, unzip removes the 12 additional header bytes of encrypted entries from the compressed size numbers. Therefore, compressed size and compression ratio figures are independent of the entry’s encryption status and show the correct compression performance. (The complete size of the encrypted compressed data stream for zipfile entries is reported by the more verbose zipinfo reports.) When no zipfile is specified (that is, the complete command is «unzip -v«), a diagnostic screen is printed. In addition to the normal header with release date and version, unzip lists the home Info-ZIP ftp site and where to find a list of other ftp and non-ftp sites; the target operating system for which it was compiled, as well as (possibly) the hardware on which it was compiled, the compiler and version used, and the compilation date; any special compilation options that might affect the program’s operation (see also decryption below); and any options stored in environment variables that might do the same (see environment options below). As a modifier it works in conjunction with other options (e.g., -t) to produce more verbose or debugging output.
-z display only the archive comment.
Читайте также:  Как добавить пользователя в кали линукс

Modifiers

-a convert text files. Ordinarily all files are extracted exactly as they are stored (as «binary» files). The -a option causes files identified by zip as text files (those with the ‘t‘ label in zipinfo listings, rather than ‘b‘) to be automatically extracted as such, converting line endings, end-of-file characters and the character set itself as necessary. (For example, Unix files use line feeds (LFs) for end-of-line (EOL) and have no end-of-file (EOF) marker; Macintoshes use carriage returns (CRs) for EOLs; and most PC operating systems use CR+LF for EOLs and control-Z for EOF. Also, IBM mainframes and the Michigan Terminal System use EBCDIC rather than the more common ASCII character set, and NT supports Unicode.) Note that zip‘s identification of text files is by no means perfect; some «text» files may actually be binary and vice versa. unzip therefore prints «[text]» or «[binary]» as a visual check for each file it extracts when using the -a option. The -aa option forces all files to be extracted as text, regardless of the supposed file type. On VMS, see also -S
-b [general] treat all files as binary (no text conversions). This is a shortcut for —a.
-b [Tandem] force the creation files with file code type 180 (‘C‘) when extracting Zip entries marked as «text». (On Tandem, -a is enabled by default; see above).
-b [VMS] auto-convert binary files (see -a above) to fixed-length, 512-byte record format. Doubling the option (-bb) forces all files to be extracted in this format. When extracting to standard output (-c or -p option in effect), the default conversion of text record delimiters is disabled for binary (-b) resp. all (-bb) files.
-B [when compiled with UNIXBACKUP defined] save a backup copy of each overwritten file. The backup file is gets the name of the target file with a tilde and optionally a unique sequence number (up to 5 digits) appended. The sequence number is applied whenever another file with the original name plus tilde already exists. When used together with the «overwrite all» option -o, numbered backup files are never created. In this case, all backup files are named as the original file with an appended tilde, existing backup files are deleted without notice. This feature works similarly to the default behavior of emacs in many locations.

As an example, an old copy of «foo» is renamed to «foo~«.

The option -UU allows to entirely disable the recognition of UTF-8 encoded filenames. The handling of filename codings within unzip falls back to the behaviour of previous versions.

  • «*.c» matches «foo.c» but not «mydir/foo.c«
  • «**.c» matches both «foo.c» and «mydir/foo.c«
  • «*/*.c» matches «bar/foo.c» but not «baz/bar/foo.c«
  • «??*/*» matches «ab/foo» and «abc/foo» but not «a/foo» or «a/b/foo«

Environment options

unzip‘s default behavior may be modified via options placed in an environment variable. This can be done with any option, but it is probably most useful with the -a, -L, -C, -q, -o, or -n modifiers: make unzip auto-convert text files by default, make it convert filenames from uppercase systems to lowercase, make it match names case-insensitively, make it quieter, or make it always overwrite or never overwrite files as it extracts them. For example, to make unzip act as quietly as possible, only reporting errors, one would use one of the following commands:

Источник

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