- How to unzip a zip file from the Terminal?
- 9 Answers 9
- Using scripting tools: Perl and Python
- Python
- Perl
- Install unzip
- UnZip File
- use tar Command in Linux / Unix
- Examples
- How To Extract Zip, Gz, Tar, Bz2, 7z, Xz, and Rar Files in Linux
- 1. Extracting ZIP Files
- 2. Extracting TAR Files
- 3. Extracting GZ Files
- 4. Extracting BZ2 Files
- 5. Extracting 7Z Files
- 6. Extracting XZ Files
- 7. Extracting RAR Files
- Conclusion
How to unzip a zip file from the Terminal?
Just downloaded a .zip file from the internet. I want to use the terminal to unzip the file. What is the correct way to do this?
@burtsevyg the backslash is not necessary there. It will make shell treat \* as literal star symbol. Just use unzip *.zip to make shell expand * to all files ending in .zip
I recommend you use & learn «unar» instead of a zip specific one, unless you need specific features. «Supported file formats include Zip, Tar-GZip, Tar-BZip2, RAR, 7-zip, LhA, StuffIt and many other old and obscure formats. «.
Ubuntu has multiple compress/decompress tools already installed. Run apropos zip from the console for the full list.
9 Answers 9
If the unzip command isn’t already installed on your system, then run:
sudo apt-get install unzip
After installing the unzip utility, if you want to extract to a particular destination folder, you can use:
unzip file.zip -d destination_folder
If you want to extract to a directory with the same name as the zip in your current working directory, you can simply do:
If you are already in the directory you want the file unzipped, omit the 2nd and 3rd arguments, i.e. unzip /path/to/file.zip
I have just used this command. This is an example. Step 1 (I changed to the directory where the zip file is stored): cd /home/paf/Copy/Programming/Javascript/Json Step2 (I extract the zip file in the directory I have just mentioned): unzip file.zip -d /home/paf/Copy/Programming/Javascript/Json
Make sure you extract to a directory, unlike tar archives, you may find many people include dozens of files in their root directory of their zip files, this can make a real mess.
cd /path/to/file unzip file.zip
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
This command lists the contents of the zip:
This command extracts the contents of the zip:
I think the install command should be sudo apt-get install p7zip or sudo apt-get install p7zip-full You need the full version to get the 7z command. The full is also the only one who handles zip and other kinds of formats out of the two.
could you clarify «A more useful tool»? Are you comparing to unzip? Could you provide any examples of features that make 7z more useful, and perhaps in which contexts 7z is preferred?
to extract to yourpath/somedir
If you want to extract to an absolute path, use
sudo unzip file.zip -d /somedir
Using scripting tools: Perl and Python
Many answers here mention tools that require installation, but nobody has mentioned that two of Ubuntu’s scripting languages, Perl and Python, already come with all the necessary modules that allow you to unzip a zip archive, which means you don’t need to install anything else. Just use either of the two scripts presented below to do the job. They’re fairly short and can even be condensed to a one-liner command if we wanted to.
Python
#!/usr/bin/env python3 import sys from zipfile import PyZipFile for zip_file in sys.argv[1:]: pzf = PyZipFile(zip_file) pzf.extractall()
python3 pyunzip.py master.zip
Perl
#!/usr/bin/env perl use Archive::Extract; foreach my $filepath (@ARGV)< my $archive = Archive::Extract->new( archive => $filepath ); $archive->extract; >
perl perlunzip.pl master.zip
Thanks, exactly what I need. I don’t have root and don’t wanna install unzip manually from source. This also can be used with a bash one-liner which mostly will work (assuming no »’ inside the file name): unzip() < python -c "from zipfile import PyZipFile; PyZipFile( '''$1''' ).extractall()"; >
Just use ZipFile class, not PyZipFile, the latter have some specific support for compressing Python libraries
This one line code using perl: perl -e «use Archive::Extract;(Archive::Extract->new(archive => ‘numpy-1.22.3.zip’))->extract;»
If the source and destination directories are the same, you can simply do:
I prefer bsdtar to unzip / zip . For extracting, they are pretty similar:
bsdtar -x -f /one/two/three/four.zip -C /five unzip /one/two/three/four.zip -d /five
However for zipping, bsdtar wins. Say you have this input:
/one/two/three/alfa/four.txt /one/two/three/bravo/four.txt
and want this in the zip file:
bsdtar -a -c -f four.zip -C /one/two/three alfa bravo
zip does not have the -d option like unzip, so you have no way to achieve the above unless you cd first.
As much as I’d like to give the win to a foss tool, apparently bsdtar doesn’t bode well with special characters like at least one in the word Blóðstokkinn when uncrompressing. I didn’t even check when compressing. What a bummer. :/ unzip handled it without problem.
Here is the detailed description of options that I find useful:
Command: unzip -[option] zip-path. -d an optional directory to which to extract files -l List archive files. -P password Use password to decrypt encrypted zipfile entries (if any). -t Test archive files with cyclic redundancy check. -u Update the existing files. -z archive comment
Install unzip
So First of all we need to install unzip on our system if it’s not installed. unzip command is used to extract files from a ZIP archive.
Run the following command to install unzip
sudo apt-get install unzip
$ unzip [-aCcfjLlnopqtuvy] [-d dir] zipfile
Now Follow the steps below:
UnZip File
OPTION 1 – If the Zip File is in the same directory/folder in which your terminal is and we want to extract it in the present working directory.
Use the following command to achieve the above described scenario
sudo unzip zip_file_name.zip
if the zip file is protected with some password, then use the following command :
sudo ubzip -P zip_file_name.zip
Please make sure you use -P (capital P) not -p because the are different options.
OPTION 2 – If the zip file is not present in the same directory and we want to extract/unzip the file in different directory.
Use the following command to achieve the above described scenario
sudo unzip path/filename.zip -d another_path_or_same_path
if we does not use option -d the file will be extracted to present working directory.
And if the zip file is password protected we can also use -P .
use tar Command in Linux / Unix
tar is an acronym for Tape Archive. tar command is used to Manipulates archives in Linux/Unix. System administrators uses tar command frequently to rip a bunch of files or directories into highly compressed archive which are called tarball or tar , bzip and gzip in Linux/Unix system.
tar Syntax
Or
tar required Flags
tar optional Flags
tar [ -d ][-B] [ -F ] [ -E ] [ -i ] [-h ] [ -l ] [ -m ] [ -o ] [ -p ] [ -w] [ -s ] [ -U ] [ -v ] [-Number] [-b Blocks] [-f Archive]
Examples
Create tar Archive File by Compressing an Directory or a Single File
The terminal command below will create a .tar file called sample_dir.tar with a directory /home/codebind/sample_dir or sample_dir in present working directory.
ripon@ripon:~$ tar -cvf sample_dir.tar sample_dir sample_dir/ sample_dir/main.cpp sample_dir/sample.png sample_dir/output ripon@ripon:~$ ls sample_dir sample_dir.tar
Here’s what those flags (-cvf) actually mean
-c, —create – create a new archive
-x, —extract, —get – extract files from an archive
-f, —file ARCHIVE – use archive file or device ARCHIVE
Create tar.gz or tgz Archive File by Compressing an Directory or a Single File
The terminal command below will create a .tar.gz file called sample_dir.tar.gz with a directory /home/codebind/sample_dir or sample_dir in present working directory.
Notice that we have added extra flag -z to the command.Here’s what the flag -z actually mean
-z, —gzip, —gunzip —ungzip – Compress the archive with gzip
ripon@ripon:~$ tar -cvzf sample_dir.tar.gz sample_dirsample_dir/ sample_dir/main.cpp sample_dir/sample.png sample_dir/output ripon@ripon:~$ ls sample_dir sample_dir.tar.gz
The command bellow will create a .tgz file. One this to notice is tar.gz and tgz both are similar.
ripon@ripon:~$ tar -cvzf sample_dir.tgz sample_dirsample_dir/ sample_dir/main.cpp sample_dir/sample.png sample_dir/output ripon@ripon:~$ ls sample_dir sample_dir.tgz
Compressing Multiple Directories or Files at Once
Let’s say, For example we want to compress the sample_dir directory, the java_test directory, and the abc.py file to a tar file called sample_dir.tar.gz .
Run the following command to achieve the goal above.
ripon@ripon:~$ tar -cvzf sample_dir.tar.gz sample_dir java_test abc.py sample_dir/ sample_dir/main.cpp sample_dir/sample.png sample_dir/output java_test/ java_test/HelloCV.java abc.py ripon@ripon:~$ ls sample_dir java_test abc.py sample_dir.tar.gz
Create .bzip2 Archive File by Compressing an Directory or a Single File
ripon@ripon:~$ tar -cjvf sample_dir.tar.bz2 sample_dir sample_dir/ sample_dir/main.cpp sample_dir/sample.png sample_dir/output ripon@ripon:~$
Notice that we have added extra flag -f to the command.Here’s what the flag -f actually mean
-f, —file ARCHIVE – use archive file or device ARCHIVE
Extract .tar Archive File
We can extract or untar the compressed file using the tar command. The command below will extract the contents of sample_dir.tar to the present directory.
ripon@ripon:~$ tar -xvf sample_dir.tar sample_dir/ sample_dir/main.cpp sample_dir/sample.png sample_dir/output ripon@ripon:~$
The following command will extract or untar files in specified Directory i.e. /home/codebind/dir_name in this case.
ripon@ripon:~$ tar -xvf sample_dir.tar -C /home/codebind/dir_name sample_dir/ sample_dir/main.cpp sample_dir/sample.png sample_dir/output ripon@ripon:~$
we have added extra flag -C to the command.Here’s what the flag -C actually mean
-C, —directory DIR – change to directory DIR
How To Extract Zip, Gz, Tar, Bz2, 7z, Xz, and Rar Files in Linux
Linux, the open-source operating system, is favored by many for its flexibility, power, and robustness. It offers a vast array of tools that can handle a multitude of tasks, including working with compressed files. In this comprehensive guide, we will walk you through the process of extracting various file formats like Zip, Gz, Tar, Bz2, 7z, Xz, and Rar in Linux.
1. Extracting ZIP Files
ZIP is one of the most commonly used file formats for compression. To extract a ZIP file in Linux, you will need the unzip command. If it’s not already installed, you can install it using the package manager of your distribution. For example, on Ubuntu, you can install unzip with:
sudo apt-get install unzip
To extract a ZIP file, use the following command:
2. Extracting TAR Files
TAR (short for Tape Archive) is a widely used archive format in Linux. To extract TAR files, use the tar command followed by the options -xvf (x: extract, v: verbose, f: file):
3. Extracting GZ Files
GZ, or gzip, is a file format and a software application used for file compression and decompression. The gunzip command is used for extracting these files. Here is an example:
To exract a tar.gz file, type:
4. Extracting BZ2 Files
BZ2 is a high-quality data compressor used for compressing and decompressing files. To extract a BZ2 file, use the bunzip2 or bzip2 -d command:
To exract a tar.bz2 file, type:
5. Extracting 7Z Files
7Z is a file format that provides a high compression ratio. It’s used with the 7-Zip software, but Linux users can extract it using the p7zip tool. If it’s not installed, install it with:
sudo apt-get install p7zip-full
Then, you can extract a 7Z file with:
6. Extracting XZ Files
XZ is a high-quality compression algorithm and file format. To extract XZ files in Linux, use the unxz command or xz -d:
7. Extracting RAR Files
RAR is a proprietary archive file format that supports data compression, error recovery, and file spanning. To extract RAR files, you need to install the unrar package first:
sudo apt-get install unrar
Then, use the following command to extract a RAR file:
Conclusion
Working with compressed files is a common task in Linux. Understanding how to extract various file formats is an essential skill for any Linux user. This guide has provided you with detailed instructions on how to extract Zip, Gz, Tar, Bz2, 7z, Xz, and Rar files.
Remember, Linux commands can vary slightly depending on your specific distribution and its package manager, so always refer to the appropriate documentation or man pages (man command_name) for the most accurate information.
Master these commands, and you will find managing compressed files in Linux a breeze!