Linux unzip one directory

How to extract only a specific folder from a zipped archive to a given directory?

If you get ‘caution: filename not matched: ‘/dir1/dir2/*’ when you try to extract something like ‘/dir1/dir2/*’ — remove first ‘/’ from the path, so it becomes ‘dir1/dir2/*’. Double-check the existence of the path if this won’t work.

unzip /path/to/archive.zip 'in/archive/folder/*' -d /path/to/unzip/to 

did this? I get caution: filename not matched: foldername/\* when I run unzip $repozip «$2-master/\*» -d /srv/www/magento/ where $2 is the folder name in the zip I want to pull all the files and folders out of

Is there a way to extract contents of «in/archive/folder/*» that does not preserve path «in/archive/folder/»? I end up using mv afterwards to get files where I needed them.

-j strips all path info, and all files go into the target folder. Is there a way to remove only the common part of the path? Akin to zip -r .

The existing two answers are both correct, but it’s a bit tricky to specify the target directory, that should be better clarified.

Let’s say /target/root/ is the target dir upon the original unzip action, e.g.:

unzip -qq src.zip -d "/target/root/" 

Then, we need to use the same /target/root/ as the target dir afterward even though we want to extract only a specific sub-directory, as the way unzip works:

unzip -qq src.zip "sub/dir/*" "/target/root/" 

After all, the rule is actually simple, use the same target root directory for the -d option.

BTW, the -qq option is for unzip to be really quiet, feel free to remove it.

FYI: I did unzip -qq 2.3.zip "magento2-2.3/vendor/*" "vendor/" and got caution: filename not matched: vendor/`. This is on an ubuntu install in bash. I rarely unzip, probably did something else wrong.

For me this unzipped the files in that specific directory, but did not pick up subdirectories; is that a different command?

The first time I read this I couldn’t make any sense of it at all. Then I read it a second time and I thought I understood it. Then I read it a third time and got a different meaning. Your answer should be [better] clarified. … … … … … … … … … … … … … … … … … … … … … … … … … … … … … … … … … … Also, you refer to “the original unzip action”. What makes you think that there was a previous unzip action? What if I want to look at data for April 2017, so I download 2017.zip from the server and I want to extract the April folder (and nothing else)?

Читайте также:  Virtual links in linux

Источник

Practical Examples of the Unzip Command in Linux

Got a zip file in the terminal? Learn how to use the unzip command in Linux with these practical examples.

If you have a zip compressed file, you can unzip it in the Linux command line. The unzip command in Linux is quite versatile and you can use it do a lot more than just extracting zip file.

I have discussed how to gzip a folder in Linux in the past. It’s time to see various usage of the unzip command. Before you do that, make sure that unzip has been installed on your system. You can use your distribution’s package manager to install the command.

On Ubuntu and Debian, you can use this command:

Once you have verified that, let’s see how to use unzip in Linux terminal.

Unzip command in Linux

The unzip command has a really simple syntax:

If you use it to extract a zip file without any option, it will extract all the files in the current directory:

unzip webdesign.zip Archive: webdesign.zip inflating: 339252-PAJF05-394.ai inflating: 339252-PAJF07-322.eps inflating: 339252-PALBTI-224.ai inflating: 339252-PALBTL-394.eps inflating: 339252-PALBTM-53.jpg inflating: License free.txt inflating: License premium.txt

And that’s not what you would want most of the time. It’ll just flood your current directory with all the extracted files.

ls 339252-PAJF05-394.ai 339252-PALBTI-224.ai 339252-PALBTM-53.jpg 'License premium.txt' 339252-PAJF07-322.eps 339252-PALBTL-394.eps 'License free.txt' webdesign.zip

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. You cannot create nested directories in this manner though.

Do note that you can also put the target directory at the end but not all options can be added at the end.

unzip zip_file -d target_directory

2. See the content of the zip file without extracting

If you want to see what the zip file contains, you son’t always have to extract it first. You can use the -l option and it will show the content of the zip file.

As you can see, it also shows the timestamp of the files and the actual size of the individual files in bytes.

[email protected]:$ unzip -l webdesign.zip Archive: webdesign.zip Length Date Time Name --------- ---------- ----- ---- 205358 2018-06-18 23:14 339252-PAJF05-394.ai 996906 2018-06-18 23:14 339252-PAJF07-322.eps 213968 2018-06-20 00:00 339252-PALBTI-224.ai 1005362 2018-06-20 00:00 339252-PALBTL-394.eps 305531 2018-06-20 00:00 339252-PALBTM-53.jpg 1462 2018-06-20 09:45 License free.txt 1116 2018-06-20 09:45 License premium.txt --------- ------- 2729703 7 files

If you want, you can get more information like the compressed size, compression ratio by using the verbose mode with -v option. The CRC-32 in the output is the cyclic redundancy check.

[email protected]:$ unzip -v webdesign.zip Archive: webdesign.zip Length Method Size Cmpr Date Time CRC-32 Name -------- ------ ------- ---- ---------- ----- -------- ---- 205358 Defl:N 142097 31% 2018-06-18 23:14 792f2380 339252-PAJF05-394.ai 996906 Defl:N 221228 78% 2018-06-18 23:14 440390d3 339252-PAJF07-322.eps 213968 Defl:N 147568 31% 2018-06-20 00:00 cdb64dfc 339252-PALBTI-224.ai 1005362 Defl:N 226727 77% 2018-06-20 00:00 aed3d97a 339252-PALBTL-394.eps 305531 Defl:N 183363 40% 2018-06-20 00:00 e4ced90f 339252-PALBTM-53.jpg 1462 Defl:N 728 50% 2018-06-20 09:45 0eb9e17b License free.txt 1116 Defl:N 558 50% 2018-06-20 09:45 165dd84e License premium.txt -------- ------- --- ------- 2729703 922269 66% 7 files

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

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

5. Update files and create if necessary

This is slightly different the overwriting all the files. In this case, only those files will will be overwritten that have newer timestamp than the existing files. If a file doesn’t exist, it will be created.

Читайте также:  Linux стандартный вывод ошибок

You can achieve that with option -u:

unzip -u -d target_directory zip_file

6. Freshen existing files but create none

Slight change from the previous example here. In this one, it will update the existing files if they have older timestamp but it won’t create any new files even if they don’ exist.

The option -f allows you to do that:

unzip -f -d target_directory zip_file

7. Extract in quiet mode

When you unzip a file, it shows all the files that have been extracted on the display. Now imagine a zip file that has hundreds of files in it. If you extract it, your screen will be cluttered with the output.

You can use the quiet mode with option -q and you won’t see anything on the display:

unzip -q -d target_directory zip_file

8. Exclude files from extraction

You can also exclude certain files or certain type of files from being extracted.

unzip zip_file -x file_to_exclude

In my example, let’s say I don’t want to extract any .eps files.

[email protected]:$ unzip webdesign.zip -x *.eps Archive: webdesign.zip inflating: 339252-PAJF05-394.ai inflating: 339252-PALBTI-224.ai inflating: 339252-PALBTM-53.jpg inflating: License free.txt inflating: License premium.txt

Those were some of the most common examples of the unzip command in Linux. You can always check its man page to learn about more options.

Do you use some other option with unzip frequently? Why not share it with us in the comments?

Источник

How to unzip into a given directory

I have a list with the path of .zip files, gathered by the command below. I want to unzip them into that directory, where they exist.

find . -name "*.zip" -print > outfile.txt 
run_all outfile.txt 'unzip -u $1' 

1 Answer 1

unzip -d output_dir/ zipfiles.zip 
#!/bin/bash for i in `cat outfile.txt`; do output_dir=$(dirname $i) unzip -d $output_dir $i done 

EDIT: As @dessert suggests, you may do this as a better alternative:

while IFS='' read -r i || [[ -n "$i" ]]; do unzip -d $ $i; done  

This will break on a single whitespace in a path, you should rather use read . Additionally the bash shell can do dirname 's work with parameter expansion: while IFS='' read -r i || [[ -n "$i" ]]; do unzip -d $ $i; done

You must log in to answer this question.

Related

Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.7.14.43533

Ubuntu and the circle of friends logo are trade marks of Canonical Limited and are used under licence.

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Источник

Unzip specific directory without creating top directory

I want to extract everything under Release , preserving the directory structure, but when I run this: unzip archive.zip Release/* -d /tmp It creates the top Release folder:

/tmp/Release/ /tmp/Release/file /tmp/Release/subdirectory/file /tmp/Release/subdirectory/file2 /tmp/Release/subdirectory/file3 

How I can extract everything inside Release without creating a Release folder, like this:

/tmp/ /tmp/file /tmp/subdirectory/file /tmp/subdirectory/file2 /tmp/subdirectory/file3 

4 Answers 4

Create a symlink at the location that the top directory would be extracted to that points at your current location:

The j flag should prevent folder creation unzip -j archive.zip -d .

-j junk paths. The archive's directory structure is not recreated; all files are deposited in the extraction directory (by default, the current one). 

I think this is close, but the OP was looking to only skip creation of the top level directory and preserve the remaining directory structure. the -j option dumps all files into the current directory without regard to the directory structure in the archive.

Python script for flattening extracted tree

The script written bellow extracts zip file and moves files contained within topmost directory out of it to the current working directory. This quick script is tailored to suit this particular question where there is one single topmost directory that contains all the files, although with a few edits can be made suitable for more general cases.

#!/usr/bin/env python3 import sys import os from zipfile import PyZipFile for zip_file in sys.argv[1:]: pzf = PyZipFile(zip_file) namelist=pzf.namelist() top_dir = namelist[0] pzf.extractall(members=namelist[1:]) for item in namelist[1:]: rename_args = [item,os.path.basename(item)] print(rename_args) os.rename(*rename_args) os.rmdir(top_dir) 

Test run

Here's an example of how the script is supposed to work. Everything extracted to current working directory, but source file can be in differet directory altogether. The test is performed on the zip archive of my personal github repository.

$ ls flatten_zip.py* master.zip $ ./flatten_zip.py master.zip ['utc-time-indicator-master/.gitignore', '.gitignore'] ['utc-time-indicator-master/LICENSE', 'LICENSE'] ['utc-time-indicator-master/utc-time-indicator', 'utc-time-indicator'] ['utc-time-indicator-master/utc_indicator.png', 'utc_indicator.png'] $ ls flatten_zip.py* LICENSE master.zip utc_indicator.png utc-time-indicator 

Test with source file being in different location

$ mkdir test_unzip $ cd test_unzip $ ../flatten_zip.py ../master.zip ['utc-time-indicator-master/.gitignore', '.gitignore'] ['utc-time-indicator-master/LICENSE', 'LICENSE'] ['utc-time-indicator-master/utc-time-indicator', 'utc-time-indicator'] ['utc-time-indicator-master/utc_indicator.png', 'utc_indicator.png'] $ ls LICENSE utc_indicator.png utc-time-indicator 

Источник

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