LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-21-2011, 04:52 AM   #1
dragonetti
LQ Newbie
 
Registered: Sep 2011
Posts: 19

Rep: Reputation: Disabled
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)
 
Old 09-21-2011, 05:06 AM   #2
nicolasdiogo
Member
 
Registered: Oct 2003
Location: UK
Distribution: debian lenny x64
Posts: 130

Rep: Reputation: 20
unzip -d /path/to/directory myZip.zip

try

Code:
man unzip
 
Old 09-21-2011, 05:15 AM   #3
dragonetti
LQ Newbie
 
Registered: Sep 2011
Posts: 19

Original Poster
Rep: Reputation: Disabled
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.
 
Old 09-21-2011, 05:31 AM   #4
knudfl
LQ 5k Club
 
Registered: Jan 2008
Location: Copenhagen, DK
Distribution: pclos2017 CentOS6.9 CentOS7.3 + 50+ other Linux OS, for test only.
Posts: 16,737

Rep: Reputation: 3317Reputation: 3317Reputation: 3317Reputation: 3317Reputation: 3317Reputation: 3317Reputation: 3317Reputation: 3317Reputation: 3317Reputation: 3317Reputation: 3317
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.

.
 
Old 09-21-2011, 05:41 AM   #5
dragonetti
LQ Newbie
 
Registered: Sep 2011
Posts: 19

Original Poster
Rep: Reputation: Disabled
@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!
 
Old 09-21-2011, 05:45 AM   #6
nicolasdiogo
Member
 
Registered: Oct 2003
Location: UK
Distribution: debian lenny x64
Posts: 130

Rep: Reputation: 20
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?
 
Old 09-21-2011, 05:58 AM   #7
dragonetti
LQ Newbie
 
Registered: Sep 2011
Posts: 19

Original Poster
Rep: Reputation: Disabled
@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!!!!
 
Old 09-21-2011, 06:04 AM   #8
nicolasdiogo
Member
 
Registered: Oct 2003
Location: UK
Distribution: debian lenny x64
Posts: 130

Rep: Reputation: 20
you are welcome

help others by closing the thread
 
Old 09-21-2011, 06:09 AM   #9
dragonetti
LQ Newbie
 
Registered: Sep 2011
Posts: 19

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by nicolasdiogo View Post
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!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
untar to a specific folder kennmurrah Linux - General 4 05-22-2010 07:06 AM
Untar File to a specific location ! Libu Linux - General 3 12-19-2005 08:14 PM
Is there a GUI way to untar/unzip things? coopns Linux - Newbie 2 05-26-2004 03:08 PM
How to unzip and untar a xxx.tar.gzip.part file> linuxharsha Linux - Software 8 02-15-2004 05:27 AM
How to untar and unzip .tar.gzip.part file? linuxharsha Linux - Software 2 02-14-2004 11:37 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:21 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration