SnapOverflow

SnapOverflow Logo SnapOverflow Logo

SnapOverflow Navigation

  • Home
  • Server
  • Ubuntu

Mobile menu

Close
  • Home
  • System Administrators
    • Hot Questions
    • New Questions
    • Tags
  • Ubuntu
    • Hot Questions
    • New Questions
    • Tags
  • Help
Home / ubuntu / Questions / 86849
Accepted
ubuntu-nerd
ubuntu-nerd
Asked: 2011-12-11 20:37:54 +0800 CST2011-12-11 20:37:54 +0800 CST 2011-12-11 20:37:54 +0800 CST

How to unzip a zip file from the Terminal?

  • 772

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?

command-line
  • 9 9 Answers
  • 6402065 Views

9 Answers

  • Voted
  1. Best Answer
    Kelley
    2011-12-11T20:44:10+08:002011-12-11T20:44:10+08:00

    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 the source and destination directories are the same, you can simply do:

    unzip file.zip
    
    • 2677
  2. Panther
    2011-12-11T20:41:57+08:002011-12-11T20:41:57+08:00

    You can simply use unzip.

    Install it:

    apt-get install unzip
    

    And use it:

    cd /path/to/file
    unzip file.zip
    
    • 273
  3. Chris
    2013-09-15T00:01:07+08:002013-09-15T00:01:07+08:00

    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.

    This command installs 7z:

    sudo apt-get install p7zip-full
    

    This command lists the contents of the zip:

    7z l zipfile.zip
    

    This command extracts the contents of the zip:

    7z x zipfile.zip
    
    • 155
  4. cristobalhdez
    2013-05-29T08:45:53+08:002013-05-29T08:45:53+08:00

    You can use:

    unzip file.zip -d somedir
    

    to extract to yourpath/somedir

    If you want to extract to an absolute path, use

    sudo unzip file.zip -d /somedir
    
    • 63
  5. Sergiy Kolodyazhnyy
    2017-06-03T10:43:43+08:002017-06-03T10:43:43+08:00

    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()
    

    Usage:

    ./pyunzip.py master.zip 
    

    or

    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;
    }
    

    Usage:

    ./perlunzip master.zip
    

    or

    perl perlunzip.pl master.zip
    

    See also

    • script for extracting tar.gz archives.
    • How do I create a script file for terminal commands?
    • 61
  6. Nadeem Khan
    2014-03-20T22:28:06+08:002014-03-20T22:28:06+08:00

    If the source and destination directories are the same, you can simply do:

    unzip filename.zip
    
    • 34
  7. Zombo
    2016-12-02T17:50:03+08:002016-12-02T17:50:03+08:00

    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:

    alfa/four.txt
    bravo/four.txt
    

    This is easy with bsdtar:

    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.

    • 27
  8. kashminder
    2014-10-15T02:58:24+08:002014-10-15T02:58:24+08:00

    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
    • 25
  9. M.A.K. Ripon
    2018-10-16T20:44:08+08:002018-10-16T20:44:08+08:00

    Follow these instructions: http://www.codebind.com/linux-tutorials/unzip-zip-file-using-terminal-linux-ubuntu-linux-mint-debian/

    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 Syntax

    $ 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

    tar [OPTION...] [FILE]...
    

    Or

    tar required Flags

    tar {-r|-t|-c|-x|-u}
    

    tar optional Flags

    tar {one of the required Flags} [ -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
    

    enter image description here

    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
    

    enter image description here

    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
    

    enter image description here

    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

    enter image description here

    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:~$ 
    

    enter image description here

    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

    enter image description here

    • 13

Sidebar

Stats

  • Questions 681965
  • Answers 980273
  • Best Answers 280204
  • Users 287326
  • Popular
  • Answers
  • Marko Smith

    How to unzip a zip file from the Terminal?

    • 9 Answers
  • Marko Smith

    How can I copy the contents of a folder to another folder in a different directory using terminal?

    • 8 Answers
  • Marko Smith

    How do I install a .deb file via the command line?

    • 11 Answers
  • Marko Smith

    How do I run .sh scripts?

    • 16 Answers
  • Marko Smith

    How do I install a .tar.gz (or .tar.bz2) file?

    • 14 Answers
  • Marko Smith

    What command do I need to unzip/extract a .tar.gz file?

    • 8 Answers
  • Marko Smith

    How to list all installed packages

    • 24 Answers
  • Marko Smith

    Unable to lock the administration directory (/var/lib/dpkg/) is another process using it?

    • 25 Answers
  • Marko Smith

    How can I add a user as a new sudoer using the command line?

    • 7 Answers
  • Marko Smith

    Change folder permissions and ownership

    • 9 Answers
  • Martin Hope
    ubuntu-nerd How to unzip a zip file from the Terminal? 2011-12-11 20:37:54 +0800 CST
  • Martin Hope
    pandisvezia How can I copy the contents of a folder to another folder in a different directory using terminal? 2011-12-11 17:19:37 +0800 CST
  • Martin Hope
    Scott Szretter Where is the cron / crontab log? 2011-08-12 04:06:43 +0800 CST
  • Martin Hope
    TheXed How do I install a .deb file via the command line? 2011-05-07 09:40:28 +0800 CST
  • Martin Hope
    EmmyS What command do I need to unzip/extract a .tar.gz file? 2011-02-09 14:50:41 +0800 CST
  • Martin Hope
    Ivan How to list all installed packages 2010-12-17 18:08:49 +0800 CST
  • Martin Hope
    La Ode Adam Saputra Unable to lock the administration directory (/var/lib/dpkg/) is another process using it? 2010-11-30 18:12:48 +0800 CST
  • Martin Hope
    David Barry How do I determine the total size of a directory (folder) from the command line? 2010-08-06 10:20:23 +0800 CST
  • Martin Hope
    jfoucher "The following packages have been kept back:" Why and how do I solve it? 2010-08-01 13:59:22 +0800 CST
  • Martin Hope
    David Ashford How can PPAs be removed? 2010-07-30 01:09:42 +0800 CST

Related Questions

Trending Tags

10.10 10.04 gnome networking server command-line package-management software-recommendation sound xorg

Explore

  • Home
  • Questions
    • Hot Questions
    • New Questions
  • Tags
  • Help

Footer

SnapOverflow

About Us

  • About Us
  • Contact Us

Legal Stuff

  • Privacy Policy

Help

© 2022 SOF-TR. All Rights Reserve