Unzip a File in Linux Terminal: Using Unzip Command With Examples

Here is how to unzip a file in Ubuntu Linux terminal. This blog post explains the unzip command in Linux with examples. On Ubuntu Linux systems, unzip command is used to list, test and extract compressed files in a ZIP archive.

The default behavior (with no options) is to extract into the current directory (and subdirectories below it) all files from the specified ZIP archive. Unzip command extract the .zip file only and it extracts the content in the current directory. So, if there are the originals files, it will ask if you want to replace it. Also when the zip file contains more than one file, it will do the operation with all the files.

The unzip command has a really simple syntax (see below). If you use it to extract a zip file without any option, it will extract all the files in the current directory.

unzip [option] zip_file

Note: By default, unzip prints the names of all the files it’s extracting and a summary when the extraction is completed. If you want to suppress the output message of the unzip command, you can use the -q switch to not print any of these extraction messages.

unzip -q

The command option -q aloows to perform operations quietly (-qq = even quieter). Ordinarily unzip prints the names of the files it’s extracting or testing, the extraction methods, any file or zipfile comments that may be stored in the archive, and possibly a summary when finished with each archive. The -q[q] options suppress the printing of some or all of these messages.

1. Unzip to a directory

The expected behavior is that you should have the files extracted to a certain directory, normally with the same name as the zip file. You can specify the target directory where you want to extract the files.

unzip -d target_directory zip_file

If the target directory doesn’t exist, it will be created. Also, you can also put the target directory at the end but not all options can be added at the end.

THe argument -d specifies that files will be extracted to 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 directory). This option need not appear at the end of the command line; it is also accepted before the zipfile specification (with the normal options), immediately after the zipfile specification, or between the file(s) and the -x option. The option and directory may be concatenated without any white space between them, but note that this may cause normal shell behavior to be suppressed. In particular, “-d ~” (tilde) is expanded by Unix C shells into the name of the user’s home directory, but “-d~” is treated as a literal subdirectory “~” of the current directory.

2. Extract into the current directory only

Extract the files from archive into the current directory only, regardless of the archive’s internal directory structure.

unzip -j

The command argument -j denotes that the archive’s directory structure is not recreated; all files are deposited in the extraction directory (by default, the current one).

3. Overwrite all the files without prompting

If there are already files with the same name in the directory where you are extracting the files, you’ll be promoted for each such files. You can force overwrite all the files with option -o.

unzip -o -d target_directory zip_file

The command argument -o denotes overwrite existing files without prompting. This is a dangerous option, so use it with care.

4. Do not overwrite any files -n

If you don’t want any existing file to be overwritten by newly extracted files, use the -n option (stands for never overwrite).

unzip -n -d target_directory zip_file

The argument -n is to never overwrite existing files. If a file already exists, skip the extraction of that file without prompting. By default, unzip queries before extracting any file that already exists; the user may choose to overwrite only the current file, overwrite all files, skip extraction of the current file, skip extraction of all existing files, or rename the current file.

NOTE: 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:

  • -a: make unzip auto-convert text files by default.
  • -L: make it convert filenames from uppercase systems to lowercase.
  • -C: make it match names case-insensitively.
  • -q: make it quieter.
  • -o: make it always overwrite.
  • -n: never overwrite files as it extracts them.

Original Article