LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   unzip / untar one specific file or (sub)folder (https://www.linuxquestions.org/questions/linux-newbie-8/unzip-untar-one-specific-file-or-sub-folder-904168/)

dragonetti 09-21-2011 03:52 AM

unzip / untar one specific file or (sub)folder
 
I have an ubuntu VPS which only has terminal SSH connection. I just started linux (ubuntu) and I am even writing scripts now.

But now I am hitting a wall again on the un-TAR and un-ZIP subject.

Normal unzip/untar actions go in the correct way, but this is when everything needs to be extracted.

But when I only want to extract only 1 specific file or folder (or subfolder) I always end up extracting all the content in it's original path. (which is normal behaviour if the archive is created with full path settings). I read all the man pages about "unzip" and "TAR" I only got to this command, which seemed to work:

Code:

tar -x modsecurity-crs_2.2.1/util/rules-updater.pl -zf modsecurity-crs_2.2.1.tar - C /root/somedir/modsec-tar/ --strip-components=2
But the part in red is a directory that does not exist and even than the command seems to work, this is not correct behaviour. And I could not get any info about "--strip-components".

When I use directory that does not exist (red highilghted), the specific file gets unpacked in the directory where run the extract command. The same goes if I use a directory that exists (red highlighted).

I could replace ".../util/rules-updater.pl" with the following: ".../util/*" which would extract everything under "util".

For the TAR command at least I have something that seems to work, for the unzip command I really do not have any idea how to approach this. All the tutorials I read always comes down to form like this:

Code:

unzip <filename>.zip
But I want to target a specific file/folder in the zip archive and extract that file/folder to directory specified by me.

If someone could provide an example for extracting 1 single specific directory from a TAR and ZIP to a specified folder, that would mean a lot to me.

I read the man pages, searched google and searched here.
(if there is a script which has the same approach, that would be welcome to)

nicolasdiogo 09-21-2011 04:06 AM

unzip -d /path/to/directory myZip.zip

try

Code:

man unzip

dragonetti 09-21-2011 04:15 AM

If try that, like this:
unzip -d /util/ modsecurity-crs_2.2.1.zip

It unpacks everything in the directory /util/ (which is located in the root)
What I want is to extract one specified folder wihtin the zip archive.
In other words: What I want is an extraction of the directory "util" in the zip archive "unzip -d /util/ modsecurity-crs_2.2.1.zip" to a specified directory of my choice.

I did read the man pages about unzip, but that didn't get me any further.

knudfl 09-21-2011 04:31 AM

http://kent.dl.sourceforge.net/proje...s_2.2.1.tar.gz
> modsecurity-crs_2.2.1.tar.gz : 382 kB.

tar xvf modsecurity-crs_2.2.1.tar.gz :
No options to extract one particular folder.
Just delete the unwanted files.

.

dragonetti 09-21-2011 04:41 AM

@knudfl

Then I guess that also goes for the zip (unzip).
That makes things a lot more clear.

If it's not possible to extract single files/folders from a zip archive then I indeed have to extract the full archive and delete unwanted ones.

Ok thanks!

nicolasdiogo 09-21-2011 04:45 AM

try something like

unzip -L yourZip.zip

from this list choose which folder you want to extract

then
unzip yourZip.zip <dir that you want> -d <target folder>

does it work?

dragonetti 09-21-2011 04:58 AM

@nicolasdiogo

Your command worked! But I used the lower "l" not capital "L" switch

-l list files (short format)
-L make (some) names lowercase

But this command you provided, helped:

Code:

unzip yourZip.zip <dir that you want> -d <target folder>
I used it like this:
Code:

unzip modsecurity-crs_2.2.1.zip util/* -d /root/scripttest/test2
I got two warnings though
Code:

caution: filename not matched:  util/regression_tests
caution: filename not matched:  util/runAV

But now I can at least get the contents of "util" to a directory of my choice.

Thank you nicolasdiogo!!!!

nicolasdiogo 09-21-2011 05:04 AM

you are welcome

help others by closing the thread

dragonetti 09-21-2011 05:09 AM

Quote:

Originally Posted by nicolasdiogo (Post 4477981)
you are welcome

help others by closing the thread

Ah sorry i'm new here, didn't see that option.
It should be closed now.

Thanks everyone!


All times are GMT -5. The time now is 07:54 PM.