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:
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 Unzip A File In Linux | Unzip Syntax | 8 Unzip Commands
If you have a compressed zip file, you can unzip it using the Linux command line. Linux’s unzip command is highly flexible just like zip commands and can be used for much more than simply extracting zip files. Files from a ZIP archive, which are often seen on MS-DOS computers, can be listed, tested, or extracted using Unzip. If no settings are provided, the default behavior i.e. with no options will extract all files from the supplied ZIP archive into the current file and all subdirectories beneath it.
Unzip syntax
First of all, you need to install the unzip package. To do so, type in a terminal command:
sudo apt-get install unzip
Once all the packets have been installed, to unzip the archive files type in a terminal command:
A more useful tool is 7z, which zips and unzips a range of compression formats, notably lzma, usually the protocol offering the highest compression rates.
sudo apt-get install p7zip-full
Extract the zip file with Linux
- Open the Files app and navigate to the directory where the zip file is located.
- Locate the file which you want to unzip.
- Right-click on the file and the context menu will appear with the list of options.
- Select the “Extract Here” option to unzip files into the present working directory or choose “Extract to. ” for a different directory.
Unzip arguments in Linux
Some of the most commonly used arguments by unzip are listed below:
It tells the path of the zip archive(s). When a wildcard is used in the file specification, the operating system chooses the order in which each matching file is processed (or file system). The path itself cannot be a wildcard; only the filename can.
A list of archive members to process that can be optional, delimited by spaces. (VMS versions that have VMSCLI specified during compilation had to delimit files using commas. (For further information, see -v in the section below that lists your options.) You can match several members using regular expressions (wildcards).
An optional list of archive members to be excluded from processing. This option can be used to exclude any files that are in subdirectories because wildcard characters often match (‘/’) directory separators (for exceptions see the option -W).
If specified, files will be extracted to the target directory exdir. By default, all files and subdirectories are recreated in the current directory; the -d option allows extraction in an arbitrary directory (always assuming one has permission to write to the current working directory). This option need not appear at the end of the command line; it is also accepted before the zip file specification (with the normal options), immediately after the zip file path, or between the file(s) and the -x option.
Unzip commands/Options in Linux
To unzip a file, you do not need to add any options like the zip command. With unzip, the default behavior is to always seek permission before overwriting current files. Using unzip, you can extract every letter from the archive. Create any necessary subdirectories, then unzip into the current directory and any subdirectories below it options and commands that unzip accepts and uses are:
1. Unzip a file to a different directory
To unzip a ZIP file to a different directory than the current folder, we use the -D (Directory) Option. Go to the file manager and click on the zip file you want to unzip. Now open the terminal window and run the following command:
Command: $ unzip filename.zip -d /path/to/directory
2. Unzip tar/tar.gz/tgz files to a specific directory
Most files in Linux are compressed using the tar format. A .tar file is a collection of uncompressed files, sometimes known as a tarball. Since .tar doesn’t compress anything, it requires a separate compression utility for compression. The tar command allows you to create tar archive files as well as decompress them. This technique can be used on other file types/ file formats also. Using tar UtilityA tar.gz file is a combination of a .tar file and a .gz file. By default, the tar command will extract files to your current directory. Go to the terminal window and run the following command:
Command: $ tar -xf file-name.tar -C /path/to/directory
The -x option tells tar to extract the files. You can also use xargs with tar to create a tar.gz archive and populate it with files from the find command. Note: Some graphical interfaces include a tool for managing tar.gz files without the command line. Working with the graphical interface is a little difficult task.
3. Unzip a password-protected zip file
To unzip a file that is password-protected, invoke the unzip command with the -P option i.e -P (Password) Option followed by the password:
Command: $ unzip -P PasswOrd filename.zip
4. Exclude Files when Unzipping a ZIP File
To exclude specific files or directories from being extracted, use the -X (Exclude) option in the terminal :
Command: $ unzip filename.zip -x file1-to-exclude file2-to-exclude
5. Suppress the Output of the unzip Command
By default, unzip prints the names of all the files it’s extracting and a summary when the extraction is completed. Use the -q switch to suppress the printing of these messages.
Command: $ unzip -q filename.zip
6. Overwrite existing files
If you want to overwrite existing files without prompting, it can be done using the -o option. To run the -o (overwrite) option open the terminal window and run the following command:
Command: $ unzip -o filename.zip
7. Unzip a zip file without overwriting existing files
This command can be performed using the -n option which forces unzip to skip the extraction of a file that already exists:
Command: $ unzip -n filename.zip
8. List the Contents of a zip file
To list the contents of a given zip file we use the -l (list archive) option. To execute the -l option to the current file, open the windows terminal and type the following command:
Command: $ unzip -l filename.zip
Summary
The unzip utility is an extremely effective and straightforward tool for transferring compressed files over a network or even between operating systems. From the above tutorial, you can easily unzip files in Linux systems through the command line using the unzip command.
Suggested reads:
I am a storyteller by nature. At Unstop, I tell stories ripe with promise and inspiration, and in life, I voice out the stories of our four-legged furry friends. Providing a prospect of a good life filled with equal opportunities to students and our pawsome buddies helps me sleep better at night. And for those rainy evenings, I turn to my colors.