Linux unzip one folder

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 

Источник

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

Читайте также:  Linux ограничить размер директории

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

Источник

How to Unzip into a Specific folder in Linux

Unzipping files is a common task in Linux. However, it can become a bit tricky when you need to unzip files into a specific folder. In this article, we will explore different methods to unzip files into a specific folder in Linux.

Method 1: Using the unzip command

The unzip command is a built-in utility in Linux that can be used to extract files from zip archives. To unzip a file into a specific folder, you can use the -d flag followed by the path to the target folder.

For example, if you want to unzip a file called sample.zip into a folder called “unzipped” in your home directory, you can use the following command:

unzip ~/sample.zip -d ~/unzipped 

Method 2: Using the tar command

The tar command is another built-in utility in Linux that can be used to create and extract tar archives. To extract a tar archive into a specific folder, you can use the -C flag followed by the path to the target folder.

For example, if you want to extract a file called sample.tar into a folder called “unzipped” in your home directory, you can use the following command:

tar -xvf ~/sample.tar -C ~/unzipped 

Method 3: Using GUI tools

If you prefer to use a graphical user interface (GUI), there are many file archiving and extraction tools available in Linux. One popular tool is the file roller, which is the default archive manager for many Linux distributions.

Читайте также:  Объекты файловой системы linux

To extract a file using the file roller, you can simply right-click on the zip or tar archive and select “Extract Here” or “Extract to” to specify the target folder.

Conclusion

Unzipping files into a specific folder in Linux is a simple task once you know the right command syntax or have a GUI tool that supports this functionality. The methods discussed in this article should be enough to get you started. However, there are many other options and flags available for the unzip and tar commands that you can explore by reading their respective manuals.

Источник

unzip a directory in unix [closed]

This is creating the root structure a/b/c/d/e/f/sample . I need to unzip a folder in the current directory.

Can you please try and explain this question a bit more? As it stands it’s very unclear what you’re asking. There are 3 ways to interpret what you’re saying. (1) unzip with no directories (2) blindly unzip just the contents of the folder without knowing it ahead of time, or (3) unzip a specific folder in the zip file.

3 Answers 3

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

Note that this will unpack all the files into the current directory, regardless of the in-file directory structure. That is probably not what the OP wants.

Unzipping a folder without it’s full hierarchy

If you’re just trying to unzip the contents of a directory within the zip file without retaining any of the hierarchy this folder is inside of, in the zip file then you’re out of luck. There is no method that provides this. The problem is with how zip files are created. They are typically built as a solid blob and so are not easily accessible in the way that most would think.

The only 2 approaches I’ve seen which can do this are to create a loop which goes through the index of a zip file, something like this:

Method #1

for i in $(unzip -l zipfile.zip); do # look for specific location # when there, make directory structure you want # unzip zipfile.zip $i -d /some/tmp/dir # move file from /some/tmp/dir to new directory done 

The above can be done in a number of programming languages such as Perl, Python, etc.

Method #2

The other method involves making a copy of the zip file and then using the tool zipnote to manipulate the index of the files inside the archive, and then extract the files once the zip file’s index has been manipulated.

Unzipping a folder with it’s full hierarchy

If on the other hand you’re simply trying to extract a single folder from the zip file, you can use this command to do so:

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

Example

Sample directory structure.

$ tree . |-- 1 | |-- 2 | | |-- 3 | | | `-- 4 | | | `-- 5 | | | |-- file1 | | | |-- file2 | | | |-- file3 | | | |-- file4 | | | `-- file5 | | `-- afile | `-- afile `-- afile 
$ zip -r ../afile.zip . adding: afile (stored 0%) adding: 1/ (stored 0%) adding: 1/afile (stored 0%) adding: 1/2/ (stored 0%) adding: 1/2/afile (stored 0%) adding: 1/2/3/ (stored 0%) adding: 1/2/3/4/ (stored 0%) adding: 1/2/3/4/5/ (stored 0%) adding: 1/2/3/4/5/file1 (stored 0%) adding: 1/2/3/4/5/file2 (stored 0%) adding: 1/2/3/4/5/file3 (stored 0%) adding: 1/2/3/4/5/file5 (stored 0%) adding: 1/2/3/4/5/file4 (stored 0%) 

Confirming contents of zip file.

$ unzip -l ../afile.zip Archive: ../afile.zip Length Date Time Name --------- ---------- ----- ---- 0 11-07-2013 16:43 afile 0 11-07-2013 16:43 1/ 0 11-07-2013 16:43 1/afile 0 11-07-2013 16:43 1/2/ 0 11-07-2013 16:43 1/2/afile 0 11-07-2013 15:51 1/2/3/ 0 11-07-2013 15:51 1/2/3/4/ 0 11-07-2013 15:51 1/2/3/4/5/ 0 11-07-2013 15:51 1/2/3/4/5/file1 0 11-07-2013 15:51 1/2/3/4/5/file2 0 11-07-2013 15:51 1/2/3/4/5/file3 0 11-07-2013 15:51 1/2/3/4/5/file5 0 11-07-2013 15:51 1/2/3/4/5/file4 --------- ------- 0 13 files 

Now we unzip just folder 5.

$ unzip afile.zip "1/2/3/4/5*" -d new/ Archive: afile.zip creating: new/1/2/3/4/5/ extracting: new/1/2/3/4/5/file1 extracting: new/1/2/3/4/5/file2 extracting: new/1/2/3/4/5/file3 extracting: new/1/2/3/4/5/file5 extracting: new/1/2/3/4/5/file4 
$ tree . . `-- 1 `-- 2 `-- 3 `-- 4 `-- 5 |-- file1 |-- file2 |-- file3 |-- file4 `-- file5 

I don’t think you can do it directly, although it can be done in a slightly roundabout way by unpacking the archive in a temporary location and moving the files into the current directory.

Читайте также:  Extend linux file system on

The problem is that you don’t really know about the structure of the zip file contents, so this is quite hard to solve in the general case. If what you want to do is to just «junk» the first directory component and dropping everything under it into the current directory, then we can write a shell script or shell function to do it. I’m going to show you an example for GNU bash; it shouldn’t be hard to adapt to other shells if you prefer something else. You can put this at the end of your ~/.bashrc to try it out.

unzip_to_current_dir() < zipfilenames="$@" for zipfilename in $; do tempdir=$(mktemp -d) unzip -d "$" "$" mv "$/*/*" ./ rm -rf "$" done > 

The idea is to take the zip file names and then, one by one, create a temporary directory, unzip into that directory, move everything in the first level subdirectory of the unzip target directory into the current directory, and then clean up the temporary directory before moving on to the next file. This effectively removes the first component of the path in the zip file, so that unpacking a zip file that has a/abc, a/bcd, a/cde and a/def/ghi stores those files as ./abc, ./bcd, ./cde and ./def/ghi.

To easily test if this does what you want, take the four most-indented lines and execute them manually, one at a time, only substituting your own value for $ . I strongly suggest doing that in a directory that does not hold any critical data; I highly doubt that there is a data-corrupting bug in that script, but: if it breaks, you get to keep all the pieces.

Источник

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